ProjectManager.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from glob import glob
  2. from json import load, dump
  3. from os import path, mkdir
  4. from shutil import rmtree
  5. from time import time
  6. from uuid import uuid1
  7. from pycs import ApplicationStatus
  8. from pycs.observable import ObservableDict
  9. from pycs.pipeline.PipelineManager import PipelineManager
  10. from pycs.projects.Project import Project
  11. class ProjectManager(ObservableDict):
  12. def __init__(self, app_status: ApplicationStatus):
  13. # TODO create projects folder if it does not exist
  14. self.app_status = app_status
  15. # initialize observable dict with no keys and
  16. # app_status object as parent
  17. super().__init__({}, app_status)
  18. app_status['projects'] = self
  19. # find projects
  20. for folder in glob('projects/*'):
  21. # load project.json
  22. with open(path.join(folder, 'project.json'), 'r') as file:
  23. project = Project(load(file), self)
  24. self[project['id']] = project
  25. def __write_project(self, uuid):
  26. with open(path.join('projects', uuid, 'project.json'), 'w') as file:
  27. dump(self[uuid], file, indent=4)
  28. def create_project(self, name, description, model):
  29. # create dict representation
  30. uuid = str(uuid1())
  31. self[uuid] = Project({
  32. 'id': uuid,
  33. 'name': name,
  34. 'description': description,
  35. 'created': int(time()),
  36. 'pipeline': {
  37. 'model-distribution': model
  38. },
  39. 'data': {}
  40. }, self)
  41. # create project directory
  42. folder = path.join('projects', uuid)
  43. mkdir(folder)
  44. # create project.json
  45. self.__write_project(uuid)
  46. def update_project(self, uuid, update):
  47. # abort if uuid is no valid key
  48. if uuid not in self.keys():
  49. return
  50. # set values and write to disk
  51. self[uuid].update_properties(update)
  52. self.__write_project(uuid)
  53. def delete_project(self, uuid):
  54. # abort if uuid is no valid key
  55. if uuid not in self.keys():
  56. return
  57. # delete project folder
  58. folder = path.join('projects', uuid)
  59. rmtree(folder)
  60. # delete project data
  61. del self[uuid]
  62. def predict(self, uuid, identifiers):
  63. # abort if uuid is no valid key
  64. if uuid not in self.keys():
  65. return
  66. project = self[uuid]
  67. # load pipeline
  68. with PipelineManager(project) as pm:
  69. # TODO add jobs to list
  70. # run predictions
  71. for file_id in identifiers:
  72. if file_id in project['data'].keys():
  73. pm.run(project['data'][file_id])