Ejemplo n.º 1
0
    def _get_curie_prefix(uself, uri, existing_curies):
        ns_count = 0

        from curies import CURIES

        # TODO: replace this with a once-per run update CURIES function
        def get_curie_online(uri):
            import requests
            try:
                r = requests.get("http://prefix.cc/reverse",
                                 params={
                                     "uri": uri,
                                     "format": "txt"
                                 })
                if r.status_code == 200:
                    # primitive check to see if it really is prefix.cc replying with a text/plain response
                    if r.headers["Content-Type"] == "text/plain":
                        return r.text.split("\t")[0]
                    else:
                        return None
                else:
                    return None
            except requests.exceptions.ConnectionError:
                # presumably this module can't access the internet or prefix.cc is down
                return None

        def get_curie_from_namespace(uri, existing_curies, ns_count):
            # strip off trailing hash or slash and return last path segment
            c = uri.rstrip("#/").split("/")[-1]

            # prevent CURIE collision = return nsX (X int) if we already have this one
            if c in existing_curies:
                ns_count += 1
                return "ns" + str(ns_count)

            return c

        # attempt to look up the well-known curie for this Namespace in http://prefix.cc dump
        for k, v in CURIES.items():
            if v == uri:
                return k

        # attempt to look up the well-known CURIE for this Namespace using http://prefix.cc online (more up-to-date)
        c = get_curie_online(uri)
        if c is not None:
            return c

        # can't find CURIE online so make up one
        c = get_curie_from_namespace(uri, existing_curies, ns_count)
        return c if c is not None else ""
Ejemplo n.º 2
0
def _get_curie_prefix(uri, ns):
    ns_count = 0

    from curies import CURIES, EXTRA_CURIES

    # TODO: replace this with a once-per run update CURIES function
    def get_curie_online(uri):
        try:
            r = requests.get('http://prefix.cc/reverse',
                             params={
                                 'uri': uri,
                                 'format': 'txt'
                             })
            if r.status_code == 200:
                return r.text.split('\t')[0]
            else:
                return None
        except requests.exceptions.ConnectionError:
            # presumably this module can't access the internet or prefix.cc is down
            return None

    def get_curie_from_namespace(uri, ns_count):
        # strip off trailing hash or slash and return last path segment
        c = uri.rstrip('#/').split('/')[-1]

        # prevent CURIE collision = return nsX (x int) if we already have this one
        for k, v in ns.items():
            if c == v:
                ns_count += 1
                return 'ns' + str(ns_count)

        return c

    # attempt to look up the well-known curie for this Namespace in http://prefix.cc dump
    for k, v in CURIES.items():
        if v == uri:
            return k

    for k, v in EXTRA_CURIES.items():
        if v == uri:
            return k

    # attempt to look up the well-known CURIE for this Namespace using http://prefix.cc online (more up-to-date)
    c = get_curie_online(uri)
    if c is not None:
        return c

    # can't fund CURIE online so make up one
    c = get_curie_from_namespace(uri, ns_count)
    return c if c is not None else ''