retriever.py 1.77 KB
Newer Older
1
2
3
4
5
6
7
8
9
import logging
logger = logging.getLogger(__name__)

from dbxref.config import load_providers
from itertools import groupby
import json

providers = load_providers()

10
def retrieve(dbxrefs):
11
12
13
    sorted(dbxrefs, key=lambda x: x['db'])
    results = []
    for key, dbxrefs in groupby(dbxrefs, lambda x: x['db']):
14
15
        if key.lower() in providers and 'retriever' in providers[key.lower()]:
            provider = providers[key.lower()]
16
17
            logger.debug('{0} is supported'.format(key))
            if provider['retriever']['type'] == 'external':
18
                results.extend( load_with_external_provider(provider, list(dbxrefs)))
19
            elif provider['retriever']['type'] == 'internal':
20
                results.extend(load_with_internal_provider(provider, list(dbxrefs)))
21
22
23
24
            else:
                raise Exception('Unknown retriever type', provider['retriever']['type'])
        else:
            logger.debug('{0} is not supported'.format(key))
25
            results.extend( map(lambda x: {'id': toString(x), 'status': 'not supported'}, dbxrefs))
26
    return (results)
27

28
def load_with_external_provider(provider, dbxrefs):
29
30
    logger.debug('Loading {0} via external provider'.format(dbxrefs))
    script = provider['retriever']['location']
31
    call = '{} {}'.format(script, ' '.join(list(map(toString, dbxrefs))))
32
33
34
35
36
    logger.debug("Running '{}'".format(call))
    import subprocess
    result = subprocess.check_output(call, shell=True)
    return json.loads(result.decode('utf-8'))

37
38
39
40
41
42
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

43
44
def toString(dbxref):
    return '{}:{}'.format(dbxref['db'], dbxref['id'])