def __getitem__(cls, val): if isinstance(val, Field): return val elif Field in getattr(val, "__mro__", {}): return val() elif Structure in getattr(val, "__mro__", {}): return ClassReference(val) elif is_function_returning_field(val): return val() elif val is None: return NoneField() else: def get_state(value): raise TypeError( "pickling of implicit wrappers for non-Typedpy fields are unsupported" ) if not isinstance(val, type): raise TypeError( "Unsupported field type in definition: {}".format(wrap_val(val)) ) the_class = val.__name__ if the_class in _FieldMeta._registry: return _FieldMeta._registry[the_class] short_hash = hashlib.sha256(the_class.encode("utf-8")).hexdigest()[:8] new_name = "Field_{}_{}".format(the_class, short_hash) class_as_field = create_typed_field(new_name, val) class_as_field.__getstate__ = get_state _FieldMeta._registry[the_class] = class_as_field return class_as_field()
def __manage_uniqueness_for_field__(self, instance, value): if not getattr(instance, "_instantiated", False): return field_name = getattr(self, "_name") structure_class_name = instance.__class__.__name__ all_instances_by_struct_name = getattr( self, "_ALL_INSTANCES", defaultdict(dict) ) instance_by_value_for_current_struct = all_instances_by_struct_name[ structure_class_name ] if ( getattr(self, MUST_BE_UNIQUE, False) and len(instance_by_value_for_current_struct) < MAX_NUMBER_OF_INSTANCES_TO_VERIFY_UNIQUENESS ): hash_of_field_val = value.__hash__() if ( instance_by_value_for_current_struct.get(hash_of_field_val, instance) != instance ): raise ValueError( "Instance copy of field {} in {}, which is defined as unique. Instance is {}".format( field_name, structure_class_name, wrap_val(value) ) ) if hash_of_field_val not in instance_by_value_for_current_struct: instance_by_value_for_current_struct[hash_of_field_val] = instance
def _validate(self, value): def err_prefix(): return "{}: ".format(self._name) if self._name else "" if not isinstance(value, self._ty) and value is not None: raise TypeError( "{}Expected {}; Got {}".format(err_prefix(), self._ty, wrap_val(value)) )
def _try_default_value(self, default): try: self.__set__(Structure(), default) except Exception as e: raise e.__class__( "Invalid default value: {}; Reason: {}".format( wrap_val(default), str(e) ) ) from e
def __set__(self, instance, value): super().__set__(instance, value) try: datetime.strptime(value, "%H:%M:%S") except ValueError as ex: raise ValueError("{}: Got {}; {}".format(self._name, wrap_val(value), ex.args[0]))
def __set__(self, instance, value): if not HostName._host_name_re.match(value): raise ValueError("{}: Got {}; wrong format for hostname".format(self._name, wrap_val(value))) components = value.split(".") for component in components: if len(component) > 63: raise ValueError("{}: Got {}; wrong format for hostname".format(self._name, wrap_val(value))) super().__set__(instance, value)
def __set__(self, instance, value): if IPV4._ipv4_re.match(value) and all( 0 <= int(component) <= 255 for component in value.split(".") ): super().__set__(instance, value) else: raise ValueError("{}: Got {}; wrong format for IP version 4".format(self._name, wrap_val(value)))
def __set__(self, instance, value): if isinstance(value, str): as_datetime = self.deserialize(value) super().__set__(instance, as_datetime) elif isinstance(value, datetime): super().__set__(instance, value) else: raise TypeError("{}: Got {}; Expected datetime or str".format(self._name, wrap_val(value)))
def deserialize(self, value): try: return datetime.strptime(value, self._datetime_format) except ValueError as ex: raise ValueError("{}: Got {}; {}".format(self._name, wrap_val(value), str(ex))) from ex