6
0

ProjectManager.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, 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. # create project.json
  50. self.write_project(uuid)
  51. def update_project(self, uuid, update):
  52. # abort if uuid is no valid key
  53. if uuid not in self.keys():
  54. return
  55. # set values and write to disk
  56. self[uuid].update_properties(update)
  57. self.write_project(uuid)
  58. def delete_project(self, uuid):
  59. # abort if uuid is no valid key
  60. if uuid not in self.keys():
  61. return
  62. # delete project folder
  63. folder = path.join('projects', uuid)
  64. rmtree(folder)
  65. # delete project data
  66. del self[uuid]
  67. def predict(self, uuid, identifiers=None):
  68. # abort if uuid is no valid key
  69. if uuid not in self.keys():
  70. return
  71. project = self[uuid]
  72. # get identifiers
  73. if identifiers is None:
  74. identifiers = list(project['data'].keys())
  75. # run prediction
  76. project.predict(identifiers)
  77. def fit(self, uuid):
  78. # abort if uuid is no valid key
  79. if uuid not in self.keys():
  80. return
  81. project = self[uuid]
  82. # run fit
  83. project.fit()