|
| 1 | +# coding=utf-8 |
| 2 | +""" |
| 3 | +Base Model |
| 4 | +
|
| 5 | +""" |
| 6 | +import collections |
| 7 | + |
| 8 | + |
| 9 | +class Model(collections.Iterable): |
| 10 | + """A model contains an __attribute__ map that defines the name, |
| 11 | + its type for type validation, an optional validation method, a method |
| 12 | + used to |
| 13 | +
|
| 14 | + .. python:: |
| 15 | +
|
| 16 | + class MyModel(Model): |
| 17 | +
|
| 18 | + __attributes__ = { |
| 19 | + 'ID': { |
| 20 | + 'type': uuid.UUID, |
| 21 | + 'required': False, |
| 22 | + 'default': None, |
| 23 | + 'cast_from': str, |
| 24 | + 'cast_to': str |
| 25 | + }, |
| 26 | + 'Serial': { |
| 27 | + 'type': int |
| 28 | + 'required': True, |
| 29 | + 'default': 0, |
| 30 | + 'validator': lambda v: v >= 0 end, |
| 31 | + } |
| 32 | + } |
| 33 | +
|
| 34 | + """ |
| 35 | + |
| 36 | + __attributes__ = {} |
| 37 | + """The attributes that define the data elements of the model""" |
| 38 | + |
| 39 | + def __init__(self, **kwargs): |
| 40 | + super(Model, self).__init__() |
| 41 | + [setattr(self, key, value) for key, value in kwargs.items()] |
| 42 | + [self._set_default(k) for k in self.__attributes__.keys() |
| 43 | + if k not in kwargs.keys()] |
| 44 | + |
| 45 | + def __iter__(self): |
| 46 | + """Iterate through the model's key, value pairs. |
| 47 | +
|
| 48 | + :rtype: iterator |
| 49 | +
|
| 50 | + """ |
| 51 | + for name in self.__attributes__.keys(): |
| 52 | + value = self._maybe_cast_value(name) |
| 53 | + if value is not None: |
| 54 | + yield self._maybe_return_key(name), value |
| 55 | + |
| 56 | + def __setattr__(self, name, value): |
| 57 | + """Set the value for an attribute of the model, validating the |
| 58 | + attribute name and its type if the type is defined in ``__types__``. |
| 59 | +
|
| 60 | + :param str name: The attribute name |
| 61 | + :param mixed value: The value to set |
| 62 | + :raises: AttributeError |
| 63 | + :raises: TypeError |
| 64 | + :raises: ValueError |
| 65 | +
|
| 66 | + """ |
| 67 | + if name not in self.__attributes__: |
| 68 | + raise AttributeError('Invalid attribute "{}"'.format(name)) |
| 69 | + value = self._validate_value(name, value) |
| 70 | + super(Model, self).__setattr__(name, value) |
| 71 | + |
| 72 | + def _maybe_cast_value(self, name): |
| 73 | + """Return the attribute value, possibly cast to a different type if |
| 74 | + the ``cast_to`` item is set in the attribute definition. |
| 75 | +
|
| 76 | + :param str name: The attribute name |
| 77 | + :rtype: mixed |
| 78 | +
|
| 79 | + """ |
| 80 | + value = getattr(self, name) |
| 81 | + if value is not None and self.__attributes__[name].get('cast_to'): |
| 82 | + return self.__attributes__[name]['cast_to'](value) |
| 83 | + return value |
| 84 | + |
| 85 | + def _maybe_return_key(self, name): |
| 86 | + """Return the attribute name as specified in it's ``key`` definition, |
| 87 | + if specified. This is to map python attribute names to their Consul |
| 88 | + alternatives. |
| 89 | +
|
| 90 | + :param str name: The attribute name |
| 91 | + :rtype: mixed |
| 92 | +
|
| 93 | + """ |
| 94 | + if self.__attributes__[name].get('key'): |
| 95 | + return self.__attributes__[name]['key'] |
| 96 | + return name |
| 97 | + |
| 98 | + def _required_attr(self, name): |
| 99 | + """Returns :data:`True` if the attribute is required. |
| 100 | +
|
| 101 | + :param str name: The attribute name |
| 102 | + :rtype: bool |
| 103 | +
|
| 104 | + """ |
| 105 | + return self.__attributes__[name].get('required', False) |
| 106 | + |
| 107 | + def _set_default(self, name): |
| 108 | + """Set the default value for the attribute name. |
| 109 | +
|
| 110 | + :param str name: The attribute name |
| 111 | +
|
| 112 | + """ |
| 113 | + setattr(self, name, self.__attributes__[name].get('default', None)) |
| 114 | + |
| 115 | + def _validate_value(self, name, value): |
| 116 | + """Ensures the the value validates based upon the type or a validation |
| 117 | + function, raising an error if it does not. |
| 118 | +
|
| 119 | + :param str name: The attribute name |
| 120 | + :param mixed value: The value that is being set |
| 121 | + :rtype: mixed |
| 122 | + :raises: TypeError |
| 123 | + :raises: ValueError |
| 124 | +
|
| 125 | + """ |
| 126 | + if value is None and self._required_attr(name): |
| 127 | + raise ValueError('Attribute "{}" is required'.format(name)) |
| 128 | + |
| 129 | + if value is not None: |
| 130 | + if not isinstance(value, self.__attributes__[name].get('type')): |
| 131 | + cast_from = self.__attributes__[name].get('cast_from') |
| 132 | + if cast_from and isinstance(value, cast_from): |
| 133 | + value = self.__attributes__[name]['type'](value) |
| 134 | + else: |
| 135 | + raise TypeError( |
| 136 | + 'Attribute "{}" must be of type {} not {}'.format( |
| 137 | + name, self.__attributes__[name]['type'].__name__, |
| 138 | + value.__class__.__name__)) |
| 139 | + |
| 140 | + if self.__attributes__[name].get('enum') \ |
| 141 | + and value not in self.__attributes__[name]['enum']: |
| 142 | + raise ValueError( |
| 143 | + 'Attribute "{}" value {!r} not valid'.format(name, value)) |
| 144 | + |
| 145 | + validator = self.__attributes__[name].get('validator') |
| 146 | + if callable(validator): |
| 147 | + if not validator(value): |
| 148 | + raise ValueError( |
| 149 | + 'Attribute "{}" value {!r} did not validate'.format( |
| 150 | + name, value)) |
| 151 | + return value |
0 commit comments