ProjectManager.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. provider.close()
  63. # create project.json
  64. self.write_project(uuid)
  65. def update_project(self, uuid, update):
  66. # abort if uuid is no valid key
  67. if uuid not in self.keys():
  68. return
  69. # set values and write to disk
  70. self[uuid].update_properties(update)
  71. self.write_project(uuid)
  72. def delete_project(self, uuid):
  73. # abort if uuid is no valid key
  74. if uuid not in self.keys():
  75. return
  76. # delete project folder
  77. folder = path.join('projects', uuid)
  78. rmtree(folder)
  79. # delete project data
  80. del self[uuid]
  81. def predict(self, uuid, identifiers=None, unlabeled=False):
  82. # abort if uuid is no valid key
  83. if uuid not in self.keys():
  84. return
  85. project = self[uuid]
  86. # get identifiers
  87. if identifiers is None:
  88. if project['unmanaged'] is None:
  89. identifiers = list(project['data'].keys())
  90. else:
  91. identifiers = project.unmanaged_files_keys
  92. # run prediction
  93. project.predict(identifiers, unlabeled=unlabeled)
  94. def fit(self, uuid):
  95. # abort if uuid is no valid key
  96. if uuid not in self.keys():
  97. return
  98. project = self[uuid]
  99. # run fit
  100. project.fit()