Exemplo n.º 1
0
 def validate(self, uri_fingerprint, require_hex_fpr=1):
     """validate the format of the document"""
     
     FOAF = Namespace("http://xmlns.com/foaf/0.1/")
     TRUST = Namespace("http://www.konfidi.org/ns/trust/1.2#")
     WOT = Namespace("http://xmlns.com/wot/0.1/")
     RDF = Namespace("http://www.w3.org/2000/01/rdf-schema#")
     store = TripleStore()
 
     # TODO: verify all <truster>s have fingerprints
     # and that they're all the same
     try:
         store.parse(StringInputSource(self.content))
     except SAXParseException:
         raise FOAFServerError, "invalid XML: " + str(sys.exc_info()[1])
     
     fingerprint = last_fingerprint = None
     for (relationship, truster) in store.subject_objects(TRUST["truster"]):
         for (key) in store.objects(truster, WOT["hasKey"]):
             fingerprint = store.objects(key, WOT["fingerprint"]).next()
             if last_fingerprint:
                 if fingerprint != last_fingerprint:
                     raise FOAFServerError, "All wot:fingerprint's from trust:truster's PubKeys must be the same.  Found '%s' and '%s'" % (fingerprint, last_fingerprint)
             last_fingerprint = fingerprint
     
     # per http://xmlns.com/wot/0.1/, we really shouldn't replace these
     fingerprint = fingerprint.replace(" ", "")
     fingerprint = fingerprint.replace(":", "")
     if require_hex_fpr and not(ishex(fingerprint)):
         raise FOAFServerError, "Invalid fingerprint format; must be hex"
     
     if uri_fingerprint and uri_fingerprint != fingerprint:
         raise FOAFServerError, "URI fingerprint doesn't match FOAF fingerprint"
         
     return fingerprint
Exemplo n.º 2
0
class rdfStore:
    """
    Provides RDF parsing and output functions for CC license and work
    definitions.
    """
    
    def __init__(self):
        # initialize the TripleStore for managing RDF
        self.store = TripleStore()
        
    def parse(self, rdf):
        """
        Parse the given String, rdf, into it's component triples.
        """

        self.store.parse( StringInputSource (rdf) )

    def subjects(self):
        """A generator which successivly returns each subject contained in
        the store, wrapped in an instance of rdfDict."""

        for subject in self.store.subjects():
            yield rdfDict(subject, store=self.store)
        
    def output(self):
        """
        Return a string containing the RDF representation of the
        licenses and works."""

        if self.store is not None:
            rdf = cStringIO.StringIO()
            self.store.serialize(stream=rdf)
            return rdf.getvalue()
        else:
            return ""

    __str__ = output

    def append(self, newItem):
        """
        Adds a new work or license to the RDF store.
        """

        # make sure the stores aren't the same
        if (newItem.store is not self.store):

            # add each triple from the newItem's store to this store
            for triple in newItem.store.triples():
                self.store.add(triple)
Exemplo n.º 3
0
    'http://www.w3.org/2001/XMLSchema#positiveInteger': (int, unicode),
    'http://www.w3.org/2001/XMLSchema#short': (int, unicode),
    'http://www.w3.org/2001/XMLSchema#unsignedInt': (long, unicode),
    'http://www.w3.org/2001/XMLSchema#byte': (int, unicode),
    'http://www.w3.org/2001/XMLSchema#unsignedShort': (int, unicode),
    'http://www.w3.org/2001/XMLSchema#unsignedByte': (int, unicode),
    'http://www.w3.org/2001/XMLSchema#float': (float, unicode),
    'http://www.w3.org/2001/XMLSchema#double': (float, unicode),  # doesn't do the whole range
    #    duration
    #    dateTime
    #    time
    #    date
    #    gYearMonth
    #    gYear
    #    gMonthDay
    #    gDay
    #    gMonth
    #    hexBinary
    'http://www.w3.org/2001/XMLSchema#base64Binary': (base64.decodestring, lambda i:base64.encodestring(i)[:-1]),
    'http://www.w3.org/2001/XMLSchema#anyURI': (str, str),
}

if __name__ == '__main__':
    # use: "python -i sparta.py [URI for RDF file]+"
    from rdflib.TripleStore import TripleStore
    import sys
    mystore = TripleStore()
    for arg in sys.argv[1:]:
        mystore.parse(arg)
    thing = ThingFactory(mystore)
Exemplo n.º 4
0
    'http://www.w3.org/2001/XMLSchema#short': (int, unicode),
    'http://www.w3.org/2001/XMLSchema#unsignedInt': (long, unicode),
    'http://www.w3.org/2001/XMLSchema#byte': (int, unicode),
    'http://www.w3.org/2001/XMLSchema#unsignedShort': (int, unicode),
    'http://www.w3.org/2001/XMLSchema#unsignedByte': (int, unicode),
    'http://www.w3.org/2001/XMLSchema#float': (float, unicode),
    'http://www.w3.org/2001/XMLSchema#double': (float, unicode),  # doesn't do the whole range
#    duration
#    dateTime
#    time
#    date
#    gYearMonth
#    gYear
#    gMonthDay
#    gDay
#    gMonth
#    hexBinary
    'http://www.w3.org/2001/XMLSchema#base64Binary': (base64.decodestring, lambda i:base64.encodestring(i)[:-1]),
    'http://www.w3.org/2001/XMLSchema#anyURI': (str, str),
}


if __name__ == '__main__':
    # use: "python -i sparta.py [URI for RDF file]+"
    from rdflib.TripleStore import TripleStore
    import sys
    mystore = TripleStore()
    for arg in sys.argv[1:]:
        mystore.parse(arg)
    thing = ThingFactory(mystore)
if not form.has_key('description'):
    print 'Content-Type: text/html\r'
    print '\r'
    print "<H1>Error</H1>"
    print "Please fill in the 'description' field with an appropriate Turtle graph."

else:
    tmpfile = mkstemp()[1]
    data = convert(form['description'].value,
                   'turtle',
                   'ntriples',
                   infile=tmpfile)

    graph = TripleStore()
    graph.parse(StringIO(data), format='nt')

    thisURI = URIRef('http://%s%s' %
                     (os.environ['HTTP_HOST'], os.environ['REQUEST_URI']))

    graphNode = URIRef('file://%s' % tmpfile)

    FOAF = Namespace("http://xmlns.com/foaf/0.1/")

    try:
        bnode = graph.objects(graphNode, FOAF["primaryTopic"]).next()
    except StopIteration:
        print 'Content-Type: text/plain\r'
        print '\r'
        print 'no primary topic given'