Ejemplo n.º 1
0
 def __init__(self,model,item,tag=None):
     rss = RDF.NS("http://purl.org/rss/1.0/")
     dc = RDF.NS("http://purl.org/dc/elements/1.1/")
     self.description = str(model.get_target(item,rss.title))
     self.extended = str(model.get_target(item,rss.description))
     self.user = User(str(model.get_target(item,dc.creator)))
     dcdate = model.get_target(item,dc.date)
     if dcdate is not None:
         self.time = xml.utils.iso8601.parse(str(dcdate))
     self.href = Href(str(item.uri))
     subject = str(model.get_target(item,dc.subject))
     self.tags = Set([])
     if subject is not None:
         self.tags = Set([Tag(x) for x in subject.split(" ")])
     if tag is not None:
         self.tags.add(tag)
Ejemplo n.º 2
0
 def addNamespaces(self, namespaces):
     for namespace in namespaces:
         if not self.ns.has_key(namespace):
             if type(namespaces[namespace]) == RDF.Uri:
                 namespaces[namespace] = str(namespaces[namespace])
             if isinstance(namespaces[namespace], RDF.NS):
                 namespaces[namespace] = namespaces[namespace]._prefix
             self.ns[namespace] = RDF.NS(namespaces[namespace])
             self.serializer.set_namespace(namespace, namespaces[namespace])
Ejemplo n.º 3
0
    def __init__(self,
                 vocabfile,
                 prefix,
                 uri,
                 templatefile,
                 outputfile,
                 instances=False):
        self.classranges = {}
        self.classdomains = {}
        self.spec_url = None
        self.spec_ns = None
        self.ns_list = {
            "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
            "http://www.w3.org/2000/01/rdf-schema#": "rdfs",
            "http://www.w3.org/2002/07/owl#": "owl",
            "http://www.w3.org/2001/XMLSchema#": "xsd",
            "http://rdfs.org/sioc/ns#": "sioc",
            "http://xmlns.com/foaf/0.1/": "foaf",
            "http://purl.org/dc/elements/1.1/": "dc",
            "http://purl.org/dc/terms/": "dct",
            "http://usefulinc.com/ns/doap#": "doap",
            "http://www.w3.org/2003/06/sw-vocab-status/ns#": "status",
            "http://purl.org/rss/1.0/modules/content/": "content",
            "http://www.w3.org/2003/01/geo/wgs84_pos#": "geo",
            "http://www.w3.org/2004/02/skos/core#": "skos"
        }

        self.rdf = RDF.NS('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
        self.rdfs = RDF.NS('http://www.w3.org/2000/01/rdf-schema#')
        self.owl = RDF.NS('http://www.w3.org/2002/07/owl#')
        self.dc = RDF.NS('http://purl.org/dc/elements/1.1/')
        self.dct = RDF.NS('http://purl.org/dc/terms/')
        self.skos = RDF.NS('http://www.w3.org/2004/02/skos/core#')
        self.vann = RDF.NS('http://purl.org/vocab/vann/')
        self.vs = RDF.NS('http://www.w3.org/2003/06/sw-vocab-status/ns#')

        self.termdir = './doc'  #TODO

        #ontology
        self.specloc = "file:" + vocabfile
        self.spec_pre = prefix
        self.ns_url = uri
        if (self.ns_url[-1] != "/" and self.ns_url[-1] != "#"):
            self.ns_url += "#"
        #template
        self.temploc = templatefile
        self.template = None
        try:
            f = open(self.temploc, "r")
            self.template = f.read()
        except Exception, e:
            raise Usage("Error reading from template %s: %s" %
                        (self.temploc, str(e)))
Ejemplo n.º 4
0
 def getInstances(self, model, classes, properties):
     """
     Extract all resources instanced in the ontology
     (aka "everything that is not a class or a property")
     """
     instances = []
     for one in classes:
         for i in model.find_statements(
                 RDF.Statement(None, self.rdf.type, self.spec_ns[one])):
             uri = str(i.subject.uri)
             if not uri in instances:
                 instances.append(uri)
     if not instances:
         for one in classes:
             for i in model.find_statements(
                     RDF.Statement(None, self.rdf.type,
                                   RDF.NS(self.ns_url)[one])):
                 uri = str(i.subject.uri)
                 if not uri in instances:
                     instances.append(uri)
     count = len(instances)
     for i in model.find_statements(
             RDF.Statement(None, self.rdfs.isDefinedBy,
                           RDF.Uri(self.spec_url))):
         uri = str(i.subject.uri)
         #if (uri.startswith(self.spec_url)):
         #    uri = uri[len(self.spec_url):]
         if ((not uri in instances) and (not uri in classes)):
             instances.append(uri)
     if len(instances) == count:
         for i in model.find_statements(
                 RDF.Statement(None, self.rdfs.isDefinedBy,
                               RDF.Uri(self.ns_url))):
             uri = str(i.subject.uri)
             #if uri.startswith(self.ns_url):
             #    uri = uri[len(self.ns_url):]
             if ((not uri in instances) and (not uri in classes)):
                 instances.append(uri)
     return instances
Ejemplo n.º 5
0
    def update_model(self, dataset_uri, rdf_model):
        """present stats in VoID (http://www.w3.org/TR/void/)"""
        results = self.stats_results
        void_model = rdf_model
        ns_void = RDF.NS("http://rdfs.org/ns/void#")
        ns_rdf = RDF.NS("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
        ns_xs = RDF.NS("http://www.w3.org/2001/XMLSchema#")
        ns_qb = RDF.NS("http://purl.org/linked-data/cube#")
        ns_stats = RDF.NS("http://example.org/XStats#")
        # FIXME?: our dataset
        #dataset = RDF.Uri(self.url)
        dataset = dataset_uri
        dataset_ns = RDF.NS("%s#" % dataset.__unicode__())
        # we're talking about datasets here
        void_model.append(RDF.Statement(dataset, ns_rdf.type, ns_void.Dataset))

        # basic stuff: no of triples, ...
        result_node = RDF.Node(literal=str(self.no_of_statements),
                               datatype=ns_xs.integer.uri)
        void_model.append(RDF.Statement(dataset, ns_void.triples, result_node))

        # void:observation extension stuff
        void_model.append(
            RDF.Statement(ns_stats.value, ns_rdf.type, ns_qb.MeasureProperty))
        void_model.append(
            RDF.Statement(ns_stats.subjectsOfType, ns_rdf.type,
                          ns_qb.DimensonProperty))
        void_model.append(
            RDF.Statement(ns_stats.schema, ns_rdf.type,
                          ns_qb.AttributeProperty))

        # voidify results from custom stats
        for stat in custom_stats.stats_to_do:
            stat.voidify(void_model, dataset)

        return void_model
Ejemplo n.º 6
0
import sys
import re
import urllib
import RDF

foaf = RDF.NS("http://xmlns.com/foaf/0.1/")
dc = RDF.NS('http://purl.org/dc/elements/1.1/')
rdf = RDF.NS('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
rdfs = RDF.NS('http://www.w3.org/2000/01/rdf-schema#')
owl = RDF.NS('http://www.w3.org/2002/07/owl#')
vs = RDF.NS('http://www.w3.org/2003/06/sw-vocab-status/ns#')

classranges = {}
classdomains = {}


def termlink(string):
    """FOAF specific: function which replaces <code>foaf:*</code> with a 
    link to the term in the document."""
    return re.sub(r"<code>foaf:(\w+)</code>",
                  r"""<code><a href="#term_\1">foaf:\1</a></code>""", string)


def return_name(m, urinode):
    "Trims the FOAF namespace out of a term to give a name to the term."
    return str(urinode.uri).replace("http://xmlns.com/foaf/0.1/", "")


def get_rdfs(m, urinode):
    "Returns label and comment given an RDF.Node with a URI in it"
    l = m.find_statements(RDF.Statement(urinode, rdfs.label, None))
Ejemplo n.º 7
0
#!/usr/bin/env python

import sys
import os
import subprocess
from cStringIO import StringIO
import RDF
import lxml.etree, lxml.html
from itertools import chain

import rdfob
import citations

RDF_NS = RDF.NS('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
RDFS_NS = RDF.NS('http://www.w3.org/2000/01/rdf-schema#')
OWL_NS = RDF.NS('http://www.w3.org/2002/07/owl#')
DCTERMS_NS = RDF.NS('http://purl.org/dc/terms/')
PRISM_NS = RDF.NS('http://prismstandard.org/namespaces/1.2/basic/')
FOAF_NS = RDF.NS('http://xmlns.com/foaf/0.1/')
MHS_NS = RDF.NS('http://miskinhill.com.au/rdfschema/1.0/')

def load():
    g = RDF.Model()
    for dirpath, dirnames, filenames in os.walk(os.getcwd()):
        for filename in filenames:
            if filename.endswith('.nt') or filename.endswith('.ttl'):
                print >>sys.stderr, 'Loading %s ...' % os.path.join(dirpath, filename),
                g.load('file:' + os.path.join(dirpath, filename))
                print >>sys.stderr, '%d triples' % len(g)
    return g
Ejemplo n.º 8
0
    'http://www.w3.org/2000/01/rdf-schema#': "rdfs",
    'http://www.w3.org/2002/07/owl#': "owl",
    'http://www.w3.org/2001/XMLSchema#': 'xsd',
    'http://rdfs.org/sioc/ns#': "sioc",
    'http://xmlns.com/foaf/0.1/': "foaf",
    'http://purl.org/dc/elements/1.1/': "dc",
    'http://purl.org/dc/terms/': "dcterms",
    'http://usefulinc.com/ns/doap#': 'doap',
    'http://www.w3.org/2003/06/sw-vocab-status/ns#': "status",
    'http://purl.org/rss/1.0/modules/content/': "content",
    'http://www.w3.org/2003/01/geo/wgs84_pos#': "geo",
    'http://www.w3.org/2004/02/skos/core#': "skos",
    'http://scot-project.org/scot/ns#': "scot"
}

rdf = RDF.NS('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
rdfs = RDF.NS('http://www.w3.org/2000/01/rdf-schema#')
owl = RDF.NS('http://www.w3.org/2002/07/owl#')
vs = RDF.NS('http://www.w3.org/2003/06/sw-vocab-status/ns#')

termdir = './doc'  #TODO


def niceName(uri):
    regexp = re.compile("^(.*[/#])([^/#]+)$")
    rez = regexp.search(uri)
    pref = rez.group(1)
    #return ns_list.get(pref, pref) + ":" + rez.group(2)
    if ns_list.has_key(pref):
        return ns_list.get(pref, pref) + ":" + rez.group(2)
    else:
Ejemplo n.º 9
0
        return instances

    def specgen(self, mode="spec"):
        """The meat and potatoes: Everything starts here."""

        m = RDF.Model()
        p = RDF.Parser()
        try:
            p.parse_into_model(m, self.specloc)
        except IOError, e:
            raise Usage("Error reading from ontology: %s" % str(e))
        except RDF.RedlandError, e:
            raise Usage("Error parsing the ontology: %s" % str(e))

        self.spec_url = self.getOntologyNS(m)
        self.spec_ns = RDF.NS(self.spec_url)
        self.ns_list[self.spec_url] = self.spec_pre
        self.ns_list[self.ns_url] = self.spec_pre

        classlist, proplist = self.specInformation(m, self.spec_url)
        classlist = sorted(classlist)
        proplist = sorted(proplist)

        instalist = None
        if self.instances:
            instalist = self.getInstances(m, classlist, proplist)
            instalist.sort(lambda x, y: cmp(
                self.getShortName(x).lower(),
                self.getShortName(y).lower()))
        if mode == "spec":
            # Build HTML list of terms.
Ejemplo n.º 10
0
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

__all__ = ['main']

import sys, time, re, urllib, getopt
import RDF

foaf = RDF.NS("http://xmlns.com/foaf/0.1/")
dc = RDF.NS('http://purl.org/dc/elements/1.1/')
rdf = RDF.NS('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
rdfs = RDF.NS('http://www.w3.org/2000/01/rdf-schema#')
owl = RDF.NS('http://www.w3.org/2002/07/owl#')
vs = RDF.NS('http://www.w3.org/2003/06/sw-vocab-status/ns#')
xfoaf = RDF.NS('http://www.foafrealm.org/xfoaf/0.1/')
mont = RDF.NS('http://www.marcont.org/ontology/marcont.owl#')
# add your namespaces here

classranges = {}
classdomains = {}

termdir = './doc'

# namespace for which the spec is being generated.
Ejemplo n.º 11
0
 def get_rdf_namespace(self, namespace):
     if (namespace in self.namespaces):
         return RDF.NS(self.namespaces[namespace])
     else:
         logger.error("Namespace %s is not defined" % namespace)
         return None
Ejemplo n.º 12
0
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

LODStats is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with LODStats.  If not, see <http://www.gnu.org/licenses/>.
"""
import RDF

# some often used namespaces
ns_xs = RDF.NS("http://www.w3.org/2001/XMLSchema#")
ns_void = RDF.NS("http://rdfs.org/ns/void#")
ns_rdf = RDF.NS("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
ns_stats = RDF.NS("http://example.org/XStats#")
ns_qb = RDF.NS("http://purl.org/linked-data/cube#")


def get_namespace(uri):
    '''extract namespace from uris'''
    # FIXME: just get those using http for now
    if not uri.startswith('http://'):
        return None
    uri_no_http = uri[len('http://'):]
    for sep in ['#', ':', '/']:
        split_uri = uri_no_http.rsplit(sep, 1)
        if len(split_uri) == 2:
Ejemplo n.º 13
0
import RDF

ns = {
  'dc': RDF.NS('http://purl.org/dc/elements/1.1/'),
  'foaf': RDF.NS('http://xmlns.com/foaf/0.1/'),
  'frbr': RDF.NS('http://purl.org/vocab/frbr/core#'),
  'rdf': RDF.NS('http://www.w3.org/1999/02/22-rdf-syntax-ns#'),
  'nao': RDF.NS('http://www.semanticdesktop.org/ontologies/2007/08/15/nao#'),
  # original URL has been removed (MusicBrainz noooooo :()
  # described at <http://www.schemaweb.info/schema/SchemaInfo.aspx?id=168>
  'mm': RDF.NS('http://musicbrainz.org/mm/mm-2.1#'),
  'mo': RDF.NS('http://purl.org/ontology/mo/'),
  # need this to get xs:int track_numbers working
  'xs': RDF.NS('http://www.w3.org/2001/XMLSchema#'),
}

import TripleStore

def resource_is_type(s, t):
  return TripleStore.model.get_target(s, ns['rdf'].type) == t

def rating(resource):
  '''a resource's nao:numericRating'''
  rating = TripleStore.model.get_target(resource, ns['nao'].numericRating)

  if rating is None:
    return 0

  return int(rating.literal_value['string'])

def rate(resource, rating):