Пример #1
0
 def test_vocabulary_is_set_of_names_without_datatypes(self):
     self.assertEqual(self.graph.vocabulary(),
                      {URI('http://www.example.org/index.html'),
                       URI('http://purl.org/dc/elements/1.1/creator'),
                       URI('http://xmlns.com/foaf/0.1/name'),
                       URI('http://xmlns.com/foaf/0.1/knows'),
                       Literal("John Lennon", XSD.string)})
Пример #2
0
 def setUp(self):
     self.triples = {(URI('http://www.example.org/index.html'),
                      URI('http://purl.org/dc/elements/1.1/creator'),
                      URI('http://www.example.org/staffid/85740')),
                     (URI('http://www.example.org/staffid/85740'),
                      URI('http://xmlns.com/foaf/0.1/name'),
                      Literal("John Lennon", XSD.string))}
     self.graph = Graph(self.triples)
Пример #3
0
 def test_names_is_set_of_uris_and_literals(self):
     self.assertEqual(self.graph.names(),
                      {URI('http://www.example.org/index.html'),
                       URI('http://purl.org/dc/elements/1.1/creator'),
                       URI('http://xmlns.com/foaf/0.1/name'),
                       URI('http://xmlns.com/foaf/0.1/knows'),
                       Literal("John Lennon", XSD.string),
                       XSD.string})
Пример #4
0
 def test_not_equal_to_frozenset_with_different_triples(self):
     triples = {(URI('http://www.example.org/index.html'),
                 URI('http://purl.org/dc/elements/1.1/creator'),
                 URI('http://www.example.org/staffid/85740')),
                (URI('http://www.example.org/staffid/85740'),
                 URI('http://xmlns.com/foaf/0.1/name'),
                 Literal("John Doe", XSD.string))}
     self.assertNotEqual(self.graph, frozenset(triples))
Пример #5
0
 def test_not_equal_to_graph_with_different_number_of_unground_triples(self):
     graph = Graph({(URI('http://www.example.org/index.html'),
                     URI('http://purl.org/dc/elements/1.1/creator'),
                     URI('john')),
                    (URI('john'),
                     URI('http://xmlns.com/foaf/0.1/name'),
                     Literal("John Lennon", XSD.string))})
     self.assertNotEqual(self.graph, graph)        
     self.assertNotEqual(graph, self.graph)        
Пример #6
0
 def _init(self):
     super()._init()
     self.uri = URI(QName(self.tag))
     self.subject = None
     self.base_uri = URI(self.base) if self.base is not None else None
     self.language = self.attrib.get(QName(XML, 'lang'))
     if self.language is None:
         parent = self.getparent()
         if parent is not None:
             self.language = parent.language
Пример #7
0
 def test_not_equal_to_graph_without_valid_bijection(self):
     graph = Graph({(URI('http://www.example.org/index.html'),
                     URI('http://purl.org/dc/elements/1.1/creator'),
                     BlankNode('john')),
                    (BlankNode('john'),
                     URI('http://xmlns.com/foaf/0.1/name'),
                     Literal("John Lennon", XSD.string)),
                    (BlankNode('paul'),
                     URI('http://xmlns.com/foaf/0.1/knows'),
                     BlankNode('john'))})
     self.assertNotEqual(self.graph, graph)
     self.assertNotEqual(graph, self.graph)
Пример #8
0
 def from_element(cls, element):
     type = URI(QName(element.tag))
     uri = URI(element.get(QName(RDF, 'about')))
     type_class = cls.TYPE_MAP.get(type, cls)
     if cls is type_class:
         test = cls(type, uri)
         test.status = cls._status(element)
         test.description = cls._description(element)
         test.warning = cls._warning(element)
     else:
         test = type_class.from_element(element)
     return test
