def __init__(self):
     Markdown.setPre("http://schema.org/")
     self.typesCount = self.propsCount = self.namedCount = 0
     self.openFile()
     self.createDom()
     self.setSkips()
     self.getGraphs()
     self.loadGraphs()
     self.cleanup()
     self.closeFile()
Exemplo n.º 2
0
 def __init__(self):
     Markdown.setPre("http://schema.org/")
     self.typesCount = self.propsCount = self.namedCount = 0
     self.openFile()
     self.createDom()
     self.setSkips()
     self.getGraphs()
     self.loadGraphs()
     self.cleanup()
     self.closeFile()
Exemplo n.º 3
0
 def getCSVComment(self, term=None, graph=None):
     query = '''select ?com where{
      <%s> rdfs:comment ?com.
     }''' % term
     res = self.doQuery(graph, query)
     ret = ', '.join([x.com for x in res])
     #print "SUBTYPES of %s: '%s'" % (term,ret)
     if self.markdown:
         Markdown.setPre(api.SdoConfig.vocabUri())
         ret = Markdown.parse(ret)
         Markdown.setPre()
     return ret.replace('\n', '')
Exemplo n.º 4
0
 def getCSVComment(self, term=None, graph=None):
     query = '''select ?com where{
      <%s> rdfs:comment ?com.
     }''' % term
     res = self.doQuery(graph, query)
     ret = ', '.join([x.com for x in res])
     #print "SUBTYPES of %s: '%s'" % (term,ret)
     if self.markdown:
         Markdown.setPre("http://schema.org/")
         ret = Markdown.parse(ret)
         Markdown.setPre()
     return ret
Exemplo n.º 5
0
 def getCSVComment(self,term=None,graph=None):
     query='''select ?com where{
      <%s> rdfs:comment ?com.
     }''' % term
     res = self.doQuery(graph,query)
     ret = ', '.join([x.com for x in res])
     #print "SUBTYPES of %s: '%s'" % (term,ret)
     if self.markdown:
         Markdown.setPre(api.SdoConfig.vocabUri())
         ret = Markdown.parse(ret)
         Markdown.setPre()
     return ret
Exemplo n.º 6
0
Arquivo: api.py Projeto: dbs/schemaorg
def GetComment(node, layers='core') : 
    """Get the first rdfs:comment we find on this node (or "No comment"), within any of the specified layers."""
    tx = GetComments(node, layers)
    if len(tx) > 0:
            return Markdown.parse(tx[0])
    else:
        return "No comment"
    def outputType(self, uri, graph):
        self.typesCount += 1

        typ = SubElement(self.dom, "owl:Class")
        typ.set("rdf:about", uri)
        ext = None
        for (p, o) in graph.predicate_objects(uri):
            if p == RDFS.label:
                l = SubElement(typ, "rdfs:label")
                l.set("xml:lang", "en")
                l.text = o
            elif p == RDFS.comment:
                c = SubElement(typ, "rdfs:comment")
                c.set("xml:lang", "en")
                c.text = Markdown.parse(o)
            elif p == RDFS.subClassOf:
                s = SubElement(typ, "rdfs:subClassOf")
                s.set("rdf:resource", o)
            elif p == URIRef(
                    "http://schema.org/isPartOf"):  #Defined in an extension
                ext = str(o)
            elif p == RDF.type and o == URIRef(
                    "http://schema.org/DataType"):  #A datatype
                s = SubElement(typ, "rdfs:subClassOf")
                s.set("rdf:resource", "http://schema.org/DataType")

        typ.append(self.addDefined(uri, ext))
Exemplo n.º 8
0
def GetComment(node, layers='core'):
    """Get the first rdfs:comment we find on this node (or "No comment"), within any of the specified layers."""
    tx = GetComments(node, layers)
    if len(tx) > 0:
        return Markdown.parse(tx[0])
    else:
        return "No comment"
