def validate(self, value): try: datetime.datetime.strptime(value, self.format) except ValueError: # FIXME(sbauza): Start_date and end_date are given using a specific # format but are shown in default ISO8601, we must # fail back to it for verification try: wutils.parse_isodatetime(value) except ValueError: raise exceptions.InvalidInput(cls=Datetime.name, value=value) return value
def test_parse_isodatetime(self): good_datetimes = [ ('2008-02-12T12:03:54', datetime.datetime(2008, 2, 12, 12, 3, 54)), ('2012-05-14T23:59:59.000004', datetime.datetime(2012, 5, 14, 23, 59, 59, 4)), ('1856-07-10T01:02:03+00:00', datetime.datetime(1856, 7, 10, 1, 2, 3, 0, pytz.UTC)), ('1856-07-10T01:02:03+23:59', datetime.datetime(1856, 7, 10, 1, 2, 3, 0, pytz.FixedOffset(1439))), ('1856-07-10T01:02:03-23:59', datetime.datetime(1856, 7, 10, 1, 2, 3, 0, pytz.FixedOffset(-1439))), ] ill_formatted_datetimes = [ '24-32-2004', '1856-07-10+33:00' ] out_of_range_datetimes = [ '2008-02-12T32:12:00', '2012-13-12T00:54:60', ] for s, t in good_datetimes: assert utils.parse_isodatetime(s) == t for s in ill_formatted_datetimes + out_of_range_datetimes: self.assertRaises(ValueError, utils.parse_isodatetime, s)
def test_parse_isodatetime(self): good_datetimes = [ ('2008-02-12T12:03:54', datetime.datetime(2008, 2, 12, 12, 3, 54)), ('2012-05-14T23:59:59.000004', datetime.datetime(2012, 5, 14, 23, 59, 59, 4)), ('1856-07-10T01:02:03+00:00', datetime.datetime(1856, 7, 10, 1, 2, 3, 0, pytz.UTC)), ('1856-07-10T01:02:03+23:59', datetime.datetime(1856, 7, 10, 1, 2, 3, 0, pytz.FixedOffset(1439))), ('1856-07-10T01:02:03-23:59', datetime.datetime(1856, 7, 10, 1, 2, 3, 0, pytz.FixedOffset(-1439))), ] ill_formatted_datetimes = ['24-12-2004', '1856-07-10+33:00'] out_of_range_datetimes = [ '2008-02-12T32:12:00', '2012-13-12T00:54:60', '1856-07-10T01:02:03-24:00', '1856-07-10T01:02:03+24:00', ] for s, t in good_datetimes: assert utils.parse_isodatetime(s) == t for s in ill_formatted_datetimes + out_of_range_datetimes: self.assertRaises(ValueError, utils.parse_isodatetime, s)
def loadxml(el, datatype): print (el, datatype, len(el)) if el.get('nil') == 'true': return None if isinstance(datatype, list): return [loadxml(item, datatype[0]) for item in el.findall('item')] elif isarray(datatype): return [ loadxml(item, datatype.item_type) for item in el.findall('item') ] elif isinstance(datatype, dict): key_type, value_type = list(datatype.items())[0] return dict(( (loadxml(item.find('key'), key_type), loadxml(item.find('value'), value_type)) for item in el.findall('item') )) elif isdict(datatype): return dict(( (loadxml(item.find('key'), datatype.key_type), loadxml(item.find('value'), datatype.value_type)) for item in el.findall('item') )) elif isdict(datatype): return dict(( (loadxml(item.find('key'), datatype.key_type), loadxml(item.find('value'), datatype.value_type)) for item in el.findall('item') )) elif len(el): d = {} for attr in datatype._wsme_attributes: name = attr.name child = el.find(name) print (name, attr, child) if child is not None: d[name] = loadxml(child, attr.datatype) print (d) return d else: if datatype == wsme.types.binary: return base64.decodestring(el.text.encode('ascii')) if isusertype(datatype): datatype = datatype.basetype if datatype == datetime.date: return parse_isodate(el.text) if datatype == datetime.time: return parse_isotime(el.text) if datatype == datetime.datetime: return parse_isodatetime(el.text) if datatype == wsme.types.text: return datatype(el.text if el.text else u('')) if datatype == bool: return el.text.lower() != 'false' if datatype is None: return el.text if datatype is wsme.types.bytes: return el.text.encode('ascii') return datatype(el.text)
def patch(self, audit, patch): """Update an existing audit. :param audit: UUID or name of an audit. :param patch: a json PATCH document to apply to this audit. """ if self.from_audits: raise exception.OperationNotPermitted context = pecan.request.context audit_to_update = api_utils.get_resource('Audit', audit, eager=True) policy.enforce(context, 'audit:update', audit_to_update, action='audit:update') try: audit_dict = audit_to_update.as_dict() initial_state = audit_dict['state'] new_state = api_utils.get_patch_value(patch, 'state') if not api_utils.check_audit_state_transition( patch, initial_state): error_message = _("State transition not allowed: " "(%(initial_state)s -> %(new_state)s)") raise exception.PatchError( patch=patch, reason=error_message % dict(initial_state=initial_state, new_state=new_state)) patch_path = api_utils.get_patch_key(patch, 'path') if patch_path in ('start_time', 'end_time'): patch_value = api_utils.get_patch_value(patch, patch_path) # convert string format to UTC time new_patch_value = wutils.parse_isodatetime( patch_value).replace(tzinfo=tz.tzlocal()).astimezone( tz.tzutc()).replace(tzinfo=None) api_utils.set_patch_value(patch, patch_path, new_patch_value) audit = Audit(**api_utils.apply_jsonpatch(audit_dict, patch)) except api_utils.JSONPATCH_EXCEPTIONS as e: raise exception.PatchError(patch=patch, reason=e) # Update only the fields that have changed for field in objects.Audit.fields: try: patch_val = getattr(audit, field) except AttributeError: # Ignore fields that aren't exposed in the API continue if patch_val == wtypes.Unset: patch_val = None if audit_to_update[field] != patch_val: audit_to_update[field] = patch_val audit_to_update.save() return Audit.convert_with_links(audit_to_update)
def loadxml(el, datatype): print(el, datatype, len(el)) if el.get('nil') == 'true': return None if isinstance(datatype, list): return [loadxml(item, datatype[0]) for item in el.findall('item')] elif isarray(datatype): return [ loadxml(item, datatype.item_type) for item in el.findall('item') ] elif isinstance(datatype, dict): key_type, value_type = list(datatype.items())[0] return dict(((loadxml(item.find('key'), key_type), loadxml(item.find('value'), value_type)) for item in el.findall('item'))) elif isdict(datatype): return dict(((loadxml(item.find('key'), datatype.key_type), loadxml(item.find('value'), datatype.value_type)) for item in el.findall('item'))) elif isdict(datatype): return dict(((loadxml(item.find('key'), datatype.key_type), loadxml(item.find('value'), datatype.value_type)) for item in el.findall('item'))) elif len(el): d = {} for attr in datatype._wsme_attributes: name = attr.name child = el.find(name) print(name, attr, child) if child is not None: d[name] = loadxml(child, attr.datatype) print(d) return d else: if datatype == wsme.types.binary: return base64.decodestring(el.text.encode('ascii')) if isusertype(datatype): datatype = datatype.basetype if datatype == datetime.date: return parse_isodate(el.text) if datatype == datetime.time: return parse_isotime(el.text) if datatype == datetime.datetime: return parse_isodatetime(el.text) if datatype == wsme.types.text: return datatype(el.text if el.text else u('')) if datatype == bool: return el.text.lower() != 'false' if datatype is None: return el.text if datatype is wsme.types.bytes: return el.text.encode('ascii') return datatype(el.text)
def test_parse_isodatetime(self): good_datetimes = [ ('2008-02-12T12:03:54', datetime.datetime(2008, 2, 12, 12, 3, 54)), ('2012-05-14T23:59:59.000004', datetime.datetime(2012, 5, 14, 23, 59, 59, 4)), ] ill_formatted_datetimes = [ '24-12-2004' ] out_of_range_datetimes = [ '2008-02-12T32:12:00', '2012-13-12T00:54:60', ] for s, t in good_datetimes: assert utils.parse_isodatetime(s) == t for s in ill_formatted_datetimes + out_of_range_datetimes: self.assertRaises(ValueError, utils.parse_isodatetime, s)
def decode_result(value, datatype): if value is None: return None if datatype == wsme.types.binary: value = base64.decodestring(value.encode('ascii')) return value if isusertype(datatype): datatype = datatype.basetype if isinstance(datatype, list): value = [decode_result(item, datatype[0]) for item in value] elif isarray(datatype): value = [decode_result(item, datatype.item_type) for item in value] elif isinstance(datatype, dict): key_type, value_type = list(datatype.items())[0] value = dict(( (decode_result(key, key_type), decode_result(value, value_type)) for key, value in value.items() )) elif isdict(datatype): key_type, value_type = datatype.key_type, datatype.value_type value = dict(( (decode_result(key, key_type), decode_result(value, value_type)) for key, value in value.items() )) elif datatype == datetime.time: value = parse_isotime(value) elif datatype == datetime.date: value = parse_isodate(value) elif datatype == datetime.datetime: value = parse_isodatetime(value) elif hasattr(datatype, '_wsme_attributes'): for attr in datatype._wsme_attributes: if attr.key not in value: continue value[attr.key] = decode_result(value[attr.key], attr.datatype) elif datatype == decimal.Decimal: value = decimal.Decimal(value) elif datatype == wsme.types.bytes: value = value.encode('ascii') elif datatype is not None and type(value) != datatype: value = datatype(value) return value
def prepare_result(value, datatype): print(value, datatype) if value is None: return None if datatype == wsme.types.binary: return base64.decodestring(value.encode("ascii")) if isusertype(datatype): datatype = datatype.basetype if isinstance(datatype, list): return [prepare_result(item, datatype[0]) for item in value] if isarray(datatype): return [prepare_result(item, datatype.item_type) for item in value] if isinstance(datatype, dict): return dict( ( (prepare_result(item[0], list(datatype.keys())[0]), prepare_result(item[1], list(datatype.values())[0])) for item in value.items() ) ) if isdict(datatype): return dict( ( (prepare_result(item[0], datatype.key_type), prepare_result(item[1], datatype.value_type)) for item in value.items() ) ) if datatype == datetime.date: return parse_isodate(value) if datatype == datetime.time: return parse_isotime(value) if datatype == datetime.datetime: return parse_isodatetime(value) if hasattr(datatype, "_wsme_attributes"): for attr in datatype._wsme_attributes: if attr.key not in value: continue value[attr.key] = prepare_result(value[attr.key], attr.datatype) return value if datatype == wsme.types.bytes: return value.encode("ascii") if type(value) != datatype: print(type(value), datatype) return datatype(value) return value
def prepare_result(value, datatype): print(value, datatype) if value is None: return None if datatype == wsme.types.binary: return base64.decodestring(value.encode('ascii')) if isusertype(datatype): datatype = datatype.basetype if isinstance(datatype, list): return [prepare_result(item, datatype[0]) for item in value] if isarray(datatype): return [prepare_result(item, datatype.item_type) for item in value] if isinstance(datatype, dict): return dict(( (prepare_result(item[0], list(datatype.keys())[0]), prepare_result(item[1], list(datatype.values())[0])) for item in value.items() )) if isdict(datatype): return dict(( (prepare_result(item[0], datatype.key_type), prepare_result(item[1], datatype.value_type)) for item in value.items() )) if datatype == datetime.date: return parse_isodate(value) if datatype == datetime.time: return parse_isotime(value) if datatype == datetime.datetime: return parse_isodatetime(value) if hasattr(datatype, '_wsme_attributes'): for attr in datatype._wsme_attributes: if attr.key not in value: continue value[attr.key] = prepare_result(value[attr.key], attr.datatype) return value if datatype == wsme.types.bytes: return value.encode('ascii') if type(value) != datatype: print(type(value), datatype) return datatype(value) return value
def datetime_from_param(datatype, value): return parse_isodatetime(value) if value else None
def datetime_fromjson(datatype, value): if value is None or value == '': return None return parse_isodatetime(value)
def datetime_fromsoap(datatype, el, ns): if el.get(nil_qn) == 'true': return None if el.get(type_qn) not in (None, 'xs:dateTime'): raise exc.InvalidInput(el.tag, ET.tostring(el)) return parse_isodatetime(el.text)