def check_global_invariants(subject, invariants): error_codes = tuple(error_code for is_ok, error_code in (invariant(subject) for invariant in invariants) if not is_ok) if error_codes: raise InvariantException(error_codes, (), 'Global invariant failed')
def persistent(self): cls = self._destination_cls is_dirty = self.is_dirty() pm = super(_PRecordEvolver, self).persistent() if is_dirty or not isinstance(pm, cls): result = cls(_precord_buckets=pm._buckets, _precord_size=pm._size) else: result = pm if cls._precord_mandatory_fields: self._missing_fields += tuple( "{0}.{1}".format(cls.__name__, f) for f in (cls._precord_mandatory_fields - set(result.keys())) ) if self._invariant_error_codes or self._missing_fields: raise InvariantException( tuple(self._invariant_error_codes), tuple(self._missing_fields), "Field invariant failed", ) check_global_invariants(result, cls._precord_invariants) return result
def __new__(cls, **kwargs): # Support *args? result = super(PClass, cls).__new__(cls) missing_fields = [] invariant_errors = [] for name, field in cls._pclass_fields.items(): if name in kwargs: value = field.factory(kwargs[name]) _check_and_set_attr(cls, field, name, value, result, invariant_errors) del kwargs[name] elif field.initial is not PFIELD_NO_INITIAL: initial = field.initial() if callable(field.initial) else field.initial _check_and_set_attr( cls, field, name, initial, result, invariant_errors) elif field.mandatory: missing_fields.append('{0}.{1}'.format(cls.__name__, name)) if invariant_errors or missing_fields: raise InvariantException(tuple(invariant_errors), tuple(missing_fields), 'Field invariant failed') if kwargs: raise AttributeError("'{0}' are not among the specified fields for {1}".format( ', '.join(kwargs), cls.__name__)) check_global_invariants(result, cls._pclass_invariants) result._pclass_frozen = True return result
def __new__(cls, **kwargs): # Support *args? result = super(PClass, cls).__new__(cls) factory_fields = kwargs.pop("_factory_fields", None) ignore_extra = kwargs.pop("ignore_extra", None) missing_fields = [] invariant_errors = [] for name, field in cls._pclass_fields.items(): if name in kwargs: if factory_fields is None or name in factory_fields: if is_field_ignore_extra_complaint(PClass, field, ignore_extra): value = field.factory(kwargs[name], ignore_extra=ignore_extra) else: value = field.factory(kwargs[name]) else: value = kwargs[name] _check_and_set_attr(cls, field, name, value, result, invariant_errors) del kwargs[name] elif field.initial is not PFIELD_NO_INITIAL: initial = field.initial() if callable( field.initial) else field.initial _check_and_set_attr(cls, field, name, initial, result, invariant_errors) elif field.mandatory: missing_fields.append("{0}.{1}".format(cls.__name__, name)) if invariant_errors or missing_fields: raise InvariantException(tuple(invariant_errors), tuple(missing_fields), "Field invariant failed") if kwargs: raise AttributeError( "'{0}' are not among the specified fields for {1}".format( ", ".join(kwargs), cls.__name__)) check_global_invariants(result, cls._pclass_invariants) result._pclass_frozen = True return result