ProjectManager.py 3.8 KB

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