6
0

ProjectManager.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. project['jobs'] = {}
  25. self[project['id']] = project
  26. def __write_project(self, uuid):
  27. with open(path.join('projects', uuid, 'project.json'), 'w') as file:
  28. copy = self[uuid].copy()
  29. del copy['jobs']
  30. dump(copy, file, indent=4)
  31. def create_project(self, name, description, model):
  32. # create dict representation
  33. uuid = str(uuid1())
  34. self[uuid] = Project({
  35. 'id': uuid,
  36. 'name': name,
  37. 'description': description,
  38. 'created': int(time()),
  39. 'pipeline': {
  40. 'model-distribution': model
  41. },
  42. 'data': {},
  43. 'labels': {},
  44. 'jobs': {}
  45. }, self)
  46. # create project directory
  47. folder = path.join('projects', uuid)
  48. mkdir(folder)
  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):
  68. # abort if uuid is no valid key
  69. if uuid not in self.keys():
  70. return
  71. project = self[uuid]
  72. # load pipeline
  73. with PipelineManager(project) as pm:
  74. # TODO add jobs to list
  75. # run predictions
  76. for file_id in identifiers:
  77. if file_id in project['data'].keys():
  78. pm.run(project['data'][file_id])