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)
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_)
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")
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
def _literal(self, token, uri): if token.endswith('>'): tokens = token.rsplit('^^', 1) lexical_form = self._string(tokens[0][1:-1]) datatype = self._uriref(tokens[1], uri) return TypedLiteral(lexical_form, datatype) else: tokens = token.rsplit('@', 1) lexical_form = self._string(tokens[0][1:-1]) if len(tokens) == 1: language = None else: language = tokens[1] return PlainLiteral(lexical_form, language)
def test_triple_12_literal_escape_return(self): triple = self._get_triple(11) self.assertEqual( triple, (EX.resource11, EX.property, PlainLiteral("return\r")))
def test_triple_13_literal_escape_tab(self): triple = self._get_triple(12) self.assertEqual(triple, (EX.resource12, EX.property, PlainLiteral("tab:\t")))
def test_triple_10_literal_escape_double_quote(self): triple = self._get_triple(9) self.assertEqual( triple, (EX.resource9, EX.property, PlainLiteral("dquote:\"")))
def setUp(self): self.literal = PlainLiteral("cat", 'en')
def test_triple_29_plain_literal_with_language_en(self): triple = self._get_triple(28) self.assertEqual( triple, (EX.resource31, EX.property, PlainLiteral("chat", 'en')))
def test_triple_8_plain_literal(self): triple = self._get_triple(7) self.assertEqual( triple, (EX.resource7, EX.property, PlainLiteral("simple literal")))
def test_hash_not_equal_to_plain_literal_without_language(self): self.assertNotEqual(hash(self.literal), hash(PlainLiteral("cat")))
def test_triple_18_unicode_euro_symbol(self): triple = self._get_triple(17) self.assertEqual(triple, (EX.resource17, EX.property, PlainLiteral("\u20AC")))
def test_hash_not_equal_to_plain_literal_with_different_lexical_form(self): self.assertNotEqual(hash(self.literal), hash(PlainLiteral("dog", 'en')))
def test_hash_not_equal_to_plain_literal_with_different_language(self): self.assertNotEqual(hash(self.literal), hash(PlainLiteral("cat", 'es')))
def test_hash_equal_to_plain_literal_with_same_lexical_form_and_language(self): self.assertEqual(hash(self.literal), hash(PlainLiteral("cat", 'en')))
def test_not_equal_to_simple_literal(self): self.assertNotEqual(self.literal, PlainLiteral("cat"))
def test_language_tag_is_normalized_to_lowercase(self): literal = PlainLiteral("cat", 'EN') self.assertEqual(literal.language, 'en')
def test_triple_15_optional_space_before_dot(self): triple = self._get_triple(14) self.assertEqual(triple, (EX.resource14, EX.property, PlainLiteral("x")))
def test_does_not_compare_to_simple_literal(self): literal = PlainLiteral("cat") with self.assertRaises(TypeError): self.literal < literal with self.assertRaises(TypeError): self.literal > literal
def test_triple_17_unicode_e_acute(self): triple = self._get_triple(16) self.assertEqual(triple, (EX.resource16, EX.property, PlainLiteral("\u00E9")))
def test_compares_less_than_simple_literal_lexicographically(self): self.assert_(self.literal < PlainLiteral("cau"))
def test_triple_28_plain_literal_with_language_fr(self): triple = self._get_triple(27) self.assertEqual( triple, (EX.resource30, EX.property, PlainLiteral("chat", 'fr')))
def test_compares_greater_than_plain_literal_with_same_lexical_form(self): self.assert_(self.literal > PlainLiteral("1", 'en'))
def test_well_formed_line_yields_triple(self): document = '<http://example.org/test> <http://example.org/property> "test" .' self.assertEqual(next(self.reader.read(document)), (EX.test, EX.property, PlainLiteral("test")))
def test_hash_not_equal_to_plain_literal_with_language(self): literal = TypedLiteral("1", URI('en')) self.assertNotEqual(hash(literal), hash(PlainLiteral("1", 'en')))
def test_triple_9_literal_escape_backslash(self): triple = self._get_triple(8) self.assertEqual( triple, (EX.resource8, EX.property, PlainLiteral("backslash:\\")))
def __call__(self, obj, context): if self.datatype is None: return PlainLiteral(obj.lexical_form) else: return TypedLiteral(obj.lexical_form, self.datatype)
def test_triple_11_literal_escape_newline(self): triple = self._get_triple(10) self.assertEqual( triple, (EX.resource10, EX.property, PlainLiteral("newline:\n")))
def test_compares_greater_than_simple_literal_lexicographically(self): self.assert_(self.literal > PlainLiteral("cas"))