from glob import glob from json import load, dump from os import path, mkdir from shutil import rmtree from time import time from uuid import uuid1 from pycs import ApplicationStatus class ProjectManager: def __init__(self, app_status: ApplicationStatus): # TODO create projects folder if it does not exist # find projects for folder in glob('projects/*'): # load project.json with open(path.join(folder, 'project.json'), 'r') as file: project = load(file) project['status'] = 'close' app_status['projects'].append(project) # subscribe to changes app_status['projects'].subscribe(self.update) def update(self, data): # detect project to create for i in range(len(data)): if data[i]['status'] == 'create': # create dict representation uuid = str(uuid1()) data[i] = { 'id': uuid, 'status': 'close', 'name': data[i]['name'], 'description': data[i]['description'], 'created': int(time()), 'access': 0, 'pipeline': { 'model-distribution': data[i]['model'] } } # create project directory folder = path.join('projects', uuid) mkdir(folder) # create project.json with open(path.join(folder, 'project.json'), 'w') as file: dump(data[i], file, indent=4) # detect project to load to_load = list(filter(lambda x: x['status'] == 'load', data)) for project in to_load: # TODO actually load pipeline project['status'] = 'open' project['access'] = int(time()) # detect project to update for i in range(len(data)): if 'action' in data[i] and data[i]['action'] == 'update': del data[i]['action'] prj = data[i].copy() del prj['status'] with open(path.join('projects', data[i]['id'], 'project.json'), 'w') as file: dump(prj, file, indent=4) # detect project to delete for i in range(len(data)): if 'action' in data[i] and data[i]['action'] == 'delete': folder = path.join('projects', data[i]['id']) del data[i] rmtree(folder) break