def get_profiles():
	realprofiles=dict()
	realprofileindices=dict()
	annotationpool=[]
	out=open("../results/EvolutionaryProfileSizes.tsv",'w')
	register('text/rdf+n3', Parser, 'rdflib.plugins.parsers.notation3', 'N3Parser')
	g = rdflib.Graph()
	result = g.parse('../data/realprofilesold.ttl', format='n3')
	index=0
	for stmt in g:
		profileid=stmt[0]
		annotation=stmt[2]
		if "VTO" in profileid:
			profileid=profileid.replace("http://purl.obolibrary.org/obo/","").replace("#profile","")
			if profileid not in realprofiles:
				realprofiles[profileid]=set()
			realprofiles[profileid].add(annotation)
			annotationpool.append(annotation)

			if profileid not in realprofileindices:
				realprofileindices[profileid]=set()	
			realprofileindices[profileid].add(index)
			index+=1	
	print "Created real profiles"
	for profileid in realprofiles:
		out.write(profileid+"\t"+str(len(realprofiles[profileid]))+"\n")
	return realprofiles,annotationpool,realprofileindices
 def __init__(self, gc):
     '''
     Constructor
     '''
     self.__globalContext = gc
     plugin.register("sparql", query.Processor, "rdfextras.sparql.processor", "Processor")
     plugin.register("sparql", query.Result, "rdfextras.sparql.query", "SPARQLQueryResult")
Example #3
0
def registerplugins():
    """
    Register plugins.

    If setuptools is used to install rdflib-sqlalchemy, all the provided
    plugins are registered through entry_points. This is strongly recommended.

    However, if only distutils is available, then the plugins must be
    registed manually.

    This method will register all of the rdflib-sqlalchemy Store plugins.

    """
    from rdflib.store import Store
    from rdflib import plugin

    try:
        x = plugin.get("SQLAlchemy", Store)
        del x
        return  # plugins already registered
    except:
        pass  # must register plugins

    # Register the plugins ...

    plugin.register(
        "SQLAlchemy",
        Store,
        "rdflib_sqlalchemy.store",
        "SQLAlchemy",
    )
Example #4
0
	def __register_Turtle_serializer(formatstring) :
		"""The default Turtle Serializers of RDFlib is buggy and not very nice as far as the output is concerned. 
		An L{own version<serializers.TurtleSerializer>} is registered in RDFLib and used in the rest of the package.
		@param formatstring: the string to identify this serializer with.
		"""
		from rdflib.plugin import register
		from rdflib.syntax import serializer, serializers
		register(formatstring, serializers.Serializer, "pyRdfa.serializers.TurtleSerializer", "TurtleSerializer")
Example #5
0
	def _register_JSON_serializer_3(self) :
		"""JSON LD serializer 
		"""
		if not MyGraph.json_serializer_registered :
			from rdflib.plugin import register
			from rdflib.serializer import Serializer
			register(_json_serializer_name, Serializer,
					 "pyRdfaExtras.serializers.jsonserializer", "JsonSerializer")
			MyGraph.json_serializer_registered = True
Example #6
0
	def __register_XML_serializer(formatstring) :
		"""The default XML Serializer of RDFlib is buggy, mainly when handling lists.
		An L{own version<serializers.PrettyXMLSerializer>} is 
		registered in RDFlib and used in the rest of the package.
		@param formatstring: the string to identify this serializer with.
		"""
		from rdflib.plugin import register
		from rdflib.syntax import serializer, serializers
		register(formatstring, serializers.Serializer, "pyRdfa.serializers.PrettyXMLSerializer", "PrettyXMLSerializer")
Example #7
0
	def _register_XML_serializer_3(self) :
		"""The default XML Serializer of RDFLib 3.X is buggy, mainly when handling lists. An L{own version<serializers.prettyXMLserializer_3>} is
		registered in RDFlib and used in the rest of the package. 
		"""
		if not MyGraph.xml_serializer_registered_3 :
			from rdflib.plugin import register
			from rdflib.serializer import Serializer
			register(_xml_serializer_name, Serializer,
					 "pyRdfa.serializers.prettyXMLserializer_3", "PrettyXMLSerializer")
			MyGraph.xml_serializer_registered_3 = True
def initialize(parameters):
    # Try to add Neo as a backend for RDFLib
    try:
        from rdflib import plugin
        from rdflib.store import Store
        import neo4j._rdf # assert that the RDF subsystem is available
    except: # requirements failed, don't register the hook
        pass
    else: # register the hook
        plugin.register('Neo', Store, 'neo4j._hooks.rdflib', 'NeoRdfStore')
Example #9
0
	def _register_Turtle_parser_2(self) :
		"""The default Turtle parser of RDFLib 2.X is buggy: some constants, like 'true' or 'false', are not handled.
		An L{own version<serializers.TurtleSerializer>} is registered in RDFLib and used in the rest of the package.
		This is not used for RDFLib 3.X.
		"""
		if not MyGraph.turtle_parser_registered_2 :
			from rdflib.plugin import register
			from rdflib.syntax 	import parsers
			register(_turtle_parser_name, parsers.Parser, "RDFClosure.parsers.N3Parser","N3Parser")
			MyGraph.turtle_parser_registered_2 = True
Example #10
0
    def buildProvenanceTrail(self):
        self.log.debug("Loading provenance trail")
        plugin.register("sparql", query.Processor, "rdfextras.sparql.processor", "Processor")
        plugin.register("sparql", query.Result, "rdfextras.sparql.query", "SPARQLQueryResult")

        #        self.log.debug(self.g.serialize(format='turtle'))

        expressions_works = self.g.query(
            """SELECT DISTINCT ?w ?e
       WHERE {
          ?w rdf:type frbr:Work .
          ?e frbr:realizationOf ?w .
          ?e provo:wasGeneratedAt ?t .
          ?t time:inXSDDateTime ?dt .
       } ORDER BY ?w, ?dt """,
            initNs=dict(
                frbr=Namespace("http://purl.org/vocab/frbr/core#"),
                provo=Namespace("http://www.w3.org/ns/prov-o/"),
                time=Namespace("http://www.w3.org/2006/time#"),
            ),
        )
        #        self.log.debug(expressions_works.result)

        for row in expressions_works.result:
            (work, expression) = row

            self.trail.setdefault(work, []).append(expression)

            self.log.debug("Work: %s\nExpression: %s" % (work, expression))

        activities = self.g.query(
            """SELECT DISTINCT ?a
       WHERE {
          ?a rdf:type provo:Activity .
          ?a provo:endedAt ?t .
          ?t time:inXSDDateTime ?dt .
       } ORDER BY ?dt """,
            initNs=dict(
                frbr=Namespace("http://purl.org/vocab/frbr/core#"),
                provo=Namespace("http://www.w3.org/ns/prov-o/"),
                time=Namespace("http://www.w3.org/2006/time#"),
            ),
        )
        self.log.debug(activities.result)

        for row in activities.result:

            self.trail.setdefault(self.PROV["Activity"], []).append(row)

            self.log.debug("Activity: %s" % (row))

        self.log.debug(self.trail)
        #        quit()
        return
