ProjectManager.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from glob import glob
  2. from json import load, dump
  3. from os import path, mkdir
  4. from time import time
  5. from uuid import uuid1
  6. from pycs import ApplicationStatus
  7. class ProjectManager:
  8. def __init__(self, app_status: ApplicationStatus):
  9. # TODO create projects folder if it does not exist
  10. # find projects
  11. for folder in glob('projects/*'):
  12. # load project.json
  13. with open(path.join(folder, 'project.json'), 'r') as file:
  14. project = load(file)
  15. project['status'] = 'close'
  16. app_status['projects'].append(project)
  17. # subscribe to changes
  18. app_status['projects'].subscribe(self.update)
  19. def update(self, data):
  20. # detect project to create
  21. for i in range(len(data)):
  22. if data[i]['status'] == 'create':
  23. # create dict representation
  24. uuid = str(uuid1())
  25. data[i] = {
  26. 'id': uuid,
  27. 'status': 'close',
  28. 'name': data[i]['name'],
  29. 'description': data[i]['description'],
  30. 'created': int(time()),
  31. 'access': 0,
  32. 'pipeline': {
  33. 'model-distribution': data[i]['model']
  34. }
  35. }
  36. # create project directory
  37. folder = path.join('projects', uuid)
  38. mkdir(folder)
  39. # create project.json
  40. with open(path.join(folder, 'project.json'), 'w') as file:
  41. dump(data[i], file, indent=4)
  42. # detect project to load
  43. to_load = list(filter(lambda x: x['status'] == 'load', data))
  44. for project in to_load:
  45. # TODO actually load pipeline
  46. project['status'] = 'open'
  47. project['access'] = time()