Commit c2e00d1a authored by Lukas Jelonek's avatar Lukas Jelonek
Browse files

Refactor: remove print statement from retriever. Remove location variable

parent 87c46362
......@@ -3,6 +3,7 @@ import argparse
import os
import logging
from dbxref import resolver
import json
def main():
parser = argparse.ArgumentParser(description='Lookup locations of database cross references and retrieve them as json')
......@@ -39,12 +40,11 @@ def info(args, config):
print ('info')
def resolve(args, config):
import json
print(json.dumps(resolver.resolve(resolver.convert_to_dbxrefs(args.dbxrefs), check_existence=args.no_check)))
def retrieve(args, config):
from dbxref import retriever
retriever.retrieve(resolver.convert_to_dbxrefs(args.dbxrefs))
print(json.dumps(retriever.retrieve(resolver.convert_to_dbxrefs(args.dbxrefs))))
if __name__ == "__main__":
main()
......@@ -7,7 +7,7 @@ import json
providers = load_providers()
def retrieve(dbxrefs, location=''):
def retrieve(dbxrefs):
sorted(dbxrefs, key=lambda x: x['db'])
results = []
for key, dbxrefs in groupby(dbxrefs, lambda x: x['db']):
......@@ -15,32 +15,30 @@ def retrieve(dbxrefs, location=''):
provider = providers[key.lower()]
logger.debug('{0} is supported'.format(key))
if provider['retriever']['type'] == 'external':
retrieved = load_with_external_provider(provider, list(dbxrefs), location)
results.extend(retrieved)
results.extend( load_with_external_provider(provider, list(dbxrefs)))
elif provider['retriever']['type'] == 'internal':
import importlib
retrieve_method = getattr(importlib.import_module(provider['retriever']['location']), 'retrieve')
retrieved = retrieve_method(dbxrefs)
results.extend(retrieved)
results.extend(load_with_internal_provider(provider, list(dbxrefs)))
else:
raise Exception('Unknown retriever type', provider['retriever']['type'])
else:
logger.debug('{0} is not supported'.format(key))
results.extend( map(lambda x: {'id': toString(x), 'status': 'not supported'}, dbxrefs))
if not location == '':
return (results)
else:
print(json.dumps(results, indent=4))
return (results)
def load_with_external_provider(provider, dbxrefs, location):
def load_with_external_provider(provider, dbxrefs):
logger.debug('Loading {0} via external provider'.format(dbxrefs))
script = provider['retriever']['location']
call = location + '{} {}'.format(script, ' '.join(list(map(toString, dbxrefs))))
call = '{} {}'.format(script, ' '.join(list(map(toString, dbxrefs))))
logger.debug("Running '{}'".format(call))
import subprocess
result = subprocess.check_output(call, shell=True)
return json.loads(result.decode('utf-8'))
def load_with_internal_provider(provider, dbxrefs):
import importlib
retrieve_method = getattr(importlib.import_module(provider['retriever']['location']), 'retrieve')
retrieved = retrieve_method(dbxrefs)
return retrieved
def toString(dbxref):
return '{}:{}'.format(dbxref['db'], dbxref['id'])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment