Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _parse(self, val):
return isodate.parse_time(val)
@staticmethod
def deserialize_time(attr):
"""Deserialize ISO-8601 formatted string into time object.
:param str attr: response string to be deserialized.
:rtype: datetime.time
:raises: DeserializationError if string format invalid.
"""
if isinstance(attr, ET.Element):
attr = attr.text
if re.search(r"[^\W\d_]", attr, re.I + re.U):
raise DeserializationError("Date must have only digits and -. Received: %s" % attr)
return isodate.parse_time(attr)
(long, (None, _XSD_INTEGER)),
(Decimal, (None, _XSD_DECIMAL)),
(datetime, (lambda i:i.isoformat(), _XSD_DATETIME)),
(date, (lambda i:i.isoformat(), _XSD_DATE)),
(time, (lambda i:i.isoformat(), _XSD_TIME)),
(xml.dom.minidom.Document, (_writeXML, _RDF_XMLLITERAL)),
# this is a bit dirty - by accident the html5lib parser produces
# DocumentFragments, and the xml parser Documents, letting this
# decide what datatype to use makes roundtripping easier, but it a
# bit random
(xml.dom.minidom.DocumentFragment, (_writeXML, _RDF_HTMLLITERAL))
]
XSDToPython = {
None : None, # plain literals map directly to value space
URIRef(_XSD_PFX + 'time'): parse_time,
URIRef(_XSD_PFX + 'date'): parse_date,
URIRef(_XSD_PFX + 'dateTime'): parse_datetime,
URIRef(_XSD_PFX + 'string'): None,
URIRef(_XSD_PFX + 'normalizedString'): None,
URIRef(_XSD_PFX + 'token'): None,
URIRef(_XSD_PFX + 'language'): None,
URIRef(_XSD_PFX + 'boolean'): lambda i: i.lower() in ['1', 'true'],
URIRef(_XSD_PFX + 'decimal'): Decimal,
URIRef(_XSD_PFX + 'integer'): long,
URIRef(_XSD_PFX + 'nonPositiveInteger'): int,
URIRef(_XSD_PFX + 'long'): long,
URIRef(_XSD_PFX + 'nonNegativeInteger'): int,
URIRef(_XSD_PFX + 'negativeInteger'): int,
URIRef(_XSD_PFX + 'int'): long,
URIRef(_XSD_PFX + 'unsignedLong'): long,
URIRef(_XSD_PFX + 'positiveInteger'): int,
def deserialize(self, value):
"""
Deserialize the given string as a `~datetime.time`.
"""
if self.format == 'iso8601':
try:
return isodate.parse_time(value)
except isodate.ISO8601Error:
raise ValidationError('invalid ISO 8601 time', value=value)
else:
try:
return datetime.datetime.strptime(value, self.format).time()
except (TypeError, ValueError):
raise ValidationError(
'invalid time, expected format {!r}'.format(self.format),
value=value,
)
>>> validate_time('24:00:00') # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
VCardValueError: Invalid time (See RFC 2425 section 5.8.4 for time syntax)
String: 24:00:00
>>> validate_time('00:00:00Z+01') # doctest: +ELLIPSIS +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
VCardValueError: Invalid time zone ...
String: Z+01
"""
time_timezone = VALID_TIME_WITH_TIMEZONE.match(text)
if time_timezone is None:
raise VCardValueError(NOTE_INVALID_TIME, {'String': text})
time_str, timezone_str = time_timezone.groups()
try:
isodate.parse_time(time_str)
except (isodate.ISO8601Error, ValueError):
raise VCardValueError(NOTE_INVALID_TIME, {'String': text})
if timezone_str == '':
return
validate_time_zone(timezone_str)
def _result_processor(self, value):
try:
return isodate.parse_time(value)
except Exception:
return datetime.time()
@staticmethod
def serialize_time(attr, **kwargs):
"""Serialize Time object into ISO-8601 formatted string.
:param datetime.time attr: Object to be serialized.
:rtype: str
"""
if isinstance(attr, str):
attr = isodate.parse_time(attr)
t = "{:02}:{:02}:{:02}".format(attr.hour, attr.minute, attr.second)
if attr.microsecond:
t += ".{:02}".format(attr.microsecond)
return t