Break up core pieces into separate modules
- Create model.py to hold the core Model class - Create exceptions.py to hold all custom exceptions
This commit is contained in:
parent
e8f0a5ade7
commit
98a44c6601
|
@ -3,3 +3,5 @@ build/
|
||||||
dist/
|
dist/
|
||||||
warlock.egg-info/
|
warlock.egg-info/
|
||||||
.tox/
|
.tox/
|
||||||
|
*.swp
|
||||||
|
*.pyc
|
||||||
|
|
|
@ -2,4 +2,4 @@
|
||||||
|
|
||||||
# pylint: disable=W0611
|
# pylint: disable=W0611
|
||||||
from warlock.core import model_factory
|
from warlock.core import model_factory
|
||||||
from warlock.core import InvalidOperation
|
from warlock.exceptions import InvalidOperation
|
||||||
|
|
114
warlock/core.py
114
warlock/core.py
|
@ -3,18 +3,12 @@
|
||||||
import copy
|
import copy
|
||||||
|
|
||||||
import jsonschema
|
import jsonschema
|
||||||
import jsonpatch
|
|
||||||
|
import model
|
||||||
|
import exceptions
|
||||||
|
|
||||||
|
|
||||||
class InvalidOperation(RuntimeError):
|
def model_factory(schema, base_class=model.Model):
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class ValidationError(ValueError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def model_factory(schema):
|
|
||||||
"""Generate a model class based on the provided JSON Schema
|
"""Generate a model class based on the provided JSON Schema
|
||||||
|
|
||||||
:param schema: dict representing valid JSON schema
|
:param schema: dict representing valid JSON schema
|
||||||
|
@ -26,104 +20,12 @@ def model_factory(schema):
|
||||||
try:
|
try:
|
||||||
jsonschema.validate(obj, schema)
|
jsonschema.validate(obj, schema)
|
||||||
except jsonschema.ValidationError as exc:
|
except jsonschema.ValidationError as exc:
|
||||||
raise ValidationError(str(exc))
|
raise exceptions.ValidationError(str(exc))
|
||||||
|
|
||||||
class Model(dict):
|
|
||||||
"""Self-validating model for arbitrary objects"""
|
|
||||||
|
|
||||||
|
class Model(base_class):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
d = dict(*args, **kwargs)
|
kwargs.setdefault('validator', validator)
|
||||||
|
base_class.__init__(self, *args, **kwargs)
|
||||||
# we overload setattr so set this manually
|
|
||||||
self.__dict__['validator'] = validator
|
|
||||||
try:
|
|
||||||
self.validator(d)
|
|
||||||
except ValidationError as exc:
|
|
||||||
raise ValueError(str(exc))
|
|
||||||
else:
|
|
||||||
dict.__init__(self, d)
|
|
||||||
|
|
||||||
self.__dict__['changes'] = {}
|
|
||||||
self.__dict__['__original__'] = copy.deepcopy(d)
|
|
||||||
|
|
||||||
def __getattr__(self, key):
|
|
||||||
try:
|
|
||||||
return self.__getitem__(key)
|
|
||||||
except KeyError:
|
|
||||||
raise AttributeError(key)
|
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
|
||||||
mutation = dict(self.items())
|
|
||||||
mutation[key] = value
|
|
||||||
try:
|
|
||||||
self.validator(mutation)
|
|
||||||
except ValidationError:
|
|
||||||
msg = "Unable to set '%s' to '%s'" % (key, value)
|
|
||||||
raise InvalidOperation(msg)
|
|
||||||
|
|
||||||
dict.__setitem__(self, key, value)
|
|
||||||
|
|
||||||
self.__dict__['changes'][key] = value
|
|
||||||
|
|
||||||
def __setattr__(self, key, value):
|
|
||||||
self.__setitem__(key, value)
|
|
||||||
|
|
||||||
def clear(self):
|
|
||||||
raise InvalidOperation()
|
|
||||||
|
|
||||||
def pop(self, key, default=None):
|
|
||||||
raise InvalidOperation()
|
|
||||||
|
|
||||||
def popitem(self):
|
|
||||||
raise InvalidOperation()
|
|
||||||
|
|
||||||
def __delitem__(self, key):
|
|
||||||
mutation = dict(self.items())
|
|
||||||
del mutation[key]
|
|
||||||
try:
|
|
||||||
self.validator(mutation)
|
|
||||||
except ValidationError:
|
|
||||||
msg = "Unable to delete attribute '%s'" % (key)
|
|
||||||
raise InvalidOperation(msg)
|
|
||||||
|
|
||||||
dict.__delitem__(self, key)
|
|
||||||
|
|
||||||
def __delattr__(self, key):
|
|
||||||
self.__delitem__(key)
|
|
||||||
|
|
||||||
# NOTE(termie): This is kind of the opposite of what copy usually does
|
|
||||||
def copy(self):
|
|
||||||
return copy.deepcopy(dict(self))
|
|
||||||
|
|
||||||
def update(self, other):
|
|
||||||
mutation = dict(self.items())
|
|
||||||
mutation.update(other)
|
|
||||||
try:
|
|
||||||
self.validator(mutation)
|
|
||||||
except ValidationError as exc:
|
|
||||||
raise InvalidOperation(str(exc))
|
|
||||||
dict.update(self, other)
|
|
||||||
|
|
||||||
def iteritems(self):
|
|
||||||
return copy.deepcopy(dict(self)).iteritems()
|
|
||||||
|
|
||||||
def items(self):
|
|
||||||
return copy.deepcopy(dict(self)).items()
|
|
||||||
|
|
||||||
def itervalues(self):
|
|
||||||
return copy.deepcopy(dict(self)).itervalues()
|
|
||||||
|
|
||||||
def values(self):
|
|
||||||
return copy.deepcopy(dict(self)).values()
|
|
||||||
|
|
||||||
@property
|
|
||||||
def changes(self):
|
|
||||||
return copy.deepcopy(self.__dict__['changes'])
|
|
||||||
|
|
||||||
@property
|
|
||||||
def patch(self):
|
|
||||||
original = self.__dict__['__original__']
|
|
||||||
return jsonpatch.make_patch(original, dict(self)).to_string()
|
|
||||||
|
|
||||||
Model.__name__ = str(schema['name'])
|
Model.__name__ = str(schema['name'])
|
||||||
return Model
|
return Model
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
""" List of errors used in warlock """
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidOperation(RuntimeError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ValidationError(ValueError):
|
||||||
|
pass
|
|
@ -0,0 +1,108 @@
|
||||||
|
"""Self-validating model for arbitrary objects"""
|
||||||
|
|
||||||
|
import copy
|
||||||
|
|
||||||
|
import jsonpatch
|
||||||
|
|
||||||
|
import exceptions
|
||||||
|
|
||||||
|
|
||||||
|
class Model(dict):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
# Load the validator from the kwargs
|
||||||
|
#self.__dict__['validator'] = kwargs.pop('validator', self.default_validator)
|
||||||
|
|
||||||
|
# we overload setattr so set this manually
|
||||||
|
d = dict(*args, **kwargs)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.validator(d)
|
||||||
|
except exceptions.ValidationError as exc:
|
||||||
|
raise ValueError(str(exc))
|
||||||
|
else:
|
||||||
|
dict.__init__(self, d)
|
||||||
|
|
||||||
|
self.__dict__['changes'] = {}
|
||||||
|
self.__dict__['__original__'] = copy.deepcopy(d)
|
||||||
|
|
||||||
|
def __getattr__(self, key):
|
||||||
|
try:
|
||||||
|
return self.__getitem__(key)
|
||||||
|
except KeyError:
|
||||||
|
raise AttributeError(key)
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
mutation = dict(self.items())
|
||||||
|
mutation[key] = value
|
||||||
|
try:
|
||||||
|
self.validator(mutation)
|
||||||
|
except exceptions.ValidationError:
|
||||||
|
msg = "Unable to set '%s' to '%s'" % (key, value)
|
||||||
|
raise exceptions.InvalidOperation(msg)
|
||||||
|
|
||||||
|
dict.__setitem__(self, key, value)
|
||||||
|
|
||||||
|
self.__dict__['changes'][key] = value
|
||||||
|
|
||||||
|
def __setattr__(self, key, value):
|
||||||
|
self.__setitem__(key, value)
|
||||||
|
|
||||||
|
def clear(self):
|
||||||
|
raise exceptions.InvalidOperation()
|
||||||
|
|
||||||
|
def pop(self, key, default=None):
|
||||||
|
raise exceptions.InvalidOperation()
|
||||||
|
|
||||||
|
def popitem(self):
|
||||||
|
raise exceptions.InvalidOperation()
|
||||||
|
|
||||||
|
def __delitem__(self, key):
|
||||||
|
mutation = dict(self.items())
|
||||||
|
del mutation[key]
|
||||||
|
try:
|
||||||
|
self.validator(mutation)
|
||||||
|
except exceptions.ValidationError:
|
||||||
|
msg = "Unable to delete attribute '%s'" % (key)
|
||||||
|
raise exceptions.InvalidOperation(msg)
|
||||||
|
|
||||||
|
dict.__delitem__(self, key)
|
||||||
|
|
||||||
|
def __delattr__(self, key):
|
||||||
|
self.__delitem__(key)
|
||||||
|
|
||||||
|
# NOTE(termie): This is kind of the opposite of what copy usually does
|
||||||
|
def copy(self):
|
||||||
|
return copy.deepcopy(dict(self))
|
||||||
|
|
||||||
|
def update(self, other):
|
||||||
|
mutation = dict(self.items())
|
||||||
|
mutation.update(other)
|
||||||
|
try:
|
||||||
|
self.validator(mutation)
|
||||||
|
except exceptions.ValidationError as exc:
|
||||||
|
raise exceptions.InvalidOperation(str(exc))
|
||||||
|
dict.update(self, other)
|
||||||
|
|
||||||
|
def iteritems(self):
|
||||||
|
return copy.deepcopy(dict(self)).iteritems()
|
||||||
|
|
||||||
|
def items(self):
|
||||||
|
return copy.deepcopy(dict(self)).items()
|
||||||
|
|
||||||
|
def itervalues(self):
|
||||||
|
return copy.deepcopy(dict(self)).itervalues()
|
||||||
|
|
||||||
|
def values(self):
|
||||||
|
return copy.deepcopy(dict(self)).values()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def changes(self):
|
||||||
|
return copy.deepcopy(self.__dict__['changes'])
|
||||||
|
|
||||||
|
@property
|
||||||
|
def patch(self):
|
||||||
|
original = self.__dict__['__original__']
|
||||||
|
return jsonpatch.make_patch(original, dict(self)).to_string()
|
||||||
|
|
||||||
|
def default_validator(self, *args, **kwargs):
|
||||||
|
return True
|
Loading…
Reference in New Issue