Ejemplo n.º 1
0
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()
Ejemplo n.º 2
0
 args = sys.argv[:]
 args.pop(0)
 while args:
     arg = args.pop(0)
     if arg == '-h':
         usage()
     if arg == '--path':
         if not args:
             print('--path requires path', file=sys.stderr)
             usage()
         path = args.pop(0)
     elif arg == '-f':
         if not args:
             print('-f requires format file', file=sys.stderr)
             usage()
         util.read_format(args.pop(0), input_format, usage)
     elif arg == '-c':
         if not args:
             print('-c requires argument', file=sys.stderr)
             usage()
         util.read_config(settings, args.pop(0))
     else:
         print('unrecognized argument: ' + arg, file=sys.stderr)
         usage()
 if path is None:
     print('need to specify path with --path', file=sys.stderr)
     usage()
 if not len(input_format.keys()):
     print('need to specify non-empty format file with -f', file=sys.stderr)
     usage()
 out = {
Ejemplo n.º 3
0
 classFile = None
 pid = None
 output = '-'
 args = sys.argv[:]
 args.pop(0)
 while args:
     arg = args.pop(0)
     if arg == '--':
         break
     if arg == '-h':
         usage()
     if arg == '-f':
         if not args or args[0] == '--':
             print('-f requires format file', file=sys.stderr)
             usage()
         util.read_format(args.pop(0), input_format, usage)
     elif arg == '-o':
         if not args or args[0] == '--':
             print('-o requires output file', file=sys.stderr)
             usage()
         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()
         pid = args.pop(0)
     elif arg == '-l':
         if not args or args[0] == '--':
             print('no file specified', file=sys.stderr)
Ejemplo n.º 4
0
         usage()
     query = args.pop(0)
 elif arg == '--query-file':
     if not args or args[0] == '--':
         print('--query-file requires file', file=sys.stderr)
         usage()
     if len(query):
         print('only one query allowed', file=sys.stderr)
         usage()
     with open(args.pop(0), 'r') as qf:
         query = qf.read()
 elif arg == '-f':
     if not args or args[0] == '--':
         print('-f requires format file', file=sys.stderr)
         usage()
     util.read_format(args.pop(0), cms_get_patient.input_format, usage)
 elif arg == '-o':
     if not args or args[0] == '--':
         print('-o requires output file', file=sys.stderr)
         usage()
     output = args.pop(0)
 elif arg == '-c':
     if not args or args[0] == '--':
         print('-c requires argument', file=sys.stderr)
         usage()
     settingsFile = args.pop(0)
     util.read_config(settings, settingsFile, build_dictionary.debugOutput)
 elif arg == '--debug':
     build_dictionary.debugOutput = True
 else:
     print('unrecognized argument: ' + arg, file=sys.stderr)