Exemplo n.º 9
0
 def outputType(self, uri, graph):
     self.typesCount += 1
     
     typ = SubElement(self.dom,"owl:Class")
     typ.set("rdf:about",uri)
     ext = None
     for (p,o) in graph.predicate_objects(uri):
         if p == RDFS.label:
             l = SubElement(typ,"rdfs:label")
             l.set("xml:lang","en")
             l.text = o
         elif p == RDFS.comment:
             c = SubElement(typ,"rdfs:comment")
             c.set("xml:lang","en")
             c.text = Markdown.parse(o)
         elif p == RDFS.subClassOf:
             s = SubElement(typ,"rdfs:subClassOf")
             s.set("rdf:resource",o)
         elif p == URIRef("http://schema.org/isPartOf"): #Defined in an extension
             ext = str(o)
         elif p == RDF.type and o == URIRef("http://schema.org/DataType"): #A datatype
             s = SubElement(typ,"rdfs:subClassOf")
             s.set("rdf:resource","http://schema.org/DataType")
     
     typ.append(self.addDefined(uri,ext))
Exemplo n.º 10
0
 def test_emph(self):
     markstring = "This is _em_, __strong__, ___strong em___"
     html = Markdown.parse(markstring, True)
     self.assertFalse(
         html !=
         "<p>This is <em>em</em>, <strong>strong</strong>, <strong><em>strong em</em></strong></p>\n",
         "Markdown string not formatted correctly")
Exemplo n.º 11
0
 def loadComment(self):
     comments = self.getComments()
     wpre = None
     name = self.getId()
     if name.startswith("http"): #Wikilinks in markdown default to current site - extermals need overriding
         val = os.path.basename(name)
         wpre = name[:len(name) - len(val)]
     
     first = True
     buf = sdoStringIO()
     for com in comments:
         if not first:
             buf.write(" ")
         else:
             first = False
         buf.write(Markdown.parse(com,wpre=wpre))
     ret = buf.getvalue()
     if not len(ret):
         ret = "-"
     self.comment = ret
Exemplo n.º 12
0
 def loadComment(self):
     comments = self.getComments()
     wpre = None
     name = self.getId()
     if name.startswith("http"): #Wikilinks in markdown default to current site - extermals need overriding
         val = os.path.basename(name)
         wpre = name[:len(name) - len(val)]
     
     first = True
     buf = sdoStringIO()
     for com in comments:
         if not first:
             buf.write(" ")
         else:
             first = False
         buf.write(Markdown.parse(com,wpre=wpre))
     ret = buf.getvalue()
     if not len(ret):
         ret = "-"
     self.comment = ret
Exemplo n.º 13
0
    def outputNamedIndividuals(self, idividual, graph, parent=None):
        self.namedCount += 1

        typ = SubElement(self.dom, "owl:NamedIndividual")
        typ.set("rdf:about", idividual)
        ext = None
        for (p, o) in graph.predicate_objects(URIRef(idividual)):
            if p == RDFS.label:
                l = SubElement(typ, "rdfs:label")
                l.set("xml:lang", "en")
                l.text = o
            elif p == RDFS.comment:
                c = SubElement(typ, "rdfs:comment")
                c.set("xml:lang", "en")
                c.text = Markdown.parse(o)
            elif p == URIRef("http://schema.org/isPartOf"):
                ext = str(o)

        typ.append(self.addDefined(idividual, ext))

        if parent:
            s = SubElement(typ, "rdfs:subClassOf")
            s.set("rdf:resource", parent)
Exemplo n.º 14
0
    def outputNamedIndividuals(self,idividual,graph,parent=None):
        self.namedCount += 1
        
        typ = SubElement(self.dom,"owl:NamedIndividual")
        typ.set("rdf:about",idividual)
        ext = None
        for (p,o) in graph.predicate_objects(URIRef(idividual)):
            if p == RDFS.label:
                l = SubElement(typ,"rdfs:label")
                l.set("xml:lang","en")
                l.text = o
            elif p == RDFS.comment:
                c = SubElement(typ,"rdfs:comment")
                c.set("xml:lang","en")
                c.text = Markdown.parse(o)
            elif p == URIRef("http://schema.org/isPartOf"):
                ext = str(o)

        typ.append(self.addDefined(idividual,ext))

        if parent:
            s = SubElement(typ,"rdfs:subClassOf")
            s.set("rdf:resource",parent)
Exemplo n.º 15
0
    def outputProp(self, uri, graph):
        self.propsCount += 1
        children = []
        domains = {}
        ranges = []
        datatypeonly = True
        ext = None
        for (p, o) in graph.predicate_objects(uri):
            if p == RDFS.label:
                l = Element("rdfs:label")
                l.set("xml:lang", "en")
                l.text = o
                children.append(l)
            elif p == RDFS.comment:
                c = Element("rdfs:comment")
                c.set("xml:lang", "en")
                c.text = Markdown.parse(o)
                children.append(c)
            elif p == RDFS.subPropertyOf:
                sub = Element("rdfs:subPropertyOf")
                subval = str(o)
                if subval == "rdf:type":  #Fixes a special case with schema:additionalType
                    subval = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"

                sub.set("rdf:resource", subval)
                children.append(sub)
            elif p == INVERSEOF:
                sub = Element("owl:inverseOf")
                sub.set("rdf:resource", o)
                children.append(sub)
            elif p == SUPERSEDEDBY:
                sub = Element("schema:supersededBy")
                sub.set("rdf:resource", o)
                children.append(sub)
            elif p == DOMAININC:
                domains[o] = True
            elif p == RANGEINC:
                ranges.append(str(o))
                if str(o) not in DATATYPES:
                    datatypeonly = False
            elif p == URIRef("http://schema.org/isPartOf"):
                ext = str(o)

        children.append(self.addDefined(uri, ext))

        if not datatypeonly:
            for r in DEFAULTRANGES:
                if r not in ranges:
                    ranges.append(r)

        if len(domains):
            d = Element("rdfs:domain")
            children.append(d)
            cl = SubElement(d, "owl:Class")
            u = SubElement(cl, "owl:unionOf")
            u.set("rdf:parseType", "Collection")
            for target in domains.keys():
                targ = SubElement(u, "owl:Class")
                targ.set("rdf:about", target)

        if len(ranges):
            r = Element("rdfs:range")
            children.append(r)
            cl = SubElement(r, "owl:Class")
            u = SubElement(cl, "owl:unionOf")
            u.set("rdf:parseType", "Collection")
            for target in ranges:
                targ = SubElement(u, "owl:Class")
                targ.set("rdf:about", target)

        if datatypeonly:
            prop = SubElement(self.dom, "owl:DatatypeProperty")
        else:
            prop = SubElement(self.dom, "owl:ObjectProperty")
        prop.set("rdf:about", uri)
        for sub in children:
            prop.append(sub)
Exemplo n.º 16
0
    def outputProp(self,uri, graph):
        self.propsCount += 1
        children = []
        domains = {}
        ranges = []
        datatypeonly = True
        ext = None        
        for (p,o) in graph.predicate_objects(uri):
            if p == RDFS.label:
                l = Element("rdfs:label")
                l.set("xml:lang","en")
                l.text = o
                children.append(l)
            elif p == RDFS.comment:
                c = Element("rdfs:comment")
                c.set("xml:lang","en")
                c.text = Markdown.parse(o)
                children.append(c)
            elif p == RDFS.subPropertyOf:
                sub = Element("rdfs:subPropertyOf")
                subval = str(o)
                if subval == "rdf:type":  #Fixes a special case with schema:additionalType
                    subval = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
                    
                sub.set("rdf:resource",subval)
                children.append(sub)
            elif p == INVERSEOF:
                sub = Element("owl:inverseOf")
                sub.set("rdf:resource",o)
                children.append(sub)
            elif p == SUPERSEDEDBY:
                sub = Element("schema:supersededBy")
                sub.set("rdf:resource",o)
                children.append(sub)
            elif p == DOMAININC:
                domains[o] = True
            elif p == RANGEINC:
                ranges.append(str(o))
                if str(o) not in DATATYPES:
                    datatypeonly = False
            elif p == URIRef("http://schema.org/isPartOf"):
                ext = str(o)
                
        children.append(self.addDefined(uri,ext))
                
                
        if not datatypeonly:
            for r in DEFAULTRANGES:
                if r not in ranges:
                    ranges.append(r)
                
        if len(domains):
            d = Element("rdfs:domain")
            children.append(d)
            cl = SubElement(d,"owl:Class")
            u = SubElement(cl,"owl:unionOf")
            u.set("rdf:parseType","Collection")
            for target in domains.keys():
                targ = SubElement(u,"owl:Class")
                targ.set("rdf:about",target)

        if len(ranges):
            r = Element("rdfs:range")
            children.append(r)
            cl = SubElement(r,"owl:Class")
            u = SubElement(cl,"owl:unionOf")
            u.set("rdf:parseType","Collection")
            for target in ranges:
                targ = SubElement(u,"owl:Class")
                targ.set("rdf:about",target)
                
                
        if datatypeonly:
            prop = SubElement(self.dom,"owl:datatypeProperty")            
        else:
            prop = SubElement(self.dom,"owl:ObjectProperty")
        prop.set("rdf:about",uri)
        for sub in children:
            prop.append(sub)
Exemplo n.º 17
0
 def MdComments(self,g):#Process Markdown
     Markdown.setPre("http://schema.org/")
     for s,p,o in list(g.triples( (None, RDFS.comment, None) )):
         no = Markdown.parse(o)        #g.remove((s,p,o))
         g.set((s,p,Literal(no)))
     Markdown.setPre()
Exemplo n.º 18
0
def buildSingleTermGraph(node, excludeAttic=True, markdown=True):

    g = rdflib.Graph()
    g.bind('owl', 'http://www.w3.org/2002/07/owl#')
    g.bind('rdfa', 'http://www.w3.org/ns/rdfa#')
    g.bind('dct', 'http://purl.org/dc/terms/')
    g.bind('schema', 'http://schema.org/')

    full = "http://schema.org/" + node
    #n = URIRef(full)
    n = SCHEMA.term(node)
    n = n
    full = str(n)
    q = queryGraph()
    ret = None

    #log.info("NAME %s %s"% (n,full))
    atts = None
    try:
        RDFLIBLOCK.acquire()
        atts = list(
            q.triples((n, SCHEMA.isPartOf, URIRef("http://attic.schema.org"))))
    finally:
        RDFLIBLOCK.release()
    if len(atts):
        #log.info("ATTIC TERM %s" % n)
        excludeAttic = False
    #Outgoing triples
    try:
        RDFLIBLOCK.acquire()
        ret = list(q.triples((n, None, None)))
    finally:
        RDFLIBLOCK.release()
    for (s, p, o) in ret:
        #log.info("adding %s %s %s" % (s,p,o))
        g.add((s, p, o))

    #Incoming triples
    ret = list(q.triples((None, None, n)))
    for (s, p, o) in ret:
        #log.info("adding %s %s %s" % (s,p,o))
        g.add((s, p, o))

        #super classes
        query = '''select * where {
	?term (^rdfs:subClassOf*) <%s>.
	?term rdfs:subClassOf ?super.
	?super ?pred ?obj.
	}''' % n

        ret = rdfQueryStore(query, q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.term,RDFS.subClassOf,row.super))
        g.add((row.term, RDFS.subClassOf, row.super))
        g.add((row.super, row.pred, row.obj))

        #poperties with superclasses in domain
        query = '''select * where{
	?term (^rdfs:subClassOf*) <%s>.
	?prop <http://schema.org/domainIncludes> ?term.
	?prop ?pred ?obj.
	}
	''' % n
        ret = rdfQueryStore(query, q)
    for row in ret:
        g.add((row.prop, SCHEMA.domainIncludes, row.term))
        g.add((row.prop, row.pred, row.obj))

        #super properties
        query = '''select * where {
	?term (^rdfs:subPropertyOf*) <%s>.
	?term rdfs:subPropertyOf ?super.
	?super ?pred ?obj.
	}''' % n
        ret = rdfQueryStore(query, q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.term,RDFS.subPropertyOf,row.super))
        g.add((row.term, RDFS.subPropertyOf, row.super))
        g.add((row.super, row.pred, row.obj))

        #Enumeration for an enumeration value
        query = '''select * where {
	<%s> a ?type.
	?type ?pred ?obj.
	FILTER NOT EXISTS{?type a rdfs:class}.
	}''' % n
        ret = rdfQueryStore(query, q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.type,row.pred,row.obj))
        g.add((row.type, row.pred, row.obj))

    if excludeAttic:  #Remove triples referencing terms part of http://attic.schema.org
        trips = list(g.triples((None, None, None)))
        try:
            RDFLIBLOCK.acquire()
            for (s, p, o) in trips:
                atts = list(
                    q.triples((s, SCHEMA.isPartOf,
                               URIRef("http://attic.schema.org"))))
                if isinstance(o, URIRef):
                    atts.extend(
                        q.triples((o, SCHEMA.isPartOf,
                                   URIRef("http://attic.schema.org"))))
                for (rs, rp, ro) in atts:
                    #log.info("Removing %s" % rs)
                    g.remove((rs, None, None))
                    g.remove((None, None, rs))
        finally:
            RDFLIBLOCK.release()
    if markdown:
        try:
            RDFLIBLOCK.acquire()
            trips = list(g.triples((None, RDFS.comment, None)))
            Markdown.setPre("http://schema.org/")
            for (s, p, com) in trips:
                mcom = Markdown.parse(com)
                g.remove((s, p, com))
                g.add((s, p, Literal(mcom)))
        finally:
            RDFLIBLOCK.release()
            Markdown.setPre()
    return g
Exemplo n.º 19
0
def buildSingleTermGraph(node,excludeAttic=True,markdown=True):
    
    g = rdflib.Graph()
    g.bind('owl', 'http://www.w3.org/2002/07/owl#')
    g.bind('rdfa', 'http://www.w3.org/ns/rdfa#')
    g.bind('dct', 'http://purl.org/dc/terms/')
    g.bind('schema', 'http://schema.org/')
    
    full = "http://schema.org/" + node
    #n = URIRef(full)
    n = SCHEMA.term(node)
    n = n
    full = str(n)
    q = queryGraph()
    ret = None
    
    #log.info("NAME %s %s"% (n,full))
    atts = None
    try:
        RDFLIBLOCK.acquire()
        atts = list(q.triples((n,SCHEMA.isPartOf,URIRef("http://attic.schema.org"))))
    finally:
        RDFLIBLOCK.release()    
    if len(atts):
        #log.info("ATTIC TERM %s" % n)
        excludeAttic = False
    #Outgoing triples
    try:
        RDFLIBLOCK.acquire()
        ret = list(q.triples((n,None,None)))
    finally:
        RDFLIBLOCK.release()
    for (s,p,o) in ret:
        #log.info("adding %s %s %s" % (s,p,o))
        g.add((s,p,o))

    #Incoming triples
    ret = list(q.triples((None,None,n)))
    for (s,p,o) in ret:
        #log.info("adding %s %s %s" % (s,p,o))
        g.add((s,p,o))

    #super classes
	query='''select * where {
	?term (^rdfs:subClassOf*) <%s>.
	?term rdfs:subClassOf ?super.
	?super ?pred ?obj.
	}''' % n

	ret = rdfQueryStore(query,q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.term,RDFS.subClassOf,row.super))
        g.add((row.term,RDFS.subClassOf,row.super))
        g.add((row.super,row.pred,row.obj))
         
    #poperties with superclasses in domain
	query='''select * where{
	?term (^rdfs:subClassOf*) <%s>.
	?prop <http://schema.org/domainIncludes> ?term.
	?prop ?pred ?obj.
	}
	''' % n
	ret = rdfQueryStore(query,q)
    for row in ret:
        g.add((row.prop,SCHEMA.domainIncludes,row.term))
        g.add((row.prop,row.pred,row.obj))

    #super properties
	query='''select * where {
	?term (^rdfs:subPropertyOf*) <%s>.
	?term rdfs:subPropertyOf ?super.
	?super ?pred ?obj.
	}''' % n
	ret = rdfQueryStore(query,q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.term,RDFS.subPropertyOf,row.super))
        g.add((row.term,RDFS.subPropertyOf,row.super))
        g.add((row.super,row.pred,row.obj))

    #Enumeration for an enumeration value
	query='''select * where {
	<%s> a ?type.
	?type ?pred ?obj.
	FILTER NOT EXISTS{?type a rdfs:class}.
	}''' % n
	ret = rdfQueryStore(query,q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.type,row.pred,row.obj))
        g.add((row.type,row.pred,row.obj))

    if excludeAttic: #Remove triples referencing terms part of http://attic.schema.org
        trips = list(g.triples((None,None,None)))
        try:
            RDFLIBLOCK.acquire()
            for (s,p,o) in trips:
                atts = list(q.triples((s,SCHEMA.isPartOf,URIRef("http://attic.schema.org"))))
                if isinstance(o, URIRef):
                    atts.extend(q.triples((o,SCHEMA.isPartOf,URIRef("http://attic.schema.org"))))
                for (rs,rp,ro) in atts:
                    #log.info("Removing %s" % rs)
                    g.remove((rs,None,None))
                    g.remove((None,None,rs))
        finally:
            RDFLIBLOCK.release()
    if markdown:
        try:
            RDFLIBLOCK.acquire()
            trips = list(g.triples((None,RDFS.comment,None)))
            Markdown.setPre("http://schema.org/")
            for (s,p,com) in trips:
                mcom = Markdown.parse(com)
                g.remove((s,p,com))
                g.add((s,p,Literal(mcom)))
        finally:
            RDFLIBLOCK.release()
            Markdown.setPre()
    return g
