def _validate(self, value): if self._max and value > self._max: raise FieldConfigurationError.ExceedsMax(value=value, max=self._max) if self._min and value < self._min: raise FieldConfigurationError.ExceedsMin(value=value, min=self._min) return value
def _validate(self, value): try: float(value) except ValueError: raise FieldConfigurationError.ExpectedType(value, int) else: value = int(value) if value != float(value): raise FieldConfigurationError.ExpectedType(value, int) return super(IntField, self).validate(value)
def __addfields__(self, **fields): """ Method that is not supposed to be used outside of the configuration of system settings. Adds fields to the SetField after it is first initialized. The reasoning is that after the SetField is initialized, the only way to make changes is to configure it, which is not what we want to do. This is useful when we have to add fields that have values that depend on fields already initialized: INSTAGRAM = fields.SetField( URLS=fields.SetField( HOME='https://www.instagram.com/', configurable=False, ), ) HEADERS = fields.PersistentDictField({ 'Referer': INSTAGRAM.urls.home, "User-Agent": USERAGENT, }) INSTAGRAM.__addfields__(HEADERS=HEADERES) """ for k, v in fields.items(): if k in self: raise FieldConfigurationError.FieldAlreadySet(k) self.__setitem__(k, v)
def wrapped(instance, value): if value is None: if not instance.optional: raise FieldConfigurationError.RequiredField() else: return None return func(instance, value)
def value(self, val): if not self.configurable: raise FieldConfigurationError.NonConfigurableField( ext= "Configurability should be checked by container and not field itself." ) val = self._validate(val) self._value = val
def _validate(self, v): if isinstance(v, dict): raise FieldConfigurationError.UnexpectedType( v, dict, ext='ConstantField(s) cannot be configured with dict instances.' ) return v
def _validate_element_key(self, k): """ [x] TODO: -------- Differentiate between expected types of fields vs. values in the FieldConfigurationError instantiations. """ tp = self._keys.get('type') or self._keys.get('types') if tp: types = ensure_iterable(tp, coercion=tuple, force_coerce=True) if not isinstance(k, types): raise FieldConfigurationError.ExpectedType(k, *types) allowed = self._keys.get('allowed') if allowed: if k not in allowed: raise FieldConfigurationError.DisallowedKey(key=k)
def configure(self, *args, **kwargs): """ Overrides the fields that belong to the SetField. (1) Cannot add fields that are not already present. - This would mean the addition of configuration settings instead of the configuring of existing settings. (2) Do Not Allow Configuration w/ Fields - Initializing the SetField requires the values to be Field instances or constants (converted to ConstantField(s)), whereas configuring requires constant values specified in a dict. >>> field.configure({'colors': {'apple': 'red'}}) (3) Child fields being overridden must be configurable, as well as the parent SetField (self). """ # Either the LazySettings object, or the parent SetField container should # be checking configurability of children fields before configuring. # However, if we configure by settings.<field_name>.configure(), we still # need this check. if not self.configurable: raise FieldConfigurationError.NonConfigurableField( field=self.name, ) for k, v in dict(*args, **kwargs).items(): # (1) Cannot add fields that are not already present. if k not in self: raise FieldConfigurationError.CannotAddField(field=k) field = self.__getfield__(k) assert isinstance(field, FieldABC) # (2) Do Not Allow Configuration w/ Fields - Only on Initialization if isinstance(v, FieldABC): raise FieldConfigurationError.UnexpectedFieldInstance(field=k) # (3) Child fields being overridden must be configurable if not field.configurable: raise FieldConfigurationError.NonConfigurableField(field=k) field.configure(v)
def _validate(self, value): """ We do not allow a DictField to store instances of other fields, that would be more appropriate for a SetField. """ if not isinstance(value, dict): raise FieldConfigurationError.ExpectedType(value, dict) for k, v in value.items(): if isinstance(v, FieldABC): raise FieldConfigurationError.UnexpectedFieldInstance(field=k) # Should this be done in configure instead? Should we even have this? if k not in self.value: raise FieldConfigurationError.CannotAddField(field=k) self._validate_element_val(v) self._validate_element_key(k) return value
def __setitem__(self, key, value): """ Sets a child sub field of the SetField, all value(s) must be instances of Field. If the configurability of the parent SetField is set (i.e. not None) and it conflicts with the configurability of a child, we have to raise an exception. [x] NOTE: -------- Cannot check configurability here because __setitem__ gets called on initial update as well as on configure. """ if not isinstance(value, FieldABC): raise FieldConfigurationError.ExpectedFieldInstance() if self._configurable is not None: if value.configurable and not self._configurable: raise FieldConfigurationError.CannotAddConfigurableField( field=key) value = self._validate(value) super(SetField, self).__setitem__(self.__keytransform__(key), value)
def update(self, *args, **kwargs): """ Sets the fields that belong to the SetField. This is only done once, on initialization. Updating of the SetField afterwards must be done through the configure method. Updating the SetField requires the values to be Field instances, whereas configuring requires dict instances. """ for k, v in dict(*args, **kwargs).items(): if k in self: raise FieldConfigurationError.FieldAlreadySet(field=k) if not isinstance(v, FieldABC): v = ConstantField(v) self.__setitem__(k, v)
def _validate(self, value): if not isinstance(value, bool): raise FieldConfigurationError.ExpectedType(value, bool) return value