output = re.sub(r'(^|[^%])((?:%%)*)%p', r'\1\2foo', output).replace('%%', '%') elif arg == '-p': if not args or args[0] == '--': print('no id specified', file=sys.stderr) usage() pid = args.pop(0) elif arg == '-l': if not args or args[0] == '--': print('no file specified', file=sys.stderr) usage() lineFile = args.pop(0) elif arg == '-c': if not args or args[0] == '--': print('no file specified', file=sys.stderr) usage() classFile = args.pop(0) else: print('unrecognized argument: ' + arg, file=sys.stderr) usage() if pid is None: print('need to specify id with -p', file=sys.stderr) usage() allPaths = [] util.convert_paths(args, allPaths) obj = process(allPaths, lineFile, classFile, pid) if output != '-' and not os.path.exists(os.path.dirname(output)): os.makedirs(os.path.dirname(output)) with util.OutWrapper(output) as out: print(json.dumps(obj, indent=2, sort_keys=True), file=out)
def start_server(max_num, settings_file, format_file, class_file, line_file, cms_path, addr, port, debug): settings = {} util.read_config(settings, settings_file, True) use_cache = settings.get('use_cache', True) all_paths = [] input_format = {} use_db = False omop = None if settings.get('omop_use_db', False): use_db = True omop = OMOP(settings, True) else: util.convert_paths([ cms_path ], all_paths) util.read_format(format_file, input_format, usage) cms_analyze.input_format = input_format cms_get_patient.input_format = input_format build_dictionary.debugOutput = True build_dictionary.init(settings, settings_file) dictionary_file = os.path.join(json_dir, dictionary_bind) patients = set() def save_patients(): if not use_cache: return with open(patients_list, 'w') as pf: pf.write('\n'.join(sorted(list(patients)))) pf.flush() if not os.path.isfile(patients_list) or not use_cache: if use_db: omop.list_patients(patients, prefix=json_dir, limit=max_num, show_old_ids=True) else: tf = StringIO() cms_analyze.compute(all_paths, {}, False, tf, filter_zero=True) tf.flush() tf.seek(0) lines = tf.readlines()[-max_num:] if max_num is not None else tf.readlines() for line in lines: patients.add(json_dir + line.strip() + '.json') save_patients() dict = {} if use_cache: if os.path.isfile(dictionary_file): with open(dictionary_file, 'r') as input: dict = json.loads(input.read()) else: os.makedirs(json_dir) # write the initial empty dictionary # also ensures that the folder is writeable with open(dictionary_file, 'w') as output: output.write("{}") server = create_server((addr, port)) server.bind_path('/', '..') prefix = '/' + os.path.basename(os.path.normpath(server.base_path)) server.add_default_white_list() server.add_file_patterns([ prefix + '/' + json_dir + '*', prefix + '/' + patients_list ], True) server.favicon_fallback = 'favicon.ico' server.report_slow_requests = True if debug: server.suppress_noise = True @server.text_get(prefix + '/' + patients_list, 0) def get_list(req, args): return '\n'.join(sorted(list(patients))) @server.json_get(prefix + '/' + json_dir, 1) def get_patient(req, args): pid = args['paths'][0] if pid.endswith('.json') and use_db: pid = pid[:-len('.json')] pid = omop.get_person_id(pid) cache_file = os.path.join(json_dir, pid) p_name = json_dir + pid.strip() if p_name not in patients: patients.add(p_name) save_patients() if pid.endswith('.json'): pid = pid[:-len('.json')] if not os.path.isfile(cache_file) or not use_cache: if use_db: patient = omop.get_patient(pid, dict, line_file, class_file) else: patient = cms_get_patient.process(all_paths, line_file, class_file, pid) build_dictionary.extractEntries(dict, patient) if use_cache: with open(cache_file, 'w') as pf: pf.write(json_dumps(patient)) pf.flush() if use_cache: with open(dictionary_file, 'w') as output: output.write(json_dumps(dict)) output.flush() return patient with open(cache_file, 'r') as pf: return json.loads(pf.read()) @server.json_get(prefix + '/' + dictionary_file) def get_dictionary(req, args): return dict msg("starting server at {0}:{1}", addr if addr else 'localhost', port) server.serve_forever() msg("shutting down..") server.server_close()
output = args.pop(0) output = re.sub(r'(^|[^%])((?:%%)*)%p', r'\1\2foo', output).replace('%%', '%') elif arg == '-p': if not args or args[0] == '--': print('no id specified', file=sys.stderr) usage() id = args.pop(0) elif arg == '-l': if not args or args[0] == '--': print('no file specified', file=sys.stderr) usage() lineFile = args.pop(0) elif arg == '-c': if not args or args[0] == '--': print('no file specified', file=sys.stderr) usage() classFile = args.pop(0) else: print('unrecognized argument: ' + arg, file=sys.stderr) usage() if id is None: print('need to specify id with -p', file=sys.stderr) usage() allPaths = [] util.convert_paths(args, allPaths) obj = process(allPaths, lineFile, classFile, id) if output != '-' and not os.path.exists(os.path.dirname(output)): os.makedirs(os.path.dirname(output)) with util.OutWrapper(output) as out: print(json.dumps(obj, indent=2, sort_keys=True), file=out)