Exemplo n.º 20
0
def buildSingleTermGraph(node, excludeAttic=True, markdown=True):

    q = queryGraph()
    g = rdflib.Graph()
    ns = q.namespaces()
    for n in ns:
        prefix, path = n
        namespaceAdd(g, prefix=prefix, path=path)
    namespaceAdd(g, api.SdoConfig.prefix(), api.SdoConfig.vocabUri())

    full = "%s%s" % (api.SdoConfig.vocabUri(), node)
    n = URIRef(full)
    full = str(n)
    ret = None

    #log.info("NAME %s %s"% (n,full))
    atts = None
    attic = api.SdoConfig.atticUri()
    if attic:
        with RDFLIBLOCK:
            atts = list(q.triples((n, SCHEMA.isPartOf, URIRef(attic))))
    if len(atts):
        #log.info("ATTIC TERM %s" % n)
        excludeAttic = False

    #Outgoing triples
    with RDFLIBLOCK:
        ret = list(q.triples((n, None, None)))

    for (s, p, o) in ret:
        #log.info("adding %s %s %s" % (s,p,o))
        g.add((s, p, o))

    #Incoming triples
    with RDFLIBLOCK:
        ret = list(q.triples((None, None, n)))
    for (s, p, o) in ret:
        #log.info("adding %s %s %s" % (s,p,o))
        g.add((s, p, o))

    #super classes
    query = '''select * where {
    ?term (^rdfs:subClassOf*) <%s>.
    ?term rdfs:subClassOf ?super.
        OPTIONAL {
        	?super ?pred ?obj.
            FILTER (strstarts(str(?super),'%s'))
        }
    }
    ''' % (n, api.SdoConfig.vocabUri())
    #log.info("Query: %s" % query)

    ret = rdfQueryStore(query, q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.term,RDFS.subClassOf,row.super))
        g.add((row.term, RDFS.subClassOf, row.super))
        pred = row.pred
        obj = row.obj
        if pred and obj:
            g.add((row.super, row.pred, row.obj))

    #poperties with superclasses in domain
        query = '''select * where{
	?term (^rdfs:subClassOf*) <%s>.
	?prop <http://schema.org/domainIncludes> ?term.
        OPTIONAL {
        	?prop ?pred ?obj.
            FILTER (strstarts(str(?prop),'%s'))
        }
	}
    ''' % (n, api.SdoConfig.vocabUri())
    #log.info("Query: %s" % query)
    ret = rdfQueryStore(query, q)
    for row in ret:
        g.add((row.prop, SCHEMA.domainIncludes, row.term))
        pred = row.pred
        obj = row.obj
        if pred and obj:
            g.add((row.prop, row.pred, row.obj))

    #super properties
        query = '''select * where {
	?term (^rdfs:subPropertyOf*) <%s>.
	?term rdfs:subPropertyOf ?super.
        OPTIONAL {
        	?super ?pred ?obj.
            FILTER (strstarts(str(?super),'%s'))
        }
    }
    ''' % (n, api.SdoConfig.vocabUri())
    #log.info("Query: %s" % query)
    ret = rdfQueryStore(query, q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.term,RDFS.subPropertyOf,row.super))
        g.add((row.term, RDFS.subPropertyOf, row.super))
        pred = row.pred
        obj = row.obj
        if pred and obj:
            g.add((row.super, row.pred, row.obj))

    #Enumeration for an enumeration value
        query = '''select * where {
	<%s> a ?type.
	?type ?pred ?obj.
	FILTER NOT EXISTS{?type a rdfs:class}.
	}''' % n
        ret = rdfQueryStore(query, q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.type,row.pred,row.obj))
        g.add((row.type, row.pred, row.obj))

    if excludeAttic:  #Remove triples referencing terms part of http://attic.schema.org
        trips = list(g.triples((None, None, None)))
        with RDFLIBLOCK:
            for (s, p, o) in trips:
                atts = list(q.triples((s, SCHEMA.isPartOf, URIRef(attic))))
                if isinstance(o, URIRef):
                    atts.extend(q.triples((o, SCHEMA.isPartOf, URIRef(attic))))
                for (rs, rp, ro) in atts:
                    #log.info("Removing %s" % rs)
                    g.remove((rs, None, None))
                    g.remove((None, None, rs))
    if markdown:
        with RDFLIBLOCK:
            trips = list(g.triples((None, RDFS.comment, None)))
            Markdown.setPre(api.SdoConfig.vocabUri())
            for (s, p, com) in trips:
                mcom = Markdown.parse(com)
                g.remove((s, p, com))
                g.add((s, p, Literal(mcom)))
        Markdown.setPre()
    return g