Example #11
0
	def _register_Turtle_serializer_3(self) :
		"""The default Turtle Serializers of RDFLib 2.X is buggy and not very nice as far as the output is concerned.
		An L{own version<serializers.TurtleSerializer>} is registered in RDFLib and used in the rest of the package.
		This is not used for RDFLib 3.X.
		"""
		if not MyGraph.turtle_serializer_registered_3 :
			from rdflib.plugin import register
			from rdflib.serializer import Serializer
			register(_turtle_serializer_name, Serializer,
					 "RDFClosure.serializers.TurtleSerializer_3", "TurtleSerializer")
			MyGraph.turtle_serialzier_registered_3 = True
Example #12
0
	def _register_Turtle_serializer_2(self) :
		"""The default Turtle Serializers of RDFLib 2.X is buggy and not very nice as far as the output is concerned.
		An L{own version<serializers.TurtleSerializer>} is registered in RDFLib and used in the rest of the package.
		This is not used for RDFLib 3.X.
		"""
		if not MyGraph.turtle_serializer_registered_2 :
			from rdflib.plugin import register
			from rdflib.syntax import serializer, serializers
			register(_turtle_serializer_name, serializers.Serializer,
					 "pyRdfaExtras.serializers.turtleserializer", "TurtleSerializer")
			MyGraph.turtle_serialzier_registered_2 = True
Example #13
0
	def _register_XML_serializer_2(self) :
		"""The default XML Serializer of RDFLib 2.X is buggy, mainly when handling lists.
		An L{own version<serializers.prettyXMLserializer>} is
		registered in RDFlib and used in the rest of the package. This is not used for RDFLib 3.X.
		"""
		if not MyGraph.xml_serializer_registered_2 :
			from rdflib.plugin import register
			from rdflib.syntax import serializer, serializers
			register(_xml_serializer_name, serializers.Serializer,
					 "pyRdfaExtras.serializers.prettyXMLserializer", "PrettyXMLSerializer")
			MyGraph.xml_serializer_registered_2 = True
Example #14
0
def test_jsonld():
    # generate shared canvase json-ld
    tei_file = "../../data/tei/ox/ox-frankenstein_notebook_c1.xml"
    manifest_uri = 'http://example.com/frankenstein.json'
    m = Manifest(tei_file, manifest_uri)
    jsonld = m.jsonld()

    # parse the json-ld as rdf
    register('json-ld', Parser, 'rdflib_jsonld.parser', 'JsonLDParser')
    g = ConjunctiveGraph()
    g.parse(data=jsonld, format='json-ld')

    # sanity check the graph
    assert g.value(URIRef('http://example.com/frankenstein.json'), RDF.type) == URIRef('http://www.shared-canvas.org/ns/Manifest')
Example #15
0
def test_jsonld():
    # generate shared canvase json-ld
    tei_file = "sga/data/tei/ox/ox-frankenstein_notebook_c1.xml"
    manifest_uri = 'http://example.com/frankenstein.json'
    m = Manifest(tei_file, manifest_uri)
    jsonld = m.jsonld()
    open('test.jsonld', 'w').write(json.dumps(jsonld, indent=2))

    # find the manifest
    manifest = None
    for r in jsonld['@graph']:
        if '@type' in r and r['@type'] == 'sc:Manifest':
            manifest = r
    assert manifest

    # check for images
    assert 'images' in manifest

    # check for canvases
    assert 'canvases' in manifest

    # get the sequence
    assert 'sequences' in manifest
    seq = get(jsonld, manifest['sequences'][0])

    # first canvas
    assert 'first' in seq
    canvas = get(jsonld, seq['first'])
    assert canvas['label'] == '1r'

    # check the content annotations
    assert count_type(jsonld, 'sc:ContentAnnotation') == 90
   
    # css should be there
    assert count_type(jsonld, 'cnt:ContentAsText') == 61

    # parse the json-ld as rdf
    register('json-ld', Parser, 'rdflib_jsonld.parser', 'JsonLDParser')
    g = ConjunctiveGraph()
    jsonld_str = json.dumps(jsonld)
    g.parse(data=jsonld_str, format='json-ld')

    # quick sanity check the graph
    assert g.value(URIRef('http://example.com/frankenstein.json'), RDF.type) == URIRef('http://www.shared-canvas.org/ns/Manifest')
    line_anns = list(g.triples((None, RDF.type, SGA.LineAnnotation)))
    assert len(line_anns) == 638
Example #16
0
 def __init__(self, directory, config, level = logging.DEBUG):
     """TabLinker constructor
     
     Keyword arguments:
     directory -- String containing the name turtle file
     config -- Configuration object, loaded from .ini file
     level -- A logging level as defined in the logging module
     """
     self.config = config
     self.log = logging.getLogger("TabLinker")
     self.log.setLevel(level)
     
     self.graph = ConjunctiveGraph()
     self.log.debug('Loading and parsing file')
     self.graph.parse(filename, format=config.get('general', 'format'))
     
     plugin.register('sparql', rdflib.query.Processor,'rdfextras.sparql.processor', 'Processor')
     plugin.register('sparql', rdflib.query.Result,'rdfextras.sparql.query', 'SPARQLQueryResult')
