示例#1
0
def tobn(gen, published):
    """convert hypothesis ids to blank nodes so that values serialize locally"""
    for s, p, o in gen:
        ns = fix(s)
        no = fix(o)
        if p == TEMP.protcurChildren:
            yield ns, p, no
        elif s != ns:
            yield ns, p, o
            yield ns, ilxtr.hasId, s
            yield ns, TEMP.hasUriHumanContext, rdflib.URIRef(s.replace(ph_prefix, 'https://hyp.is/'))
        else:
            yield s, p, o

        if o == sparc.Protocol:
            try:
                pid = idlib.Pio(s)
                os = pio_onts[pid.identifier.suffix]
                yield os, rdf.type, owl.Ontology
                yield os, TEMP.hasUriApi, s
                for _s in (s, os):
                    yield _s, TEMP.hasUriHuman, pid.uri_human.asType(rdflib.URIRef)
                    doi = pid.doi
                    if doi is not None:
                        yield _s, TEMP.hasDoi, pid.doi.asType(rdflib.URIRef)
                    if s in published:
                        yield _s, TEMP.datasetPublishedDoi, published[s]
            except (idlib.exc.NotAuthorizedError) as e:
                tn = GetTimeNow()
                yield s, errorns.NotAuthorized, rdflib.Literal(tn._start_time_local)
            except (idlib.exc.IdDoesNotExistError) as e:
                tn = GetTimeNow()
                yield s, errorns.IdDoesNotExist, rdflib.Literal(tn._start_time_local)
            except (idlib.exc.MalformedIdentifierError) as e:
                pass
示例#2
0
        def protocol_url_or_doi(self, value):
            #_, s = self.c.protocol_url_or_doi(value)
            #yield s, rdf.type, owl.NamedIndividual
            #yield s, rdf.type, sparc.Protocol
            log.debug(value)
            if not isinstance(value, idlib.Pio):
                if isinstance(value, idlib.Doi):
                    try:
                        t = None
                        for t in value.triples_gen:
                            yield t
                    except idlib.exc.RemoteError as e:
                        if t is None:
                            # we already logged this error during id dereferencing
                            return

                    ds, _, _ = t
                    try:
                        pioid = value.dereference(asType=idlib.Pio)
                        s = self.c.l(pioid)
                        yield ds, TEMP.dereferencesTo, s
                        yield s, TEMP.hasDoi, ds
                    except idlib.exc.MalformedIdentifierError as e:
                        log.warning(e)
                        return
                else:
                    try:
                        pioid = idlib.Pio(
                            value
                        )  # FIXME :/ should be handled in Pio directly probably?
                    except idlib.exc.MalformedIdentifierError as e:
                        logd.warning(e)
                        return
            else:
                pioid = value

            try:
                pioid_int = pioid.uri_api_int
                s = self.c.l(pioid_int)
                yield from pioid_int.triples_gen
                # FIXME needs to be a pipeline so that we can export errors
                try:
                    data = pioid.data()
                except (OntId.BadCurieError,
                        idlib.exc.MalformedIdentifierError) as e:
                    loge.error(e)  # FIXME export errors ...
                    data = None
            except idlib.exc.RemoteError as e:  # FIXME sandbox violation
                loge.exception(e)
                s = self.c.l(pioid)
                data = None

            yield s, rdf.type, sparc.Protocol

            if data:
                yield s, rdfs.label, rdflib.Literal(pioid.label)
                nsteps = len(data['steps'])
                yield s, TEMP.protocolHasNumberOfSteps, rdflib.Literal(nsteps)
示例#3
0
    def _protocol_url_or_doi(self, value):
        doi = False
        if 'doi' in value:
            doi = True
        elif value.startswith('10.'):
            value = 'doi:' + value
            doi = True

        if doi:
            value = idlib.Doi(value)
        else:
            value = idlib.Pio(value)

        return value
示例#4
0
    def _protocol_url_or_doi(self, value):
        doi = False
        if 'doi' in value:
            doi = True
        elif value.startswith('10.'):
            value = 'doi:' + value
            doi = True

        if doi:
            value = idlib.Doi(value)  # XXX possible encapsulation issue
        else:
            value = idlib.Pio(value)  # XXX possible encapsulation issue

        return value
示例#5
0
        def mkval(cell):
            hl = cell.hyperlink
            cv = cell.value
            if hl is None:
                hl = cv if cv else None

            if hl is not None:
                try:
                    return idlib.Pio(hl)
                except idlib.exc.IdlibError as e:
                    try:
                        return idlib.Doi(hl)
                    except idlib.exc.IdlibError as e:
                        pass

            logd.warning(f'unhandled value {cell.value}')
            return cv
示例#6
0
文件: from_oq.py 项目: tgbugs/idlib
    def __new__(cls, something):
        if '10.' in something:
            if 'http' in something and 'doi.org' not in something:
                pass  # probably a publisher uri that uses the handle
            else:
                return idlib.Doi(something)

        if 'orcid' in something:
            return idlib.Orcid(something)

        if '/ror.org/' in something or something.startswith('ror:'):
            return idlib.Ror(something)

        if 'protocols.io' in something:
            return idlib.Pio(something)

        return oq.OntId(something)
        return OntTerm(something)  # use the better local version of OntTerm
示例#7
0
def write_graphs(sgs, path=None):
    if path is None:
        path = Path(tempfile.tempdir) / 'protcur-individual'

    if not path.exists():
        path.mkdir()

    pp = path / 'published'
    if not pp.exists():
        pp.mkdir()

    hpath = path / 'html'
    if not hpath.exists():
        hpath.mkdir()

    hpp = hpath / 'published'
    if not hpp.exists():
        hpp.mkdir()

    opath = path / 'org'
    if not opath.exists():
        opath.mkdir()

    opp = opath / 'published'
    if not opp.exists():
        opp.mkdir()

    for wg in sgs:
        u = next(wg[:rdf.type:sparc.Protocol])
        published = bool(list(wg[u:TEMP.datasetPublishedDoi:]))
        try:
            pid = idlib.Pio(u)
            base = 'pio-' + pid.identifier.suffix
        except idlib.exc.IdlibError as e:
            pid = None
            base = (u
                    .replace('http://', '')
                    .replace('https://', '')
                    .replace('/', '_')
                    .replace('.', '_'))

        name = base + '.ttl'
        hname = base + '.html'
        oname = base + '.org'

        if published:
            wt_path = pp / name
            wh_path = hpp / hname
            wo_path = opp / oname
        else:
            wt_path = path / name
            wh_path = hpath / hname
            wo_path = opath / oname

        wg.write(wt_path)
        write_html(wg, wh_path)

        if pid is None:
            org = None
        else:
            #if wo_path.exists(): continue  # XXX remove after testing complete
            try:
                org = pid.asOrg()
            except idlib.exc.IdlibError as e:
                org = None

        if org is not None:
            with open(wo_path, 'wt') as f:
                f.write(org)