Python-ELMO is a Python library which offers an encapsulation of the binary tool ELMO, in order to manipulate it easily in Python and SageMath script.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

project_reader.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from project_base import SimulationProject
  2. import os, re
  3. import inspect
  4. PROJECTS_REPOSITORY = 'projects'
  5. class ProjectReader:
  6. def __init__(self):
  7. pass
  8. def get_projects(self):
  9. projects = {}
  10. for root, repositories, files in os.walk(PROJECTS_REPOSITORY):
  11. for filename in files:
  12. if re.fullmatch(r'.*project.*\.py', filename):
  13. # Encapsulation the project
  14. complete_filename = root+'/'+filename
  15. globals = {
  16. #'__builtins__': {'__build_class__': __build_class__},
  17. 'SimulationProject': SimulationProject,
  18. }
  19. locals = {}
  20. # Read the project code
  21. with open(complete_filename, 'r') as _file:
  22. project = '\n'.join(_file.readlines())
  23. exec(project, globals, locals)
  24. # Extract the simulations
  25. for key, obj in locals.item():
  26. if inspect.isclass(obj) and issubclass(obj, SimulationProject):
  27. if key in projects:
  28. print('Warning! Multiplie simulation with the same name. Simulation ignored: {} in {}'.format(key, complete_filename[len(PROJECTS_REPOSITORY)+1:]))
  29. else:
  30. obj.set_project_directory(root[len(PROJECTS_REPOSITORY)+1:])
  31. projects[key] = obj
  32. return projects
  33. def get_project_classes(self):
  34. return self.get_projects().values()