Exemplo n.º 1
0
def detect_namespace(rdf):
    """Try to automatically detect the URI namespace of the vocabulary.

    Return namespace as URIRef.

    """

    # pick a concept
    conc = rdf.value(None, RDF.type, SKOS.Concept, any=True)
    if conc is None:
        logging.critical(
            "Namespace auto-detection failed. "
            "Set namespace using the --namespace option.")
        sys.exit(1)

    ln = localname(conc)
    ns = URIRef(conc.replace(ln, ''))
    if ns.strip() == '':
        logging.critical(
            "Namespace auto-detection failed. "
            "Set namespace using the --namespace option.")
        sys.exit(1)

    logging.info(
        "Namespace auto-detected to '%s' "
        "- you can override this with the --namespace option.", ns)
    return ns
Exemplo n.º 2
0
def detect_namespace(rdf):
    """Try to automatically detect the URI namespace of the vocabulary.

    Return namespace as URIRef.

    """

    # pick a concept
    conc = rdf.value(None, RDF.type, SKOS.Concept, any=True)
    if conc is None:
        logging.critical("Namespace auto-detection failed. "
                         "Set namespace using the --namespace option.")
        sys.exit(1)

    ln = localname(conc)
    ns = URIRef(conc.replace(ln, ''))
    if ns.strip() == '':
        logging.critical("Namespace auto-detection failed. "
                         "Set namespace using the --namespace option.")
        sys.exit(1)

    logging.info(
        "Namespace auto-detected to '%s' "
        "- you can override this with the --namespace option.", ns)
    return ns
    def detect_namespace(self):
        """
        Attempts to extract a base namespace from the vocabulary graph.

        Will first find a random concept or concept scheme and then extract the base name space from it.

        :raises NoNamespaceDetectedError: If no namespace can be found.
        """
        concept = self.rdf.value(None, RDF.type, SKOS.Concept, any=True)
        if concept is None:
            concept = self.rdf.value(None,
                                     RDF.type,
                                     SKOS.ConceptScheme,
                                     any=True)
            if concept is None:
                raise NoNamespaceDetectedError(
                    'Could not detect a namespace for ' + self.name +
                    ', because there are no SKOS Concepts or SKOS Concept Schemes defined.'
                )

        local_name = concept.split('/')[-1].split('#')[-1]
        namespace = URIRef(concept.replace(local_name, '').strip('#'))
        if namespace.strip() == '':
            raise NoNamespaceDetectedError(
                'Could not detect a namespace for ' + self.name +
                ', because the URI is not valid.')

        self.logger.info('Namespace detection successful: %s.', namespace)
        self.namespace = namespace