ProjectManager.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from glob import glob
  2. from json import load, dump
  3. from os import path, mkdir
  4. from shutil import rmtree, copytree
  5. from time import time
  6. from uuid import uuid1
  7. from pycs import ApplicationStatus
  8. from pycs.observable import ObservableDict
  9. from pycs.projects.Project import Project
  10. class ProjectManager(ObservableDict):
  11. def __init__(self, app_status: ApplicationStatus):
  12. # TODO create projects folder if it does not exist
  13. self.app_status = app_status
  14. # initialize observable dict with no keys and
  15. # app_status object as parent
  16. super().__init__({}, app_status)
  17. app_status['projects'] = self
  18. # find projects
  19. for folder in glob('projects/*'):
  20. # load project.json
  21. with open(path.join(folder, 'project.json'), 'r') as file:
  22. project = Project(load(file), self)
  23. self[project['id']] = project
  24. def write_project(self, uuid):
  25. copy = self[uuid].copy()
  26. del copy['jobs']
  27. del copy['model']
  28. with open(path.join('projects', uuid, 'project.json'), 'w') as file:
  29. dump(copy, file, indent=4)
  30. def create_project(self, name, description, model, label, unmanaged=None):
  31. # create dict representation
  32. uuid = str(uuid1())
  33. # create project directory
  34. folder = path.join('projects', uuid)
  35. mkdir(folder)
  36. # copy model to project directory
  37. copytree(self.parent['models'][model]['path'], path.join(folder, 'model'))
  38. # create project object
  39. self[uuid] = Project({
  40. 'id': uuid,
  41. 'name': name,
  42. 'description': description,
  43. 'unmanaged': unmanaged,
  44. 'created': int(time()),
  45. 'data': {},
  46. 'labels': {},
  47. 'jobs': {}
  48. }, self)
  49. # add labels if id is valid
  50. if label in self.parent['labels']:
  51. code_path = path.join(self.parent['labels'][label]['path'], self.parent['labels'][label]['code']['module'])
  52. module_name = code_path.replace('/', '.').replace('\\', '.')
  53. class_name = self.parent['labels'][label]['code']['class']
  54. mod = __import__(module_name, fromlist=[class_name])
  55. cl = getattr(mod, class_name)
  56. provider = cl(self.parent['labels'][label]['path'], self.parent['labels'][label])
  57. for label in provider.get_labels():
  58. self[uuid].add_label(label['name'], identifier=label['id'])
  59. # create project.json
  60. self.write_project(uuid)
  61. def update_project(self, uuid, update):
  62. # abort if uuid is no valid key
  63. if uuid not in self.keys():
  64. return
  65. # set values and write to disk
  66. self[uuid].update_properties(update)
  67. self.write_project(uuid)
  68. def delete_project(self, uuid):
  69. # abort if uuid is no valid key
  70. if uuid not in self.keys():
  71. return
  72. # delete project folder
  73. folder = path.join('projects', uuid)
  74. rmtree(folder)
  75. # delete project data
  76. del self[uuid]
  77. def predict(self, uuid, identifiers=None):
  78. # abort if uuid is no valid key
  79. if uuid not in self.keys():
  80. return
  81. project = self[uuid]
  82. # get identifiers
  83. if identifiers is None:
  84. if project['unmanaged'] is None:
  85. identifiers = list(project['data'].keys())
  86. else:
  87. identifiers = project.unmanaged_files_keys
  88. # run prediction
  89. project.predict(identifiers)
  90. def fit(self, uuid):
  91. # abort if uuid is no valid key
  92. if uuid not in self.keys():
  93. return
  94. project = self[uuid]
  95. # run fit
  96. project.fit()