diff --git a/mongokit/schema_document.py b/mongokit/schema_document.py index b26e481..ec9fbf9 100644 --- a/mongokit/schema_document.py +++ b/mongokit/schema_document.py @@ -25,40 +25,40 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -import bson import datetime +import re import logging from copy import deepcopy log = logging.getLogger(__name__) -from mongokit.operators import SchemaOperator, IS -from mongokit.helpers import DotCollapsedDict -from mongokit.helpers import DotExpandedDict -from mongokit.helpers import i18nDotedDict -from mongokit.helpers import DotedDict +from operators import SchemaOperator, IS +from helpers import * __all__ = [ - 'AuthorizedTypeError', - 'BadKeyError', 'CustomType', - 'DefaultFieldTypeError', - 'DotCollapsedDict', + 'SchemaProperties', + 'SchemaDocument', 'DotedDict', 'DotExpandedDict', - 'DuplicateDefaultValueError', + 'DotCollapsedDict', + 'RequireFieldError', + 'StructureError', + 'BadKeyError', + 'AuthorizedTypeError', + 'ValidationError', 'DuplicateRequiredError', - 'i18n', - 'i18nError', + 'DuplicateDefaultValueError', 'ModifierOperatorError', - 'RequireFieldError', 'SchemaDocument', - 'SchemaDocumentError', - 'SchemaProperties', 'SchemaTypeError', - 'Set', - 'StructureError', - 'ValidationError', + 'DefaultFieldTypeError', + 'totimestamp', + 'fromtimestamp', + 'i18n', + 'i18nError', + 'EvalException', + 'Set' ] @@ -146,16 +146,8 @@ class i18nError(SchemaDocumentError): pass -class DeprecationError(Exception): - pass - - -class DuplicateI18nError(Exception): - pass - - class SchemaProperties(type): - def __new__(mcs, name, bases, attrs): + def __new__(cls, name, bases, attrs): attrs['_protected_field_names'] = set( ['_protected_field_names', '_namespaces', '_required_namespace']) for base in bases: @@ -198,7 +190,7 @@ def __new__(mcs, name, bases, attrs): attrs['_namespaces'] = list(base._SchemaDocument__walk_dict(attrs['structure'])) if [1 for i in attrs['_namespaces'] if type(i) is type]: raise DeprecationError("%s: types are not allowed as structure key anymore" % name) - mcs._validate_descriptors(attrs) + cls._validate_descriptors(attrs) ## building required fields namespace attrs['_required_namespace'] = set([]) for rf in attrs.get('required_fields', []): @@ -212,10 +204,10 @@ def __new__(mcs, name, bases, attrs): attrs['_i18n_namespace'] = [] if attrs.get('i18n'): attrs['_i18n_namespace'] = set(['.'.join(i.split('.')[:-1]) for i in attrs['i18n']]) - return type.__new__(mcs, name, bases, attrs) + return type.__new__(cls, name, bases, attrs) @classmethod - def _validate_descriptors(mcs, attrs): + def _validate_descriptors(cls, attrs): # TODO i18n validator for dv in attrs.get('default_values', {}): if not dv in attrs['_namespaces']: @@ -234,9 +226,9 @@ def _validate_descriptors(mcs, attrs): if attrs.get('i18n'): if len(attrs['i18n']) != len(set(attrs['i18n'])): raise DuplicateI18nError("duplicated i18n : %s" % attrs['i18n']) - for _i18n in attrs['i18n']: - if _i18n not in attrs['_namespaces']: - raise ValueError("Error in i18n: can't find {} in structure".format(_i18n)) + for i18n in attrs['i18n']: + if i18n not in attrs['_namespaces']: + raise ValueError("Error in i18n: can't find %s in structure" % i18n) class SchemaDocument(dict): @@ -340,11 +332,10 @@ class SchemaDocument(dict): list, dict, datetime.datetime, - bson.binary.Binary, CustomType, ] - def __init__(self, doc=None, gen_skel=True, _gen_auth_types=True, _validate=True, lang='en', fallback_lang='en'): + def __init__(self, doc=None, gen_skel=True, gen_auth_types=True, validate=True, lang='en', fallback_lang='en'): """ doc : a dictionary gen_skel : if True, generate automatically the skeleton of the doc @@ -354,7 +345,6 @@ def __init__(self, doc=None, gen_skel=True, _gen_auth_types=True, _validate=True gen_auth_types: if True, generate automatically the self.authorized_types attribute from self.authorized_types """ - super(SchemaDocument, self).__init__() if self.structure is None: self.structure = {} self._current_lang = lang @@ -412,7 +402,7 @@ def __setattr__(self, key, value): else: if self.dot_notation_warning and not key.startswith('_') and key not in \ ['db', 'collection', 'versioning_collection', 'connection', 'fs']: - log.warning("dot notation: {} was not found in structure. Add it as attribute instead".format(key)) + log.warning("dot notation: %s was not found in structure. Add it as attribute instead" % key) dict.__setattr__(self, key, value) def __getattr__(self, key): @@ -482,7 +472,7 @@ def _validate_structure(cls, structure, name, authorized_types): validate if all fields in self.structure are in authorized types. """ ############## - def __validate_structure(struct, name, _authorized): + def __validate_structure(struct, name, authorized): if type(struct) is type: if struct not in authorized_types: if struct not in authorized_types: @@ -650,7 +640,8 @@ def _validate_doc(self, doc, struct, path=""): for i in range(len(struct)): self._validate_doc(doc[i], struct[i], path) - def _process_validators(self, doc, _struct, _path=""): + def _process_validators(self, doc, struct, path=""): + doted_struct = DotCollapsedDict(self.structure) doted_doc = DotCollapsedDict(doc) for key, validators in self.validators.iteritems(): if key in doted_doc and doted_doc[key] is not None: @@ -659,10 +650,10 @@ def _process_validators(self, doc, _struct, _path=""): for validator in validators: try: if not validator(doted_doc[key]): - raise ValidationError("%s does not pass the validator " + validator.__name__) + raise ValidationError("%s does not pass the validator %s" % (key, validator.__name__)) except Exception, e: self._raise_exception(ValidationError, key, - unicode(e) % key) + unicode(e)) def _process_custom_type(self, target, doc, struct, path="", root_path=""): for key in struct: @@ -807,7 +798,7 @@ def _set_default_fields(self, doc, struct, path=""): else: doc[key] = new_value - def _validate_required(self, doc, _struct, _path="", _root_path=""): + def _validate_required(self, doc, struct, path="", root_path=""): doted_struct = DotCollapsedDict(self.structure) doted_doc = DotCollapsedDict(doc, reference=doted_struct) for req in self.required_fields: @@ -855,7 +846,7 @@ def __generate_skeleton(self, doc, struct, path=""): elif struct[key] is list: doc[key] = [] elif isinstance(struct[key], tuple): - doc[key] = [None for _ in range(len(struct[key]))] + doc[key] = [None for i in range(len(struct[key]))] else: doc[key] = None #