Example #17
0
 def setUp(self):
     register('json-ld',
              Parser,
              'rdflib_jsonld.parser',
              'JsonLDParser')
     # self.graph = Graph()
     self.proj_context_uri = 'http://127.0.0.1:8000/contexts/projects/3.json'
     self.context_file = settings.STATIC_IMPORTS_ROOT + '3-context.json'
     self.data_file = settings.STATIC_IMPORTS_ROOT + 'dt-bone.json'
     self.context_str = self.request_json_str(self.proj_context_uri)
     self.data_str = self.load_json_file_str(self.data_file)
     g_context = ConjunctiveGraph(identifier=self.proj_context_uri)
     g_context.parse(data=self.context_str, format='json-ld')
     proj_graph_obj = URIRef(self.proj_context_uri)
     print('N3: ' + str(g_context.__str__()))
     for c in g_context.contexts():
         print(str(c))
     print('-------Triples for context-graph------')
     self.test_type_linking(g_context)
     g_data = ConjunctiveGraph().parse(data=self.data_str, format='json-ld')
     print('-------Triples for data-record--------')
     self.test_type_linking(g_data)
import rdflib
from rdflib import Graph, OWL, RDF, RDFS, XSD
# Enable SPARQL query
from rdflib import plugin
plugin.register(
    'sparql', rdflib.query.Processor,
    'rdfextras.sparql.processor', 'Processor')
plugin.register(
    'sparql', rdflib.query.Result,
    'rdfextras.sparql.query', 'SPARQLQueryResult')


class OntologyAnalyzer:
    """
    """
    
    def __init__(self, ontology, opts = {}):
        """
        ontology -- File name of the ontology to analyze
        """
        if not ontology: raise ValueError
        
        self.graph = Graph()
        self.graph.load(ontology)
        
        # Bind default NS to prefix on graph
        self.graph.bind('owl',  OWL)
        self.graph.bind('rdf',  RDF)
        self.graph.bind('rdfs', RDFS)
        self.graph.bind('xsd',  XSD)
        
Example #19
0
from typing import Any, Dict, List, Mapping, MutableSequence, Optional, Union, cast
from urllib.parse import urlparse

import pkg_resources  # part of setuptools
from rdflib.parser import Parser
from rdflib.plugin import register
from ruamel.yaml.comments import CommentedMap, CommentedSeq

from . import codegen, jsonld_context, schema
from .avro.schema import SchemaParseException
from .exceptions import ValidationException, to_one_line_messages
from .makedoc import makedoc
from .ref_resolver import Loader, file_uri
from .utils import json_dumps

register("json-ld", Parser, "rdflib_jsonld.parser", "JsonLDParser")
_logger = logging.getLogger("salad")


def printrdf(
    workflow: str,
    wf: Union[CommentedMap, CommentedSeq],
    ctx: Dict[str, Any],
    sr: str,
) -> None:
    g = jsonld_context.makerdf(workflow, wf, ctx)
    print(g.serialize(format=sr, encoding="utf-8").decode("utf-8"))


def main(argsl: Optional[List[str]] = None) -> int:
    if argsl is None:
Example #20
0
# -*- coding: UTF-8 -*-

import re
import json
import itertools
from rdflib import Graph
from rdflib.plugin import register, Serializer

register("json-ld", Serializer, "rdflib.plugins.serializers.jsonld", "JsonLDSerializer")


cases = []


def case(*args):
    cases.append(args)


case(
    """
@prefix dc: <http://purl.org/dc/terms/> .
<http://example.org/>
    dc:title "Homepage"@en .
""",
    {
        "@context": {"@vocab": "http://purl.org/dc/terms/", "@language": "en"},
        "@id": "http://example.org/",
        "title": "Homepage",
    },
)
Example #21
0
from os import path
from rdflib import ConjunctiveGraph, URIRef, Literal
from rdflib.namespace import Namespace, RDF, RDFS, DCTERMS, XSD
from rdflib.plugin import register, Serializer
from ConfigParser import SafeConfigParser
from datetime import datetime
from urllib2 import urlparse

# rdflib-jsonld module required
register('application/ld+json', Serializer, 'rdflib_jsonld.serializer',
         'JsonLDSerializer')

# default metadata config file
_CONFIG_FILE = path.join(path.dirname(__file__), 'metadata.ini')

# define additional namespaces
DCAT = Namespace('http://www.w3.org/ns/dcat#')
LANG = Namespace('http://id.loc.gov/vocabulary/iso639-1/')
DBPEDIA = Namespace('http://dbpedia.org/resource/')
#SPARQLSD = Namespace('http://www.w3.org/ns/sparql-service-description#')

# define which sections/fields in the metadata config file are mandatory
_CORE_META = ['title', 'publisher', 'version', 'issued', 'modified']
_REQUIRED_META = dict(fdp=_CORE_META + ['fdp_id', 'catalog_id'],
                      catalog=_CORE_META + ['dataset_id', 'theme_taxonomy'],
                      dataset=_CORE_META + ['distribution_id', 'theme'],
                      distribution=_CORE_META +
                      ['access_url|download_url', 'media_type', 'license'])

