class AllegroTripleStore(SesameTripleStore):
    def connect(self, dburi):
        host, rest = dburi[7:].split(':', 1)
        port, self._name = rest.split('/', 1)

        self._url = 'http://%s:%s/sesame' % (host, port)
        self._http = httplib2.Http()
        params = urlencode({'id': self._name, 'if-exists': 'open'})
        try:
            resp, content = self._http.request(
                '%s/repositories?%s' % (self._url, params), "POST")
        except socket.error:
            raise ConnectionError(
                'Can not connect to repository, is it running?')

        if resp['status'] != '204':
            raise ConnectionError('Can not connect to server: %s' %
                                  resp['status'])

    def _turtle_to_ntriples(self, data):
        # Turtle syntax is not supported by allegro graph
        # HACK workaround using redland
        import RDF
        model = RDF.Model()
        parser = RDF.TurtleParser()
        try:
            parser.parse_string_into_model(model, data.read(), '-')
        except RDF.RedlandError, err:
            raise TripleStoreError(err)

        serializer = RDF.Serializer(name='ntriples')
        return StringIO(serializer.serialize_model_to_string(model))
Example #2
0
    def write(self, path, fmt='', base_uri=None):
        if fmt == '':
            fmt = self._guess_fmt(path)

        gzipped_path = None

        if self._gzipped(path):
            gzipped_path = path
            tmp = self._mktemp()
            path = tmp

        serializer = RDF.Serializer(name=fmt)
        for (prefix, uri) in self.namespace_tbl.items():
            serializer.set_namespace(prefix, uri)

        logger.info('writing to "%s"...' % path)
        d = os.path.dirname(path)
        if d != '' and not os.path.exists(d):
            logger.warning('No such directory: "%s"' % d)
            logger.info('creating "%s"...' % d)
            os.makedirs(d)
        serializer.serialize_model_to_file(path,
                                           self._model,
                                           base_uri=base_uri)
        logger.info('done.')

        if gzipped_path:
            self._gzip(path, gzipped_path)
            os.unlink(path)
    def _serialize_stream(self, stream, format):
        temp = model_from_uri('memory')
        temp.add_statements(stream)
        serializer = RDF.Serializer(name=format)
        for prefix, ns in self._nsmap.items():
            serializer.set_namespace(prefix, ns)

        return StringIO(serializer.serialize_model_to_string(temp))
Example #4
0
def serialize(g):
    print >>sys.stderr, 'Serializing ...',
    ser = RDF.Serializer(name='rdfxml-abbrev')
    for prefix, namespace in rdfob.NAMESPACES.iteritems():
        ser.set_namespace(prefix, namespace[''].uri)
    s = ser.serialize_model_to_string(g)
    print >>sys.stderr, 'done'
    return s
Example #5
0
def serialize(model, ns, filename, format):
    if format is None:
        format = "turtle"

    serializer = RDF.Serializer(name=format)

    for prefix in ns:
        serializer.set_namespace(prefix, RDF.Uri(ns[prefix]))

    serializer.serialize_model_to_file(filename, model)
Example #6
0
    def _turtle_to_ntriples(self, data):
        # Turtle syntax is not supported by allegro graph
        # HACK workaround using redland
        import RDF
        model = RDF.Model()
        parser = RDF.TurtleParser()
        try:
            parser.parse_string_into_model(model, data.read(), '-')
        except RDF.RedlandError as err:
            raise TripleStoreError(err)

        serializer = RDF.Serializer(name='ntriples')
        return StringIO(serializer.serialize_model_to_string(model))
Example #7
0
    def _rdfxml_to_ntriples(self, data):
        # Ntriples syntax is not supported by allegro graph
        # as a result format for SPARQL Construct Queries
        # HACK workaround using redland
        import RDF
        model = RDF.Model()
        parser = RDF.Parser()
        try:
            parser.parse_string_into_model(model, data.read(), '-')
        except RDF.RedlandError as err:
            raise TripleStoreError(err)

        serializer = RDF.Serializer(name='ntriples')
        return StringIO(serializer.serialize_model_to_string(model))
Example #8
0
def updateVoIDFile(to):
    """Updates a VoID file with a new last-modified value.

    Note: The filepath of the VoID file is accessed by a global variable.

    The resulting VoID file should look something like this:

    @prefix void: <http://rdfs.org/ns/void#> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix dcterms: <http://purl.org/dc/terms/> .
    @prefix d1lod: <http://dataone.org/> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

    d1lod:d1lod a void:Dataset ;
      void:feature d1lod:fulldump ;
      dcterms:modified "2015-11-01";
      void:dataDump d1lod:d1lod.ttl .
    """

    # Create the VoID RDF Model
    m = createVoIDModel(to)

    # Verify the size of the model as a check for its successful creation
    if m.size() != 4:
        logging.error(
            "The VoID model that was created was the wronng size (%d, not %d).",
            m.size(), 4)
        return

    # Create a serializer
    s = RDF.Serializer(name="turtle")

    # Add in namespaces
    void = "http://rdfs.org/ns/void#"
    d1lod = "http://dataone.org/"

    s.set_namespace('rdf', NAMESPACES['rdf'])
    s.set_namespace('void', void)
    s.set_namespace('dcterms', NAMESPACES['dcterms'])
    s.set_namespace('d1lod', d1lod)

    # Write to different locations depending on production or testing
    try:
        if os.path.isfile(VOID_FILEPATH):
            s.serialize_model_to_file(VOID_FILEPATH, m)
        else:
            s.serialize_model_to_file('./void.ttl', m)
    except Exception, e:
        logging.exception(e)