Пример #9
0
 def _property_attrs(self, element):
     # 2.5 Property Attributes
     for attr, value in element.items():
         if attr not in _XML_ATTRS:
             predicate = URI(QName(attr))
             if predicate not in self.ILLEGAL_PROPERTY_ATTRS:
                 if predicate != RDF.type:
                     object_ = PlainLiteral(value, element.language)
                 else:
                     object_ = URI(value)
                 yield (element.subject, predicate, object_)
             elif predicate == RDF.li:
                 raise ParseError("rdf:li is not allowed as attribute")
Пример #10
0
 def test_matches_only_typed_literals(self):
     self.assertFalse(BlankNode() in self.type)
     self.assertFalse(URI('test') in self.type)
     self.assertFalse(PlainLiteral('1.5') in self.type)
     self.assertFalse(PlainLiteral('1.5', 'en') in self.type)
     self.assert_(TypedLiteral('1.5', XSD.float) in self.type)
     self.assert_(TypedLiteral('1.5', XSD.string) in self.type)
Пример #11
0
 def __init__(self, lexical_form, datatype):
     super().__init__(lexical_form)
     if isinstance(datatype, str):
         if not isinstance(datatype, URI):
             datatype = URI(datatype)
         self.datatype = datatype
     else:
         raise TypeError("datatype must be a string")
Пример #12
0
 def _empty_property(self, element, parent, ids):
     # 7.2.21 Production emptyPropertyElt
     id_ = self._id(element, ids)
     literal_attrs = _XML_ATTRS | {QName(RDF, 'ID')}
     if all(attr in literal_attrs for attr in element.keys()):
         object_ = PlainLiteral("", element.language)
         triple = (parent.subject, element.uri, object_)
         yield triple
         if id_ is not None:
             for triple in self._reify(id_, triple):
                 yield triple
     else:
         resource = element.attrib.get(QName(RDF, 'resource'))
         node_id = element.attrib.get(QName(RDF, 'nodeID'))
         if resource is not None:
             if node_id is None:
                 object_ = self._uri(resource, element.base_uri)
             else:
                 raise ParseError
         elif node_id is not None:
             if _NCNAME.match(node_id):
                 object_ = BlankNode(node_id)
             else:
                 raise ParseError(
                     "rdf:nodeID does not match NCName: {!r}".format(
                         node_id))
         else:
             object_ = BlankNode()
         triple = (parent.subject, element.uri, object_)
         yield triple
         if id_ is not None:
             for triple in self._reify(id_, triple):
                 yield triple
         subject = object_
         property_attrs = set(element.keys())
         property_attrs -= literal_attrs | {
             QName(RDF, 'resource'),
             QName(RDF, 'nodeID')
         }
         for attr in property_attrs:
             predicate = URI(QName(attr))
             if predicate in self.XML_TERMS:
                 continue
             elif predicate in self.ILLEGAL_PROPERTY_ATTRS:
                 raise ParseError
             value = element.get(attr)
             if predicate != RDF.type:
                 object_ = PlainLiteral(value, element.language)
             else:
                 object_ = self._uri(value, element.base_uri)
             yield (subject, predicate, object_)
Пример #13
0
 def test_compares_to_uri_lexicographically(self):
     self.assert_(
         URI('http://script.example/Latin') < URI(
             'http://script.example/Кириллица') < URI(
                 'http://script.example/漢字') < self.uri)
     self.assert_(self.uri > URI('http://script.example/漢字') > URI(
         'http://script.example/Кириллица') > URI(
             'http://script.example/Latin'))
Пример #14
0
 def _literal_property(self, element, parent, ids):
     # 7.2.16 Production literalPropertyElt
     datatype = element.get(QName(RDF, 'datatype'))
     if datatype is not None:
         object_ = TypedLiteral(element.text, URI(datatype))
     else:
         object_ = PlainLiteral(element.text, element.language)
     triple = (parent.subject, element.uri, object_)
     yield triple
     id_ = self._id(element, ids)
     if id_ is not None:
         # 7.3 Reification Rules
         for triple in self._reify(id_, triple):
             yield triple