# mappings between fields in the config file and ontologies/vocabularies and data types
_ONTO_MAP = dict(fdp_id=[(DCTERMS.identifier, XSD.string)],
Example #22
0
"""
RDFlib Store implementation for Django, providing lots of extra goodies.

Use this application by just including it in your INSTALLED_APPS. After this,
you can create a new Graph using:

>>> import rdflib
>>> g = rdflib.Graph('Django')

"""
from rdflib.plugin import register
from rdflib.store import Store

register('Django', Store, 'rdflib_django.store', 'DjangoStore')
Example #23
0
def initialize(args):
    """Build all needed objects.

    Returns:
        A dictionary containing the store object and git repo object.

    """
    if args['verbose']:
        ch.setLevel(logging.ERROR - args['verbose'] * 10)

    logger.addHandler(ch)
    logger.debug("Parsed args: {}".format(args))

    # add the handlers to the logger
    if args['logfile']:
        try:
            fh = logging.FileHandler(args['logfile'])
            fh.setLevel(logging.DEBUG)
            fh.setFormatter(formatter)
            logger.addHandler(fh)
            logger.debug("Logfile: {}".format(args['logfile']))
        except FileNotFoundError:
            logger.error("Logfile not found: {}".format(args['logfile']))
            sys.exit('Exiting quit')
        except PermissionError:
            logger.error("Can not create logfile: {}".format(args['logfile']))
            sys.exit('Exiting quit')

    # from Github: https://github.com/RDFLib/rdflib/issues/617
    # Egregious hack, the SequencePath object doesn't support compare, this implements the __lt__
    # method so that algebra.py works on sorting in SPARQL queries on e.g. rdf:List paths

    def sequencePathCompareLt(self, other):
        return str(self) < str(other)

    def sequencePathCompareGt(self, other):
        return str(self) < str(other)

    setattr(SequencePath, '__lt__', sequencePathCompareLt)
    setattr(SequencePath, '__gt__', sequencePathCompareGt)
    # End egregious hack

    # To get the best behavior, but still we have https://github.com/RDFLib/rdflib/issues/810
    rdflib.plugins.sparql.SPARQL_DEFAULT_GRAPH_UNION = args['defaultgraph_union']

    # To disable web access: https://github.com/RDFLib/rdflib/issues/810
    rdflib.plugins.sparql.SPARQL_LOAD_GRAPHS = False

    register(
        'sparql', Processor,
        'quit.tools.processor', 'SPARQLProcessor')

    register(
        'sparql', UpdateProcessor,
        'quit.tools.processor', 'SPARQLUpdateProcessor')

    register(
        'application/x-turtle', Serializer,
        'rdflib.plugins.serializers.turtle', 'TurtleSerializer')

    register(
        'application/xml', Serializer,
        'rdflib.plugins.serializers.rdfxml', 'XMLSerializer')

    register(
        'application/json', Serializer,
        'rdflib_jsonld.serializer', 'JsonLDSerializer')

    register(
        'application/trig', Serializer,
        'rdflib.plugins.serializers.trig', 'TrigSerializer')

    register(
        'application/xml', ResultSerializer,
        'rdflib.plugins.sparql.results.xmlresults', 'XMLResultSerializer')

    register(
        'application/json', ResultSerializer,
        'rdflib.plugins.sparql.results.jsonresults', 'JSONResultSerializer')

    register(
        'html', ResultSerializer,
        'quit.plugins.serializers.results.htmlresults', 'HTMLResultSerializer')

    register(
        'text/html', ResultSerializer,
        'quit.plugins.serializers.results.htmlresults', 'HTMLResultSerializer')

    register(
        'application/xhtml+xml', ResultSerializer,
        'quit.plugins.serializers.results.htmlresults', 'HTMLResultSerializer')

    register(
        'text/csv', ResultSerializer,
        'rdflib.plugins.sparql.results.csvresults', 'CSVResultSerializer')

    register(
        'application/sparql-results+xml', ResultSerializer,
        'rdflib.plugins.sparql.results.xmlresults', 'XMLResultSerializer')

    register(
        'application/sparql-results+json', ResultSerializer,
        'rdflib.plugins.sparql.results.jsonresults', 'JSONResultSerializer')

    try:
        config = QuitStoreConfiguration(
            configfile=args['configfile'],
            targetdir=args['targetdir'],
            upstream=args['repourl'],
            features=args['features'],
            namespace=args['namespace'],
            oauthclientid=args['oauth_clientid'],
            oauthclientsecret=args['oauth_clientsecret'],
        )
    except InvalidConfigurationError as e:
        logger.error(e)
        sys.exit('Exiting quit')

    # since repo is handled, we can add graphs to config

    logger.info('QuitStore Configuration initialized.')
    logger.debug('Path of Gitrepo: ' + config.getRepoPath())

    return config
Example #24
0
from rdflib.plugin import register, Parser
from rdflib import Graph, ConjunctiveGraph
import json

register('application/ld+json', Parser, 'rdflib_jsonld.parser', 'JsonLDParser')

graph = ConjunctiveGraph()

sample_v1 = {
    "@context": {
        "organ": "http://purl.obolibrary.org/obo/UBERON_0000062"
    },
    "biomaterial_id": "Specimen_PBMC2",
    "ncbi_taxon_id": 9606,
    "organ": "blood"
}

graph.parse(data=json.dumps(sample_v1), format="json-ld")

qres = graph.query(
    "SELECT ?organ WHERE { ?s <http://purl.obolibrary.org/obo/UBERON_0000062> ?organ}"
)

for row in qres:
    print("%s" % row)

graph = ConjunctiveGraph()

graph.parse("sample_v2.json", format="json-ld")

qres = graph.query(
Example #25
0
format = args.format
outfile = args.outfile
dbpfx = 'sqlite:///'

if dburl.startswith(dbpfx) is True:
    dbfile = dburl.replace(dbpfx, '')

    if os.path.isfile(dbfile) is False:
        parser.error("sqlite dbfile '%s' not found" % dbfile)

import rdflib as _rdf
import sqlalchemy as _sqla
from rdb2rdf import *
from rdflib.plugin import register, Store

# serialize RDF data
# Bug: rdb2rdf crashes on MySQL INTEGER data types TINY|SMALL|MEDIUM|BIGINT
db = _sqla.create_engine(dburl, echo=False)
register('rdb2rdf_dm', Store, 'rdb2rdf.stores', 'DirectMapping')
graph = _rdf.Graph('rdb2rdf_dm')
graph.open(db)

# write RDF into file or STDOUT
if outfile is not None:
    with open(outfile, 'w') as fout:
        fout.write(graph.serialize(format=format))
else:
    print(graph.serialize(format=format))

graph.close()
Example #26
0
    r.addNarrower(animals, mammals)
    r.addBroader(mammals, animals)
    r.addHasTopConcept(keyScheme, animals)

    r.addRelatedMatch(animals, mammals)
    r.addEditorialNote(animals, 'this is a test')

    gen = r.getBroader(mammals)
    for item in gen:
        if type(item) is URIRef:
            print 'Concept: ' + item
        else:
            print 'Literal language: ' + item.language

    plugin.register('skos', Serializer, 'skosserializer', 'SKOSSerializer')
    print r.graph.serialize(format='skos', encoding=r.encoding)

    for i, j in r.graph.subject_objects(r.osmImplies):
        print i
        print j
    print '\n'
    r.removeOSMImpliesLiteral(animals, 'blubb')
    print '\n'
    for i, j in r.graph.subject_objects(r.osmImplies):
        print i
        print j
    print '\n'
    '''graph = Graph()
    g = graph.parse(utils.dataDir() + 'gemet\gemet_concept_en.rdf')
    for i in g.objects(URIRef('http://www.eionet.europa.eu/gemet/concept/8073'), SKOS.prefLabel):
Example #27
0
from rdflib import plugin
from rdflib import store

# Support execution of nosetests without actual installation
plugin.register(
        'ZODB', store.Store,
        'rdflib_zodb.ZODB', 'ZODBStore')
Example #28
0
#
# ---------------------------------------------------------------------

from rdflib import URIRef
from rdflib.plugin import register
try:  # These moved during the transition from rdflib 2.4 to rdflib 3.0.
    from rdflib.plugins.serializers.rdfxml import PrettyXMLSerializer  # 3.0
    from rdflib.serializer import Serializer
except ImportError:
    from rdflib.syntax.serializers.PrettyXMLSerializer import PrettyXMLSerializer  # 2.4
    from rdflib.syntax.serializers import Serializer


class BetterPrettyXMLSerializer(PrettyXMLSerializer):
    def relativize(self, uri):
        base = URIRef(self.base)
        basedir = URIRef(
            self.base if base.endswith('/') else base.rsplit('/', 1)[0])
        if base is not None:
            if uri == base:
                uri = URIRef('')
            elif uri == basedir:
                uri = URIRef('.')
            elif uri.startswith(basedir + '/'):
                uri = URIRef(uri.replace(basedir + '/', "", 1))
        return uri


register('better-pretty-xml', Serializer, __name__,
         'BetterPrettyXMLSerializer')
Example #29
0
'''
Created on 11/04/2011

@author: acalvo
'''
import rdflib
from rdflib import Graph, Namespace
from rdflib import plugin
FOAF = Namespace("http://xmlns.com/foaf/0.1/")
RDFS = Namespace("http://www.w3.org/2000/01/rdf-schema#")

if False:
    plugin.register('sparql', rdflib.query.Processor,
                    'rdfextras.sparql.processor', 'Processor')
    plugin.register('sparql', rdflib.query.Result, 'rdfextras.sparql.query',
                    'SPARQLQueryResult')

my_data = '''
        @prefix dc:   <http://purl.org/dc/elements/1.1/> .
        @prefix :     <http://example.org/book/> .
        @prefix ns:   <http://example.org/ns#> .
        
        :book1  dc:title  "SPARQL Tutorial" .
        :book1  ns:price  42 .
        :book2  dc:title  "The Semantic Web" .
        :book2  ns:price  23 .
     '''
g = Graph()
g.parse(data=my_data, format="n3")
print g.serialize(format='xml')
Example #30
0
>>> import rdflib
>>> import rdflib_schemaorg_csv
>>> g = rdflib.Graph()
>>> g.parse("test.csv", format="schemaorg_csv")
>>> print g.serialize()
"""

import csv
import StringIO
import sys
from rdflib import URIRef, Literal, BNode, Namespace, RDF
from rdflib.plugin import register
from rdflib.parser import Parser

register("schemaorg_csv", Parser, "rdflib_schemaorg_csv", "SchemaOrgCSVParser")

class SchemaOrgCSVParser(Parser):
	NAMESPACES = {	
		'schema' : Namespace('http://schema.org/'),
		'scsv' : Namespace('http://purl.org/NET/schema-org-csv#'),
		'dcterms' : Namespace('http://purl.org/dc/terms/')
	}
	
	def parse(self, source, sink, **kwargs):
		"""
		Pass in a file or file-like object containing CSV with Schema.org
		column headers and populate the sink graph with triples.
		"""
		row_num = 1
		fURI = kwargs.get("csv_file_URI", "")
Example #31
0
def registerplugins():
    """
    If rdfextras is installed with setuptools, all plugins are registered
    through entry_points. This is strongly recommended. 

    If only distutils is available, the plugins must be registed manually
    This method will register all rdfextras plugins

    """
    from rdflib import plugin
    from rdflib.query import Processor

    try:
        x=plugin.get('sparql',Processor)
        return # plugins already registered
    except:
        pass # must register plugins    

    from rdflib.query import ResultParser, ResultSerializer, Result

    plugin.register('sparql', Result,
        'rdfextras.sparql.query', 'SPARQLQueryResult')
    plugin.register('sparql', Processor,
        'rdfextras.sparql.processor', 'Processor')

    plugin.register('html', ResultSerializer,
        'rdfextras.sparql.results.htmlresults', 'HTMLResultSerializer')
    plugin.register('xml', ResultSerializer,
        'rdfextras.sparql.results.xmlresults', 'XMLResultSerializer')
    plugin.register('json', ResultSerializer,
        'rdfextras.sparql.results.jsonresults', 'JSONResultSerializer')
    plugin.register('xml', ResultParser,
        'rdfextras.sparql.results.xmlresults', 'XMLResultParser')
    plugin.register('json', ResultParser,
        'rdfextras.sparql.results.jsonresults', 'JSONResultParser')
Example #32
0
from rdflib.store import Store
from rdflib.namespace import RDF, RDFS, SKOS, OWL
from urllib import quote
from datetime import datetime
import re
import os
import string
import requests
from linkitup.util import get_qname
from linkitup import app
from provenance import trail_to_prov

# Override RDFLib trig serializer with app/util/trig.py
# RDFLib serializer still uses the deprecated '=' between graph URI and graph contents
from rdflib.serializer import Serializer
plugin.register('trig', Serializer, 'app.util.trig', 'TrigSerializer')

LUV = Namespace('http://linkitup.data2semantics.org/vocab/')
LU = Namespace('http://linkitup.data2semantics.org/resource/')
DBPEDIA = Namespace('http://dbpedia.org/resource/')
FOAF = Namespace('http://xmlns.com/foaf/0.1/')
DCTERMS = Namespace('http://purl.org/dc/terms/')
NANOPUB = Namespace('http://www.nanopub.org/nschema#')
PROV = Namespace('http://www.w3.org/ns/prov#')
OA = Namespace('http://www.w3.org/ns/oa#')

graph_store_endpoint = app.config.get('GRAPH_STORE_ENDPOINT')
graph_store_auth_string = app.config.get('GRAPH_STORE_AUTH')
if graph_store_auth_string:
    graph_store_auth = requests.auth.HTTPDigestAuth(*graph_store_auth_string.split(':'))
else:
Example #33
0
import os
import sys
import rdflib
from enum import Enum
from rdflib import plugin
from rdflib.serializer import Serializer
from rdflib.plugins.memory import IOMemory

__author__ = 'Misael Mongiovi, Andrea Giovanni Nuzzolese'

plugin.register('application/rdf+xml', Serializer,
                'rdflib.plugins.serializers.rdfxml', 'XMLSerializer')
plugin.register('xml', Serializer, 'rdflib.plugins.serializers.rdfxml',
                'XMLSerializer')


class FredType(Enum):
    Situation = 1
    Event = 2
    NamedEntity = 3
    SkolemizedEntity = 4
    Quality = 5
    Concept = 6


class NodeType(Enum):
    Class = 1
    Instance = 0


class ResourceType(Enum):
Example #34
0
from rdflib import Namespace
from rdflib import plugin
from rdflib.serializer import Serializer

import rdfjson
import rdfextras


SUPPORTED_OUTPUT_FORMATS = ["rdf-xml", "ttl", "nt", "json", "csv"]

NAMESPACES = {	'void' : Namespace('http://rdfs.org/ns/void#'),
				'eui' : Namespace('http://institutions.publicdata.eu/#')
}

# for RDF/JSON output:
plugin.register("rdf-json-pretty", Serializer, "rdfjson.RdfJsonSerializer", "PrettyRdfJsonSerializer")
# for SPARQL query support:
plugin.register('sparql', rdflib.query.Processor, 'rdfextras.sparql.processor', 'Processor')
plugin.register('sparql', rdflib.query.Result, 'rdfextras.sparql.query', 'SPARQLQueryResult')

class MainHandler(webapp.RequestHandler):
	def get(self):
		self.response.out.write(template.render('index.html', None))

class NotFoundHandler(webapp.RequestHandler):
	def get(self):
		self.error(404)
		self.response.out.write(template.render('a404.html', None))

class WellKnownVoIDHandler(webapp.RequestHandler):
	def get(self):
Example #35
0
Project      : rdfconvert
Description  : A little python script that converts files and whole directory trees from one RDF serialization into another.
Author       : Wim Pessemier
Contact      : w**.p********@ster.kuleuven.be (replace *)
Organization : Institute of Astronomy, KU Leuven
"""

from rdflib import Graph, plugin
from rdflib.parser import Parser
from rdflib.serializer import Serializer
import sys
import os
import argparse
import fnmatch

plugin.register("rdf-json", Parser, "rdflib_rdfjson.rdfjson_parser",
                "RdfJsonParser")
plugin.register("rdf-json", Serializer, "rdflib_rdfjson.rdfjson_serializer",
                "RdfJsonSerializer")

INPUT_FORMAT_TO_EXTENSIONS = {
    "application/rdf+xml": [".xml", ".rdf", ".owl"],
    "text/html": [".html"],
    "xml": [".xml", ".rdf", ".owl"],
    "rdf-json": [".json"],
    "json-ld": [".jsonld", ".json-ld"],
    "ttl": [".ttl"],
    "nt": [".nt"],
    "nquads": [".nq"],
    "trix": [".xml", ".trix"],
    "rdfa": [".xhtml", ".html"],
    "n3": [".n3"]
Example #36
0
 def setUp(self):
     self.execute('CREATE')
     plugin.register(
         'hstore', Store, 'rdflib_hstore.hstorestore', 'HstoreStore')
     self.graphs = []
Example #37
0
		for prefix, namespace in bindings:
			if (not suppressNamespace) or (not unicode(namespace) in suppressNamespace) :# << Suppress namespaces <<< CHANGE
				if prefix:
					write('   xmlns:%s="%s"\n' % (prefix, namespace))
				else:
					write('   xmlns="%s"\n' % namespace)
		write('>\n')

		# write out triples by subject
		for subject in self.store.subjects():
			self.subject(subject, 1)

		# endRDF
		write("</rdf:RDF>\n")

		# Set to None so that the memory can get garbage collected.
		# self.__serialized = None
		del self._XMLSerializer__serialized
		


		
register(
    'rdf-xml', Serializer,
    'RefactorRDF', 'XMLSerializerExtension')

		
	
if __name__ == '__main__':
	RefactorRDF()
from rdflib import plugin
from rdflib import store

plugin.register('SQLAlchemy', store.Store,
        'rdflib_sqlalchemy.SQLAlchemy', 'SQLAlchemy')
Example #39
0
from rdflib import plugin
from rdflib import store
import sys  # sop to Hudson
sys.path.insert(0, '/var/lib/tomcat6/webapps/hudson/jobs/rdfextras')

plugin.register('MySQL', store.Store, 'rdflib_mysql.MySQL', 'MySQL')
Example #40
0

class TPFStore(Store):
    def open(self, start_iri, create=False):
        self.collec = TriplePatternFragment.from_iri(start_iri).dataset

    def add(self, *triples, context, quoted=False):
        raise NotImplemented("TPFStore is readonly")

    def remove(self, *triples, context=None):
        raise NotImplemented("TPFStore is readonly")

    def triples(self, triple_pattern, context=None):
        for triple in self.collec.iter_triples(*triple_pattern):
            if _triple_match(triple_pattern, triple):
                yield triple, context

    def __len__(self, context=None):
        return self.collec.get_tpf().triple_count


def _node_match(template_node, node):
    return 1 if (template_node is None or template_node == node) else 0


def _triple_match(template_triple, triple):
    return sum(map(_node_match, template_triple, triple)) == 3


register("TPFStore", Store, "hydra.tpf", "TPFStore")
Example #41
0
from rdflib import plugin
from rdflib import store
from rdflib import query
#from .test_context import ContextTestCase
#from .test_graph import GraphTestCase

import sys # sop to Hudson
sys.path.insert(0, '/var/lib/tomcat6/webapps/hudson/jobs/rdfextras')

plugin.register(
        'sparql', query.Processor,
        'rdfextras.sparql.processor', 'Processor')

plugin.register(
        'sparql', query.Result,
        'rdfextras.sparql.query', 'SPARQLQueryResult')

plugin.register(
        'BerkeleyDB', store.Store,
        'rdfextras.store.BerkeleyDB', 'BerkeleyDB')

plugin.register(
        'BDBOptimized', store.Store,
        'rdfextras.store.BDBOptimized', 'BDBOptimized')

plugin.register(
        'SPARQL', store.Store,
        'rdfextras.store.SPARQL', 'SPARQLStore')

# SQLObject schemes
# scheme://[user[:password]@]host[:port]/database[?parameters]
Example #42
0
from django.core.exceptions import PermissionDenied

from tardis.tardis_portal.auth import decorators as authz
from tardis.tardis_portal.models import \
    Experiment, Schema
from tardis.tardis_portal.shortcuts import render_response_index, \
    return_response_error, return_response_not_found, \
    RestfulExperimentParameterSet

from .forms import FoRCodeForm

SCHEMA_URI = 'http://purl.org/asc/1297.0/2008/for/'
PARAMETER_NAMES = FoRCodeForm().fields.keys()

plugin.register('application/octet-stream', Parser,
                'rdflib.plugins.parsers.notation3', 'N3Parser')

logger = logging.getLogger(__name__)

for_graph = Graph()
try:
    for_graph.parse(SCHEMA_URI)
except (urllib.error.URLError, SocketError):
    logger.debug('no data connection to get external schema definition')


def _get_schema_func(schema_uri):
    def get_schema():
        try:
            return Schema.objects.get(namespace=schema_uri)
        except Schema.DoesNotExist:
Example #43
0
import rdflib

import validator

import jsonld

from rdflib import Graph, plugin
from rdflib.serializer import Serializer
from rdflib.parser import Parser
plugin.register('json-ld', Serializer, 'rdflib_jsonld.serializer', 'JsonLDSerializer')
plugin.register('json-ld', Parser, 'rdflib_jsonld.parser', 'JsonLDParser')

#import rdfextras
#rdfextras.registerplugins() # if no setuptools

from contextlib import closing

from urllib2 import urlopen
import html5lib
import json

import logging
import re


context = {
  "@vocab": "http://schema.org/",
  "http://schema.org/url": {
    "@type": "@id"
  },
from rdflib.plugin import register, Serializer
register('json-ld', Serializer, 'rdflib_jsonld.serializer', 'JsonLDSerializer')
from SPARQLWrapper import SPARQLWrapper, JSON
import requests
import urllib.parse
import json
import time

def dbpediaSpotlight(text):

    subjects = []    
    #*
    data= urllib.parse.urlencode({'text' : text,'confidence':'0.5', 'types' : 'DBpedia:Event, DBpedia:Food, DBpedia:Name, DBpedia:Person, DBpedia:Place, DBpedia:Work'})
 
    data = data.encode('utf-8')
    
    headers = {"Accept": "application/json"}
    
    r = requests.post("http://model.dbpedia-spotlight.org/it/annotate", data=data, headers=headers, timeout=50)
    
    print ("dbpediaspot: "+r.text) 
    # Convert it to a Python dictionary
    
    try:
        json.loads(r.text)
    
    except ValueError:
        return subjects
    
    data = json.loads(r.text)
    
from nose.exc import SkipTest
from rdflib import Literal
from rdflib import RDF
from rdflib import RDFS
from rdflib import URIRef
from rdflib import plugin
from rdflib import query
from rdflib.graph import ConjunctiveGraph
from rdflib.graph import Graph
from rdflib.graph import ReadOnlyGraphAggregate
from rdflib.store import Store
from six.moves import cStringIO as StringIO


plugin.register("xml", query.ResultParser, "rdflib.plugins.sparql.results.xmlresults", "XMLResultParser")
plugin.register("xml", query.ResultSerializer, "rdflib.plugins.sparql.results.xmlresults", "XMLResultSerializer")

plugin.register("json", query.ResultParser, "rdflib.plugins.sparql.results.jsonresults", "JSONResultParser")
plugin.register("json", query.ResultSerializer, "rdflib.plugins.sparql.results.jsonresults", "JSONResultSerializer")


testGraph1N3 = """
@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix : <http://test/> .
:foo a rdfs:Class.
:bar :d :c.
:a :d :c.
"""
Example #46
0
        "beginOffset" : {
          "@id" : "oax:begin"
        },
        "endOffset" : {
          "@id" : "oax:end"
        },
        "textOffsetSelector" : {
          "@type" : "@id",
          "@id" : "oax:TextOffsetSelector"
        },
        "chars" : "cnt:chars",
        "encoding" : "cnt:characterEncoding",
        "bytes" : "cnt:bytes",
        "format" : "dc:format",
        "language" : "dc:language",
        "annotatedAt" : "oa:annotatedAt",
        "serializedAt" : "oa:serializedAt",
        "when" : "oa:when",
        "value" : "rdf:value",
        "start" : "oa:start",
        "end" : "oa:end",
        "exact" : "oa:exact",
        "prefix" : "oa:prefix",
        "suffix" : "oa:suffix",
        "label" : "rdfs:label",
        "name" : "foaf:name",
        "mbox" : "foaf:mbox"
      }

register('json-ld', Serializer, 'rdflib_jsonld.serializer', 'JsonLDSerializer')
Example #47
0
import copy
import yaml
import pprint
import os
import json

# setup the ontology
from smart_common.rdf_tools.rdf_ontology import *
from smart_common.rdf_tools.util import bound_graph, URIRef

# setup the json-ld serializer
import rdflib_jsonld_serializer
from rdflib import plugin
from rdflib.serializer import Serializer
plugin.register('json-ld',
                Serializer,
                'rdflib_jsonld_serializer.jsonld_serializer',
                'JsonLDSerializer')

f = None
try:
    f = open(os.getcwd()+'/_config.yml').read()
except:
    raise IOError, "Can't read _config.yml"

if f != None:
    config = yaml.load(f)

SP_STATMENT = "http://smartplatforms.org/terms#Statement"
CONTEXT_URI = config['production_url']+'/reference/data_model/contexts/smart_context.jsonld'

# create smart_jsonld_context, copied from smart_sample_apps
Example #48
0
from rdflib import plugin
from rdflib import store

plugin.register('SQLAlchemy', store.Store, 'rdflib_sqlalchemy.SQLAlchemy',
                'SQLAlchemy')
directly, since it's a plugin. You'll just want to:

>>> import rdflib
>>> import rdflib_schemaorg_csv
>>> g = rdflib.Graph()
>>> g.parse("test.csv", format="schemaorg_csv")
>>> print g.serialize()
"""
import sys
import csv
import StringIO
from rdflib import URIRef, Literal, BNode, Namespace, RDF
from rdflib.plugin import register
from rdflib.parser import Parser

register("schemaorg_csv", Parser, "rdflib_schemaorg_csv", "SchemaOrgCSVParser")


class SchemaOrgCSVParser(Parser):
    NAMESPACES = {
        "schema": Namespace("http://schema.org/"),
        "scsv": Namespace("http://purl.org/NET/schema-org-csv#"),
        "dcterms": Namespace("http://purl.org/dc/terms/"),
    }

    def parse(self, source, sink, **kwargs):
        """
		Pass in a file or file-like object containing CSV with Schema.org
		column headers and populate the sink graph with triples.
		"""
        row_num = 1
Example #50
0
from rdflib import Graph, util
from os import path
import requests
from rdflib.plugin import register, Serializer
from rdflib_jsonld import serializer
from docprofiles import PROFILES
from docprofile_owlp import Owlp
from docprofile_skosp import Skosp

register("json-ld", Serializer, "rdflib_jsonld.serializer", "JsonLDSerializer")

RDF_FILE_EXTENSIONS = [".rdf", ".owl", ".ttl", ".n3", ".nt", ".json"]

RDF_SERIALIZER_MAP = {
    "text/turtle": "turtle",
    "text/n3": "n3",
    "application/n-triples": "nt",
    "application/ld+json": "json-ld",
    "application/rdf+xml": "xml",
    # Some common but incorrect mimetypes
    "application/rdf": "xml",
    "application/rdf xml": "xml",
    "application/json": "json-ld",
    "application/ld json": "json-ld",
    "text/ttl": "turtle",
    "text/turtle;charset=UTF-8": "turtle",
    "text/ntriples": "nt",
    "text/n-triples": "nt",
    "text/plain":
    "nt",  # text/plain is the old/deprecated mimetype for n-triples
}
Example #51
0
from os import path
from rdflib import Graph
from rdflib.compare import graph_diff
from rdflib.plugin import register, Parser
from myglobals import (BASE_URL, DUMP_DIR, MIME_TYPES, URL_PATHS)
import six

if six.PY2:
    from urllib2 import (urlopen, urlparse, Request)
    urljoin = urlparse.urljoin
else:
    from urllib.parse import urljoin
    from urllib.request import (Request, urlopen, urlparse)


register('application/ld+json', Parser, 'rdflib_jsonld.parser', 'JsonLDParser')

URLs = [urljoin(BASE_URL, p) for p in URL_PATHS]
g_fdp = Graph()   # FDP metadata upon HTTP request
g_dump = Graph()  # reference metadata from dump file


def test_compare_triple_counts():
    for mime, fext in MIME_TYPES.items():
        dump_path = path.join(DUMP_DIR, path.basename(mime))

        for url in URLs:
            if six.PY2:
                fname = '%s.%s' % (path.basename(urlparse.urlparse(url).path), fext)
            else:
                fname = '%s.%s' % (path.basename(urlparse(url).path), fext)
from typing import Any, Dict, List, Union, Text

from rdflib import Graph, plugin
from rdflib.serializer import Serializer

from . import schema
from . import jsonld_context
from . import makedoc
from . import validate
from .sourceline import strip_dup_lineno
from .ref_resolver import Loader, file_uri
_logger = logging.getLogger("salad")

from rdflib.plugin import register, Parser
register('json-ld', Parser, 'rdflib_jsonld.parser', 'JsonLDParser')


def printrdf(workflow,  # type: str
             wf,        # type: Union[List[Dict[Text, Any]], Dict[Text, Any]]
             ctx,       # type: Dict[Text, Any]
             sr         # type: str
             ):
    # type: (...) -> None
    g = jsonld_context.makerdf(workflow, wf, ctx)
    print(g.serialize(format=sr))


def main(argsl=None):  # type: (List[str]) -> int
    if argsl is None:
        argsl = sys.argv[1:]
Example #53
0
from rdflib import Graph, plugin, Namespace
from rdflib.parser import Parser
from rdflib.serializer import Serializer

plugin.register("rdf-json", Parser,
   "rdflib_rdfjson.rdfjson_parser", "RdfJsonParser")

plugin.register("rdf-json", Serializer,
    "rdflib_rdfjson.rdfjson_serializer", "RdfJsonSerializer")

plugin.register("rdf-json-pretty", Serializer,
    "rdflib_rdfjson.rdfjson_serializer", "PrettyRdfJsonSerializer")


testrdfjson = '''{
  "http://example.org/about" :
    {
       "http://purl.org/dc/elements/1.1/title": [
            { "type" : "literal" , "value" : "Anna's Homepage." }
        ]
    }
}'''

g = Graph()
g.bind("dc", "http://purl.org/dc/elements/1.1/")
g.parse(data=testrdfjson, format="rdf-json")
rdfxml = g.serialize(format="xml")
assert '''Anna's Homepage''' in rdfxml

print(rdfxml)
print("""<?xml version="1.0" encoding="UTF-8"?>
Example #54
0
from rdflib.plugin import register, Serializer, Parser


# adding namespaces
dcat = Namespace("http://www.w3.org/ns/dcat#")
res = Namespace("http://purl.org/obs/resource#")
obs = Namespace("http://purl.org/obs#")

# Logging Configuration
logging.basicConfig(filename='logger.log', level=logging.DEBUG, format="%(message)s")
logger  = logging.getLogger("observer")
logger.setLevel(logging.DEBUG)


# Registering Parsers
register('text/rdf+n3', Parser, 'rdflib.plugins.parsers.notation3', 'N3Parser')
register('n3', Parser, 'rdflib.plugins.parsers.notation3', 'N3Parser')
register('nquads', Parser, 'rdflib.plugins.parsers.nquads', 'NQuadsParser')
register('nt', Parser, 'rdflib.plugins.parsers.nt', 'NTParser')
register('trix', Parser,'rdflib.plugins.parsers.trix', 'TriXParser')
register('application/rdf+xml', Parser, 'rdflib.plugins.parsers.rdfxml', 'RDFXMLParser')
register('xml', Parser, 'rdflib.plugins.parsers.rdfxml', 'RDFXMLParser')
register('rdfa', Parser, 'rdflib.plugins.parsers.rdfa', 'RDFaParser')
register('text/html', Parser, 'rdflib.plugins.parsers.rdfa', 'RDFaParser')
register('application/xhtml+xml', Parser,'rdflib.plugins.parsers.rdfa', 'RDFaParser')
register('text/plain', Parser, 'rdflib.plugins.parsers.notation3', 'N3Parser')         
register('application/octet-stream', Parser, 'rdflib.plugins.parsers.notation3', 'N3Parser')

         
# Constants
SPARQL_FORMAT_TAG = [