Exemplo n.º 21
0
def buildSingleTermGraph(node,excludeAttic=True,markdown=True):
    
    q = queryGraph()
    g = rdflib.Graph()
    ns = q.namespaces()
    for n in ns:
        prefix, path = n
        namespaceAdd(g,prefix=prefix,path=path)
    namespaceAdd(g,api.SdoConfig.prefix(),api.SdoConfig.vocabUri())
    
    full = "%s%s" % (api.SdoConfig.vocabUri(), node)
    n = URIRef(full)
    full = str(n)
    ret = None
    
    #log.info("NAME %s %s"% (n,full))
    atts = None
    attic = api.SdoConfig.atticUri()
    if attic:
        with RDFLIBLOCK:
            atts = list(q.triples((n,SCHEMA.isPartOf,URIRef(attic))))
    if len(atts):
        #log.info("ATTIC TERM %s" % n)
        excludeAttic = False

    #Outgoing triples
    with RDFLIBLOCK:
        ret = list(q.triples((n,None,None)))

    for (s,p,o) in ret:
        #log.info("adding %s %s %s" % (s,p,o))
        g.add((s,p,o))

    #Incoming triples
    with RDFLIBLOCK:
        ret = list(q.triples((None,None,n)))
    for (s,p,o) in ret:
        #log.info("adding %s %s %s" % (s,p,o))
        g.add((s,p,o))

    #super classes
    query='''select * where {
    ?term (^rdfs:subClassOf*) <%s>.
    ?term rdfs:subClassOf ?super.
        OPTIONAL {
        	?super ?pred ?obj.
            FILTER (strstarts(str(?super),'%s'))
        }
    }
    ''' % (n,api.SdoConfig.vocabUri())
    #log.info("Query: %s" % query)

    ret = rdfQueryStore(query,q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.term,RDFS.subClassOf,row.super))
        g.add((row.term,RDFS.subClassOf,row.super))
        pred = row.pred
        obj = row.obj
        if pred and obj:
            g.add((row.super,row.pred,row.obj))
         
    #poperties with superclasses in domain
	query='''select * where{
	?term (^rdfs:subClassOf*) <%s>.
	?prop <http://schema.org/domainIncludes> ?term.
        OPTIONAL {
        	?prop ?pred ?obj.
            FILTER (strstarts(str(?prop),'%s'))
        }
	}
    ''' % (n,api.SdoConfig.vocabUri())
    #log.info("Query: %s" % query)
    ret = rdfQueryStore(query,q)
    for row in ret:
        g.add((row.prop,SCHEMA.domainIncludes,row.term))
        pred = row.pred
        obj = row.obj
        if pred and obj:
            g.add((row.prop,row.pred,row.obj))

    #super properties
	query='''select * where {
	?term (^rdfs:subPropertyOf*) <%s>.
	?term rdfs:subPropertyOf ?super.
        OPTIONAL {
        	?super ?pred ?obj.
            FILTER (strstarts(str(?super),'%s'))
        }
    }
    ''' % (n,api.SdoConfig.vocabUri())
    #log.info("Query: %s" % query)
    ret = rdfQueryStore(query,q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.term,RDFS.subPropertyOf,row.super))
        g.add((row.term,RDFS.subPropertyOf,row.super))
        pred = row.pred
        obj = row.obj
        if pred and obj:
            g.add((row.super,row.pred,row.obj))

    #Enumeration for an enumeration value
	query='''select * where {
	<%s> a ?type.
	?type ?pred ?obj.
	FILTER NOT EXISTS{?type a rdfs:class}.
	}''' % n
	ret = rdfQueryStore(query,q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.type,row.pred,row.obj))
        g.add((row.type,row.pred,row.obj))

    if excludeAttic: #Remove triples referencing terms part of http://attic.schema.org
        trips = list(g.triples((None,None,None)))
        with RDFLIBLOCK:
            for (s,p,o) in trips:
                atts = list(q.triples((s,SCHEMA.isPartOf,URIRef(attic))))
                if isinstance(o, URIRef):
                    atts.extend(q.triples((o,SCHEMA.isPartOf,URIRef(attic))))
                for (rs,rp,ro) in atts:
                    #log.info("Removing %s" % rs)
                    g.remove((rs,None,None))
                    g.remove((None,None,rs))
    if markdown:
        with RDFLIBLOCK:
            trips = list(g.triples((None,RDFS.comment,None)))
            Markdown.setPre(api.SdoConfig.vocabUri())
            for (s,p,com) in trips:
                mcom = Markdown.parse(com)
                g.remove((s,p,com))
                g.add((s,p,Literal(mcom)))
        Markdown.setPre()
    return g
Exemplo n.º 22
0
 def test_emph(self):
     markstring = "This is _em_, __strong__, ___strong em___"
     html = Markdown.parse(markstring,True)
     self.assertFalse(html != "<p>This is <em>em</em>, <strong>strong</strong>, <strong><em>strong em</em></strong></p>\n", "Markdown string not formatted correctly")