Пример #15
0
 def setUp(self):
     self.triples = {(URI('http://www.example.org/index.html'),
                      URI('http://purl.org/dc/elements/1.1/creator'),
                      BlankNode('john')),
                     (BlankNode('john'),
                      URI('http://xmlns.com/foaf/0.1/name'),
                      Literal("John Lennon", XSD.string)),
                     (BlankNode('john'),
                      URI('http://xmlns.com/foaf/0.1/knows'),
                      BlankNode('paul'))}
     self.bijection = {(URI('http://www.example.org/index.html'),
                        URI('http://purl.org/dc/elements/1.1/creator'),
                        BlankNode('lennon')),
                       (BlankNode('lennon'),
                        URI('http://xmlns.com/foaf/0.1/name'),
                        Literal("John Lennon", XSD.string)),
                       (BlankNode('lennon'),
                        URI('http://xmlns.com/foaf/0.1/knows'),
                        BlankNode('starr'))}
     self.graph = Graph(self.triples)
Пример #16
0
 def __getattr__(self, local_name):
     return URI(self + local_name)
Пример #17
0
 def __getitem__(self, local_name):
     if isinstance(local_name, str):
         return URI(self + local_name)
     else:
         return super().__getitem__(local_name)
Пример #18
0
 def test_getitem_returns_new_uri(self):
     self.assertEqual(self.namespace['test'],
                      URI('http://example.org/test'))
Пример #19
0
 def test_getattr_returns_new_uri(self):
     self.assertEqual(self.namespace.test, URI('http://example.org/test'))
Пример #20
0
 def __init__(self, type, uri=None):
     self.type = URI(type)
     self.uri = URI(uri) if uri is not None else uri
Пример #21
0
 def test_compares_equal_to_uri(self):
     self.assertEqual(self.namespace, URI('http://example.org/'))
Пример #22
0
 def _datatype_support(cls, element):
     for child in element.findall(str(QName(TEST, 'datatypeSupport'))):
         uri = child.get(QName(RDF, 'resource'))
         if uri is not None:
             yield URI(uri)
Пример #23
0
 def test_calling_blank_node_with_different_context_returns_different_blank_node(self):
     context = Context()
     bnode_1 = self.subjects.blank_node(URI('test'), self.context)
     bnode_2 = self.subjects.blank_node(URI('test'), context)
     self.assertNotEqual(bnode_1, bnode_2)
Пример #24
0
 def test_datatype_is_converted_to_uri(self):
     literal = TypedLiteral("1", "string")
     self.assert_(isinstance(literal.datatype, URI))
     self.assertEqual(literal.datatype, URI("string"))
Пример #25
0
 def _entailment_rules(cls, element):
     for child in element.findall(str(QName(TEST, 'entailmentRules'))):
         uri = child.get(QName(RDF, 'resource'))
         if uri is not None:
             yield URI(uri)
Пример #26
0
 def test_hash_not_equal_to_plain_literal_with_language(self):
     literal = TypedLiteral("1", URI('en'))
     self.assertNotEqual(hash(literal), hash(PlainLiteral("1", 'en')))
Пример #27
0
 def test_compares_greater_than_uri(self):
     uri = URI('http://example.org/')
     self.assert_(self.literal > uri)
     self.assert_(not self.literal < uri)
Пример #28
0
 def _uri(self, uri, base_uri=None):
     if base_uri and not uri:
         base_uri = base_uri.rsplit('#', 1)[0]
     return URI(urllib.parse.urljoin(base_uri or '', uri))
Пример #29
0
 def test_nodes_is_set_of_subjects_and_objects(self):
     self.assertEqual(self.graph.nodes(),
                      {URI('http://www.example.org/index.html'),
                       BlankNode('john'), BlankNode('paul'),
                       Literal("John Lennon", XSD.string)})
Пример #30
0
 def test_nodes_is_set_of_subjects_and_objects(self):
     self.assertEqual(self.graph.nodes(),
                      {URI('http://www.example.org/index.html'),
                       URI('http://www.example.org/staffid/85740'),
                       Literal("John Lennon", XSD.string)})