ProjectManager.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. class ProjectManager:
  9. def __init__(self, app_status: ApplicationStatus):
  10. # TODO create projects folder if it does not exist
  11. # find projects
  12. for folder in glob('projects/*'):
  13. # load project.json
  14. with open(path.join(folder, 'project.json'), 'r') as file:
  15. project = load(file)
  16. project['status'] = 'close'
  17. app_status['projects'].append(project)
  18. # subscribe to changes
  19. app_status['projects'].subscribe(self.update)
  20. def update(self, data):
  21. # detect project to create
  22. for i in range(len(data)):
  23. if data[i]['status'] == 'create':
  24. # create dict representation
  25. uuid = str(uuid1())
  26. data[i] = {
  27. 'id': uuid,
  28. 'status': 'close',
  29. 'name': data[i]['name'],
  30. 'description': data[i]['description'],
  31. 'created': int(time()),
  32. 'access': 0,
  33. 'pipeline': {
  34. 'model-distribution': data[i]['model']
  35. }
  36. }
  37. # create project directory
  38. folder = path.join('projects', uuid)
  39. mkdir(folder)
  40. # create project.json
  41. with open(path.join(folder, 'project.json'), 'w') as file:
  42. dump(data[i], file, indent=4)
  43. # detect project to load
  44. to_load = list(filter(lambda x: x['status'] == 'load', data))
  45. for project in to_load:
  46. # TODO actually load pipeline
  47. project['status'] = 'open'
  48. project['access'] = int(time())
  49. # detect project to update
  50. for i in range(len(data)):
  51. if 'action' in data[i] and data[i]['action'] == 'update':
  52. del data[i]['action']
  53. prj = data[i].copy()
  54. del prj['status']
  55. with open(path.join('projects', data[i]['id'], 'project.json'), 'w') as file:
  56. dump(prj, file, indent=4)
  57. # detect project to delete
  58. for i in range(len(data)):
  59. if 'action' in data[i] and data[i]['action'] == 'delete':
  60. folder = path.join('projects', data[i]['id'])
  61. del data[i]
  62. rmtree(folder)
  63. break