123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- from glob import glob
- from json import load, dump
- from os import path, mkdir
- from os.path import exists
- from shutil import rmtree, copytree
- from time import time
- from uuid import uuid1
- from pycs import ApplicationStatus
- from pycs.observable import ObservableDict
- from pycs.projects.Project import Project
- class ProjectManager(ObservableDict):
- def __init__(self, app_status: ApplicationStatus):
- self.app_status = app_status
-
- if not exists('projects/'):
- mkdir('projects/')
-
-
- super().__init__({}, app_status)
- app_status['projects'] = self
-
- for folder in glob('projects/*'):
-
- with open(path.join(folder, 'project.json'), 'r') as file:
- project = Project(load(file), self)
- self[project['id']] = project
- def write_project(self, uuid):
- copy = self[uuid].copy()
- del copy['jobs']
- del copy['model']
- with open(path.join('projects', uuid, 'project.json'), 'w') as file:
- dump(copy, file, indent=4)
- def create_project(self, name, description, model, label, unmanaged=None):
-
- uuid = str(uuid1())
-
- folder = path.join('projects', uuid)
- mkdir(folder)
-
- copytree(self.parent['models'][model]['path'], path.join(folder, 'model'))
-
- self[uuid] = Project({
- 'id': uuid,
- 'name': name,
- 'description': description,
- 'unmanaged': unmanaged,
- 'created': int(time()),
- 'data': {},
- 'labels': {},
- 'jobs': {}
- }, self)
-
- if label in self.parent['labels']:
- code_path = path.join(self.parent['labels'][label]['path'], self.parent['labels'][label]['code']['module'])
- module_name = code_path.replace('/', '.').replace('\\', '.')
- class_name = self.parent['labels'][label]['code']['class']
- mod = __import__(module_name, fromlist=[class_name])
- cl = getattr(mod, class_name)
- provider = cl(self.parent['labels'][label]['path'], self.parent['labels'][label])
- for label in provider.get_labels():
- self[uuid].add_label(label['name'], identifier=label['id'])
- provider.close()
-
- self.write_project(uuid)
- def update_project(self, uuid, update):
-
- if uuid not in self.keys():
- return
-
- self[uuid].update_properties(update)
- self.write_project(uuid)
- def delete_project(self, uuid):
-
- if uuid not in self.keys():
- return
-
- folder = path.join('projects', uuid)
- rmtree(folder)
-
- del self[uuid]
- def predict(self, uuid, identifiers=None, unlabeled=False):
-
- if uuid not in self.keys():
- return
- project = self[uuid]
-
- if identifiers is None:
- if project['unmanaged'] is None:
- identifiers = list(project['data'].keys())
- else:
- identifiers = project.unmanaged_files_keys
-
- project.predict(identifiers, unlabeled=unlabeled)
- def fit(self, uuid):
-
- if uuid not in self.keys():
- return
- project = self[uuid]
-
- project.fit()
|