From 1e4247a9bf3cfdc87e177f3bcb52aeffdf66d41b Mon Sep 17 00:00:00 2001 From: Prince Sachdeva Date: Wed, 27 May 2020 01:28:28 +0530 Subject: [PATCH 1/4] #747 Create new GregorianType class in extras/gregorianType.py to add gYear and gYearMonth types --- rdflib/extras/gregorianType.py | 16 ++++++++++++++++ rdflib/term.py | 19 ++++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 rdflib/extras/gregorianType.py diff --git a/rdflib/extras/gregorianType.py b/rdflib/extras/gregorianType.py new file mode 100644 index 000000000..5d2405d9d --- /dev/null +++ b/rdflib/extras/gregorianType.py @@ -0,0 +1,16 @@ +class GregorianType: + def __init__(self, lexical_value : str, datatype : int): + self.lexical_value = lexical_value + lvalue = lexical_value.split('-') + self.gYear = lvalue[0] + self.gMonth = None + if datatype == 2: + if len(lvalue) != 2: + raise Exception("Wrong value for GYearMonth") + self.gMonth = lvalue[1] + + def __str__(self): + if self.gMonth == None: + return self.gYear + return self.gYear + '-' + self.gMonth + diff --git a/rdflib/term.py b/rdflib/term.py index 3d290258d..f697d0bb7 100644 --- a/rdflib/term.py +++ b/rdflib/term.py @@ -25,6 +25,7 @@ from __future__ import print_function # from __future__ import unicode_literals from fractions import Fraction +from rdflib.extras.gregorianType import GregorianType __all__ = [ 'bind', @@ -74,7 +75,6 @@ _invalid_uri_chars = '<>" {}|\\^`' - def _is_valid_uri(uri): return all(map(lambda c: ord(c) > 256 or not c in _invalid_uri_chars, uri)) @@ -549,7 +549,16 @@ def __new__(cls, lexical_or_value, lang=None, datatype=None, normalize=None): datatype = URIRef(datatype) value = None - if isinstance(lexical_or_value, Literal): + + if datatype == URIRef(_XSD_GYEAR) or datatype == URIRef(_XSD_GYEARMONTH): + if datatype == URIRef(_XSD_GYEAR): + if lexical_or_value.find('-') != -1: + raise Exception('Lexical value not of gYear(YYYY) type') + value = GregorianType(lexical_or_value, 1) + else: + value = GregorianType(lexical_or_value, 2) + + elif isinstance(lexical_or_value, Literal): # create from another Literal instance lang = lang or lexical_or_value.language @@ -564,7 +573,6 @@ def __new__(cls, lexical_or_value, lang=None, datatype=None, normalize=None): # passed a string # try parsing lexical form of datatyped literal value = _castLexicalToPython(lexical_or_value, datatype) - if value is not None and normalize: _value, _datatype = _castPythonToLiteral(value, datatype) if _value is not None and _is_valid_unicode(_value): @@ -1408,6 +1416,9 @@ def _unhexlify(value): _OWL_RATIONAL = URIRef('http://www.w3.org/2002/07/owl#rational') _XSD_HEXBINARY = URIRef(_XSD_PFX + 'hexBinary') # TODO: gYearMonth, gYear, gMonthDay, gDay, gMonth +_XSD_GYEAR = URIRef(_XSD_PFX + 'gYear') +_XSD_GYEARMONTH = URIRef(_XSD_PFX + 'gYearMonth') + _NUMERIC_LITERAL_TYPES = ( _XSD_INTEGER, @@ -1541,8 +1552,6 @@ def _castPythonToLiteral(obj, datatype): None: None, # plain literals map directly to value space URIRef(_XSD_PFX + 'time'): parse_time, URIRef(_XSD_PFX + 'date'): parse_date, - URIRef(_XSD_PFX + 'gYear'): parse_date, - URIRef(_XSD_PFX + 'gYearMonth'): parse_date, URIRef(_XSD_PFX + 'dateTime'): parse_datetime, URIRef(_XSD_PFX + 'duration'): parse_duration, URIRef(_XSD_PFX + 'dayTimeDuration'): parse_duration, From 5078b8b6f7789cec759a8912e5594a2867bb0f40 Mon Sep 17 00:00:00 2001 From: Prince Sachdeva Date: Wed, 27 May 2020 17:28:44 +0530 Subject: [PATCH 2/4] #747 Create new GregorianType class in extras/gregorianType.py to add gYear and gYearMonth types Signed-off-by: Prince Sachdeva --- rdflib/extras/{gregorianType.py => gregorian_type.py} | 2 -- rdflib/term.py | 6 ++---- 2 files changed, 2 insertions(+), 6 deletions(-) rename rdflib/extras/{gregorianType.py => gregorian_type.py} (81%) diff --git a/rdflib/extras/gregorianType.py b/rdflib/extras/gregorian_type.py similarity index 81% rename from rdflib/extras/gregorianType.py rename to rdflib/extras/gregorian_type.py index 5d2405d9d..4f0031541 100644 --- a/rdflib/extras/gregorianType.py +++ b/rdflib/extras/gregorian_type.py @@ -5,8 +5,6 @@ def __init__(self, lexical_value : str, datatype : int): self.gYear = lvalue[0] self.gMonth = None if datatype == 2: - if len(lvalue) != 2: - raise Exception("Wrong value for GYearMonth") self.gMonth = lvalue[1] def __str__(self): diff --git a/rdflib/term.py b/rdflib/term.py index f697d0bb7..087d18cdc 100644 --- a/rdflib/term.py +++ b/rdflib/term.py @@ -25,7 +25,7 @@ from __future__ import print_function # from __future__ import unicode_literals from fractions import Fraction -from rdflib.extras.gregorianType import GregorianType +from rdflib.extras.gregorian_type import GregorianType __all__ = [ 'bind', @@ -549,11 +549,8 @@ def __new__(cls, lexical_or_value, lang=None, datatype=None, normalize=None): datatype = URIRef(datatype) value = None - if datatype == URIRef(_XSD_GYEAR) or datatype == URIRef(_XSD_GYEARMONTH): if datatype == URIRef(_XSD_GYEAR): - if lexical_or_value.find('-') != -1: - raise Exception('Lexical value not of gYear(YYYY) type') value = GregorianType(lexical_or_value, 1) else: value = GregorianType(lexical_or_value, 2) @@ -600,6 +597,7 @@ def __new__(cls, lexical_or_value, lang=None, datatype=None, normalize=None): inst._language = lang inst._datatype = datatype inst._value = value + print(inst, inst._value, 'literal created') return inst def normalize(self): From 31c9e495ff0db2c0709172ea48bd20eb0c696fa1 Mon Sep 17 00:00:00 2001 From: Prince Sachdeva Date: Wed, 27 May 2020 17:33:45 +0530 Subject: [PATCH 3/4] #747 Create new GregorianType class in extras/gregorian_type.py to add gYear and gYearMonth types Signed-off-by: Prince Sachdeva --- rdflib/term.py | 1 - 1 file changed, 1 deletion(-) diff --git a/rdflib/term.py b/rdflib/term.py index 92344a395..9d2344dd0 100644 --- a/rdflib/term.py +++ b/rdflib/term.py @@ -591,7 +591,6 @@ def __new__(cls, lexical_or_value, lang=None, datatype=None, normalize=None): inst._language = lang inst._datatype = datatype inst._value = value - print(inst, inst._value, 'literal created') return inst def normalize(self): From f64eb1fe48ccc9f63cad82a0fef7f3d3cbcff0c4 Mon Sep 17 00:00:00 2001 From: Prince Sachdeva Date: Wed, 27 May 2020 18:17:21 +0530 Subject: [PATCH 4/4] #747 Error in convert function of _XSD_BOOLEAN, which is not related to this issue Signed-off-by: Prince Sachdeva --- rdflib/term.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rdflib/term.py b/rdflib/term.py index 9d2344dd0..40fc26f8f 100644 --- a/rdflib/term.py +++ b/rdflib/term.py @@ -1584,7 +1584,7 @@ def _castPythonToLiteral(obj, datatype): URIRef(_XSD_PFX + 'normalizedString'): None, URIRef(_XSD_PFX + 'token'): None, URIRef(_XSD_PFX + 'language'): None, - URIRef(_XSD_PFX + 'boolean'): lambda i: i.lower() == 'true', + URIRef(_XSD_PFX + 'boolean'): lambda i: i.lower() == 'true' or (i.isdigit() and int(i) == True), URIRef(_XSD_PFX + 'decimal'): Decimal, URIRef(_XSD_PFX + 'integer'): long_type, URIRef(_XSD_PFX + 'nonPositiveInteger'): int,