Exemplo n.º 1
0
    def route_dynamic(path):
        args = dict(request.args)
        if 'direction' in args:
            direction = args.pop('direction')
        else:
            direction = 'OUTGOING'  # should always be outgoing here since we can't specify?

        if 'format' in args:
            format_ = args.pop('format')
        else:
            format_ = None

        try:
            j = sgd.dispatch(path, **args)
        except rHTTPError as e:
            log.exception(e)
            abort(e.response.status_code)  # DO NOT PASS ALONG THE MESSAGE
        except ValueError as e:
            log.exception(e)
            abort(404)

        if j is None or 'edges' not in j or not j['edges']:
            log.error(pformat(j))
            log.debug(sgd._last_url)
            return abort(400)

        prov = [
            hfn.titletag(f'Dynamic query result for {path}'),
            f'<meta name="date" content="{UTCNOWISO()}">',
            f'<link rel="http://www.w3.org/ns/prov#wasGeneratedBy" href="{wgb}">',
            '<meta name="representation" content="SciGraph">',
            f'<link rel="http://www.w3.org/ns/prov#wasDerivedFrom" href="{sgd._last_url}">'
        ]

        kwargs = {'json': cleanBad(j), 'html_head': prov}
        tree, extras = creatTree(*Query(None, None, direction, None), **kwargs)
        #print(extras.hierarhcy)
        #print(tree)
        if format_ is not None:
            if format_ == 'table':
                #breakpoint()
                def nowrap(class_, tag=''):
                    return (f'{tag}.{class_}' '{ white-space: nowrap; }')

                ots = [
                    OntTerm(n) for n in flatten_tree(extras.hierarchy)
                    if 'CYCLE' not in n
                ]
                #rows = [[ot.label, ot.asId().atag(), ot.definition] for ot in ots]
                rows = [[ot.label,
                         hfn.atag(ot.iri, ot.curie), ot.definition]
                        for ot in ots]

                return hfn.htmldoc(hfn.render_table(rows, 'label', 'curie',
                                                    'definition'),
                                   styles=(hfn.table_style,
                                           nowrap('col-label', 'td')))

        return hfn.htmldoc(extras.html, other=prov, styles=hfn.tree_styles)
Exemplo n.º 2
0
def makeProv(pred, root, wgb):
    return [
        hfn.titletag(f'Transitive closure of {root} under {pred}'),
        f'<meta name="date" content="{UTCNOWISO()}">',
        f'<link rel="http://www.w3.org/ns/prov#wasGeneratedBy" href="{wgb}">'
    ]
Exemplo n.º 3
0
def sparc_dynamic(data_sgd, data_sgc, path, wgb, process=lambda coll, blob: blob):
    args = dict(request.args)
    if 'direction' in args:
        direction = args.pop('direction')
    else:
        direction = 'OUTGOING'  # should always be outgoing here since we can't specify?

    if 'format' in args:
        format_ = args.pop('format')
    else:
        format_ = None

    if 'apinat' in path:  # FIXME bad hardcoded hack
        _old_get = data_sgd._get
        try:
            data_sgd._get = data_sgd._normal_get
            j = data_sgd.dispatch(path, **args)
        except ValueError as e:
            log.exception(e)
            abort(404)
        except rHTTPError as e:
            log.exception(e)
            abort(e.response.status_code)  # DO NOT PASS ALONG THE MESSAGE
        finally:
            data_sgd._get = _old_get
    else:
        try:
            j = data_sgd.dispatch(path, **args)
        except rHTTPError as e:
            log.exception(e)
            abort(e.response.status_code)  # DO NOT PASS ALONG THE MESSAGE
        except ValueError as e:
            log.exception(e)
            abort(404)

    j = process(collapse_apinat, j)

    if j is None or 'edges' not in j:
        log.error(pformat(j))
        return abort(400)

    elif not j['edges']:
        return node_list(j['nodes'])  # FIXME ... really should error?

    if path.endswith('housing-lyphs'):  # FIXME hack
        root = 'NLX:154731'
        #direction = 'INCOMING'
    else:
        root = None

    prov = [
        hfn.titletag(f'Dynamic query result for {path}'),
        f'<meta name="date" content="{UTCNOWISO()}">',
        f'<link rel="http://www.w3.org/ns/prov#wasGeneratedBy" href="{wgb}">',
        '<meta name="representation" content="SciGraph">',
        ('<link rel="http://www.w3.org/ns/prov#wasDerivedFrom" '
         f'href="{data_sgd._last_url}">')]

    kwargs = {'json': cleanBad(j),
                'html_head': prov,
                'prefixes': data_sgc.getCuries(),  # FIXME efficiency
    }
    tree, extras = creatTree(*Query(root, None, direction, None), **kwargs)
    #print(extras.hierarhcy)
    #print(tree)
    if format_ is not None:
        if format_ == 'table':
            #breakpoint()
            def nowrap(class_, tag=''):
                return (f'{tag}.{class_}'
                        '{ white-space: nowrap; }')

            ots = [OntTerm(n)
                   for n in flatten_tree(extras.hierarchy)
                   if 'CYCLE' not in n]
            #rows = [[ot.label, ot.asId().atag(), ot.definition] for ot in ots]
            rows = [[ot.label, hfn.atag(ot.iri, ot.curie), ot.definition]
                    for ot in ots]

            return hfn.htmldoc(hfn.render_table(rows, 'label', 'curie', 'definition'),
                               styles=(hfn.table_style, nowrap('col-label', 'td')))

    return hfn.htmldoc(extras.html,
                       other=prov,
                       styles=hfn.tree_styles)