def __init__(self, url_path): if not url_path or url_path == '/': file_path = get_setting('source', 'default_db') elif url_path in get_setting('source', 'db_map'): file_path = get_setting('source', 'db_map', url_path) else: raise RuntimeError( f'Cannot load database!\nMissing path in configuration: {url_path}' ) self.path = Path(file_path) files = list(self.path.glob('*_DBconfig.json')) if len(files) == 0: raise RuntimeError( f'Cannot load database!\nConfig file not found in path: {url_path}' ) if len(files) > 1: log.msg( f'Warning: more than one config in path! Using only first: {files[0]}', logLevel=logging.WARN) self.config_file = files[0] self.ssff_extensions = []
def get_source(path): if not get_setting('source', 'type'): return None source_type = get_setting('source', 'type') m = importlib.import_module(f'sources.{source_type}') c = getattr(m, source_type) return c(path)
def onMessage(self, payload, is_binary): if is_binary: self.sendMessage(self.get_error('Received binary data!')) return request = json.loads(payload) self.callbackID = request['callbackID'] if not self.db: self.sendMessage( self.get_error('Cannot find database: ' + self.path)) req_type = request['type'] if req_type == 'GETPROTOCOL': self.sendMessage(self.get_reply(self.data_protocol())) elif req_type == 'GETDOUSERMANAGEMENT': if self.db.do_login(): self.sendMessage(self.get_reply('YES')) else: self.sendMessage(self.get_reply('NO')) elif req_type == 'LOGONUSER': user = request['userName'] password = request['pwd'] if not self.db.check_user(user): self.sendMessage(self.get_reply('BADUSERNAME')) elif not self.db.check_login(user, password): self.sendMessage(self.get_reply('BADPASSWORD')) else: self.sendMessage(self.get_reply('LOGGEDON')) elif req_type == 'GETGLOBALDBCONFIG': self.sendMessage(self.get_reply(self.db.get_config())) elif req_type == 'GETBUNDLELIST': self.sendMessage(self.get_reply(self.db.get_bundle_list())) elif req_type == 'GETBUNDLE': session = request['session'] bundle = request['name'] self.sendMessage( self.get_reply(self.db.get_bundle(session, bundle))) elif req_type == 'DISCONNECTWARNING': self.sendMessage(self.get_reply(None)) elif req_type == 'SAVEBUNDLE': if not get_setting('readonly'): session = request['data']['session'] bundle = request['data']['annotation']['name'] data = request['data'] self.db.save_bundle(session, bundle, data) self.sendMessage(self.get_reply(None)) elif req_type == 'SAVECONFIG': if not get_setting('readonly'): self.db.save_config(request['data']) self.sendMessage(self.get_reply(None)) else: log.msg('NYI ' + req_type) self.sendMessage(self.get_error('NYI: {}'.format(req_type)))
def save_bundle(self, session, bundle, data): bndl = self.proj['bundles'][f'{session}_{bundle}'] work_dir = get_setting('source', 'work_dir') fd, new_path = mkstemp(dir=work_dir) close(fd) new_path = Path(work_dir) / new_path self.save_file(new_path, annot_to_ctm(data['annotation'])) hash = file_hash(new_path) file = db.clarin.resources.find_one({'hash': hash}) if file: id = file['_id'] new_path.unlink() else: time = datetime.datetime.utcnow() from_task = {'task': 'emu-smds', 'seg': bndl['seg']} from_hash = hashlib.sha1(json.dumps(from_task).encode('utf-8')).hexdigest() res = db.clarin.resources.insert_one( {'file': new_path.name, 'type': 'segmentation', 'hash': hash, 'created': time, 'modified': time, 'from': from_task, 'from_hash': from_hash}) id = res.inserted_id db.clarin.emu.update_one({'_id': ObjectId(self.proj['_id'])}, {'$set': {f'bundles.{bundle}.seg': id}})
def __init__(self, proj_id): self.proj = db.clarin.emu.find_one({'_id': ObjectId(proj_id[1:])}) if not self.proj: raise RuntimeError self.work_dir = Path(get_setting('source', 'work_dir')) self.password = None if self.proj['visibility'] == 'private': self.password = self.proj['password']
def run_server(): from twisted.python import log from twisted.internet import reactor, ssl from autobahn.twisted.websocket import WebSocketServerFactory from EmuServer import EmuServerProtocol if get_setting('logFile'): f = open(get_setting('logFile'), 'a') log.startLogging(f) else: log.startLogging(sys.stdout) factory = WebSocketServerFactory() factory.protocol = EmuServerProtocol if get_setting('secure'): contextFactory = ssl.DefaultOpenSSLContextFactory( get_setting('tls', 'private'), get_setting('tls', 'cert')) reactor.listenSSL(get_setting('port'), factory, contextFactory) else: reactor.listenTCP(get_setting('port'), factory) reactor.run()
def save_bundle(self, session, bundle, data): if get_setting('source', 'readonly'): return bundle_path = self.path / f'{session}_ses' / f'{bundle}_bndl' for f in data['ssffFiles']: ext = f['fileExtension'] fp = bundle_path / f'{bundle}.{ext}' assert f['encoding'] == 'BASE64', 'Only BASE64 encoding supported!' self.save_file(fp, f['data'], base64_dec=True) if 'mediaFile' in data and len(data['mediaFile']['data']) > 0: fp = bundle_path / f'{bundle}.wav' assert data['mediaFile'][ 'encoding'] == 'BASE64', 'Only BASE64 encoding supported!' self.save_file(fp, data['mediaFile']['data'], base64_dec=True) annotation = data['annotation'] with open(bundle_path / f'{bundle}_annot.json', 'w') as f: json.dump(annotation, f)
def save_config(self, data): if get_setting('source', 'readonly'): return with open(self.config_file, 'w') as f: json.dump(data, f)
def check_login(self, user, passwd): return user == get_setting('source', 'user') and passwd == get_setting( 'source', 'pass')
def do_login(self): return get_setting('source', 'authorize')
factory.protocol = EmuServerProtocol if get_setting('secure'): contextFactory = ssl.DefaultOpenSSLContextFactory( get_setting('tls', 'private'), get_setting('tls', 'cert')) reactor.listenSSL(get_setting('port'), factory, contextFactory) else: reactor.listenTCP(get_setting('port'), factory) reactor.run() if __name__ == '__main__': parser = argparse.ArgumentParser(description='EMU-webApp server program') parser.add_argument('settings', help='Settings file', nargs='?') args = parser.parse_args() if args.settings: load_settings(args.settings) if get_setting('daemonize'): d = daemonize.Daemonize(app=os.path.basename(sys.argv[0]), pid=get_setting('pid'), action=run_server) d.start() else: run_server()