Example #9
0
    def voidify(self, serialize_as="ntriples"):
        model = self.update_model(RDF.Model())

        # serialize to string and return
        serializer = RDF.Serializer(name=serialize_as)
        if serialize_as == "ntriples":
            return serializer.serialize_model_to_string(void_model)

        serializer.set_namespace("void", "http://rdfs.org/ns/void#")
        serializer.set_namespace(
            "rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
        serializer.set_namespace("qb", "http://purl.org/linked-data/cube#")
        serializer.set_namespace("xstats", "http://example.org/XStats#")
        #serializer.set_namespace("thisdataset", dataset_ns._prefix)
        return serializer.serialize_model_to_string(void_model)
Example #10
0
    def _ntriples_to_turtle(self, data):
        # Turtle syntax is not supported by allegro graph
        # HACK workaround using redland
        import RDF
        model = RDF.Model()
        parser = RDF.Parser('ntriples')
        data = data.read()
        data = (data.strip() + '\n')
        try:
            parser.parse_string_into_model(model, data, '-')
        except RDF.RedlandError as err:
            raise TripleStoreError(err)

        serializer = RDF.Serializer(name='turtle')
        for prefix, ns in self._nsmap.items():
            serializer.set_namespace(prefix, ns)
        return StringIO(serializer.serialize_model_to_string(model))
Example #11
0
    def save(self):
        """
		 Saves the pass process modell to a file using the given file path. If no file
		 path is given, an error is thrown.

		@return  :
		@author
		"""
        if (self._filePath is not None):
            #Create the needed datatypes (storage and model) for rdf
            storage = RDF.MemoryStorage()
            if (storage is None):
                raise Exception(
                    "Failed to create storage for saving the model!")
            #The model
            model = RDF.Model(storage)
            if (model is None):
                raise Exception("Failed to creat a new RDF model for saving!")

            #Now request all statements from the resources and add them to the model
            for resource in self.resources:
                for statement in resource.serialize():
                    model.add_statement(statement)

            #Now serialize to file
            #Get right type of serializer depending on the ending
            fileExtension = os.path.splitext(self._filePath)[-1]
            name = None
            mimeType = None
            if (fileExtension == ".nt"):
                name = "ntriples"
            else:
                #By default use the default serialize
                pass
            #Now serialize
            serializer = RDF.Serializer(name=name, mime_type=mimeType)
            serializer.serialize_model_to_file(self.filePath, model)
            print(("INFO! Saving model to file \"" + self.filePath +
                   "\" was successfull!"))
        else:
            #No file path is currently set
            raise Exception(
                "Cannot save a file directly that has never been saved before. Use \"saveAs(self, filePath)\" instead "
            )
Example #12
0
    def __init__(self, namespaces={}):
        # Initialize variables
        name = "db-{0!s}".format(random.randint(1, 999999))
        self.db = RDF.Storage(storage_name="hashes",
                              name=name,
                              options_string="hash-type='bdb'")
        self.model = RDF.Model(self.db)
        self.serializer = RDF.Serializer(name="turtle")
        self.ns = {}

        # Define basic namespaces
        basicNamespaces = {
            "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
            "xsd": "http://www.w3.org/2001/XMLSchema#",
        }
        # Extend basic namespaces with those provided in parameters
        basicNamespaces.update(namespaces)
        self.addNamespaces(basicNamespaces)
        atexit.register(self.clearTempFiles)
Example #13
0
    def export_volume(self, volume_urn, fd):
        """ Serialize a suitable properties file for the
        volume_urn. We include all the objects which are contained in
        the volume.
        """
        storage = RDF.Storage(storage_name='hashes',
                              name='X',
                              options_string="new='yes',hash-type='memory',dir='.'")
        model = RDF.Model(storage)
        
        for urn in aff4.oracle.resolve_list(volume_urn, AFF4_CONTAINS):
            try:
                urn_id = self.get_id_by_urn(urn)
            except ValueError: continue
            self.export_model(urn, model)

        self.export_model(volume_urn, model)
        serializer = RDF.Serializer("turtle")
        fd.write(serializer.serialize_model_to_string(model))
Example #14
0
def serialize(graph, format='turtle'):
    # TODO this is not quite so simple ...
    # it would see that we can't just give it a list of statements ...
    def r_to_l(element):
        if isinstance(element, rdflib.URIRef):
            RDF.Uri(element)
        elif isinstance(element, rdflib.Literal):
            if element.datatype:
                kwargs = dict(datatype=RDF.Uri(element.datatype))
            else:
                kwargs = dict(language=element.language)
            RDF.Node(literal=str(element), **kwargs)
        elif isinstance(element, rdflib.BNode):
            RDF.Node(blank=str(element))

    gen = (RDF.Statement(*(r_to_l(e) for e in t)) for t in graph)
    stream = Stream(gen)
    ser = RDF.Serializer(name=format)
    string = ser.serialize_stream_to_string(stream)
    return string
Example #15
0
 def get_serializer(self):
     serializer = RDF.Serializer(name=self.serialize_as)
     serializer.set_namespace("rdf", self.namespaces.get_namespace('rdf'))
     serializer.set_namespace("void", self.namespaces.get_namespace('void'))
     serializer.set_namespace("void-ext",
                              self.namespaces.get_namespace('void_ext'))
     serializer.set_namespace("qb", self.namespaces.get_namespace('qb'))
     serializer.set_namespace("dcterms",
                              self.namespaces.get_namespace('dcterms'))
     serializer.set_namespace("ls-void",
                              self.namespaces.get_namespace('ls_void'))
     serializer.set_namespace("ls-qb",
                              self.namespaces.get_namespace('ls_qb'))
     serializer.set_namespace("ls-cr",
                              self.namespaces.get_namespace('ls_cr'))
     serializer.set_namespace("xsd", self.namespaces.get_namespace('xsd'))
     serializer.set_namespace("xstats",
                              self.namespaces.get_namespace('stats'))
     serializer.set_namespace("foaf", self.namespaces.get_namespace('foaf'))
     serializer.set_namespace("rdfs", self.namespaces.get_namespace('rdfs'))
     return serializer
Example #16
0
import ckan.model as model
import pylons
import RDF
import re
import urllib



root = 'http://localhost:5000/'
path_to_dataset = 'dataset/'
path_to_user = '******'
path_to_subscription = 'subscription/'
serializer = RDF.Serializer(name='ntriples')

            
def dataset_to_uri(dataset_name):
    return root + path_to_dataset + dataset_name
    
          
def user_to_uri(user_name):
    return root + path_to_user + user_name        

          
def subscription_to_uri(user_name, subscription_name):
    return root + path_to_user + user_name + '/' + path_to_subscription + urllib.quote(subscription_name)


def user_id_to_object(user_id):
    return model.Session.query(model.User).get(user_id)

        serializer = RDF.Serializer(name='ntriples')
        return StringIO(serializer.serialize_model_to_string(model))

    def _rdfxml_to_ntriples(self, data):
        # Ntriples syntax is not supported by allegro graph
        # as a result format for SPARQL Construct Queries
        # HACK workaround using redland
        import RDF
        model = RDF.Model()
        parser = RDF.Parser()
        try:
            parser.parse_string_into_model(model, data.read(), '-')
        except RDF.RedlandError, err:
            raise TripleStoreError(err)

        serializer = RDF.Serializer(name='ntriples')
        return StringIO(serializer.serialize_model_to_string(model))

    def _ntriples_to_turtle(self, data):
        # Turtle syntax is not supported by allegro graph
        # HACK workaround using redland
        import RDF
        model = RDF.Model()
        parser = RDF.Parser('ntriples')
        data = data.read()
        data = (data.strip() + '\n')
        try:
            parser.parse_string_into_model(model, data, '-')
        except RDF.RedlandError, err:
            raise TripleStoreError(err)
Example #18
0
    if opt in ("-i", "--input"):
        inputPath = arg
    elif opt in ("-o", "--output"):
        outputUri = arg
    elif opt in ("-b", "--baseUri"):
        baseUri = arg
    elif opt in ("-h", "--help"):
        help()
        sys.exit(0)

sys.stderr.write("Input: " + inputPath + "\n")

model = RDF.Model()

addFiles(model, baseUri, inputPath)

ttlSerializer = RDF.Serializer(name="turtle")
ttlSerializer.set_namespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#")
ttlSerializer.set_namespace("foaf", "http://xmlns.com/foaf/0.1/")
ttlSerializer.set_namespace("sysont", "http://ns.ontowiki.net/SysOnt/")
ttlSerializer.set_namespace("site", "http://ns.ontowiki.net/SysOnt/Site/")
string = ttlSerializer.serialize_model_to_string(model, baseUri)

if (outputUri):
    outFile = open(outputUri, "w")
else:
    outFile = sys.stdout
outFile.write(string)

sys.stderr.write("done\n")
Example #19
0
    count = count + 1

print "Parsing added", count, "statements"

print "Printing all statements"
for s in model.as_stream():
    print "Statement:", s

q = RDF.Query(
    "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?a ?c WHERE {?a dc:title ?c}"
)
print "Querying for dc:titles:"
for result in q.execute(model):
    print "{"
    for k in result:
        print "  " + k + " = " + str(result[k])
    print "}"

print "Writing model to test-out.rdf as rdf/xml"

# Use any rdf/xml parser that is available
serializer = RDF.Serializer()
serializer.set_namespace("dc", RDF.Uri("http://purl.org/dc/elements/1.1/"))
serializer.serialize_model_to_file("test-out.rdf", model)

print "Serialized to ntriples as a string size", len(
    model.to_string(name="ntriples",
                    base_uri="http://example.org/base#")), "bytes"

print "Done"