From 1abeed3df5d6144cc0be3bfad8b86d3bd9e12361 Mon Sep 17 00:00:00 2001 From: Hassan Jannah Date: Sat, 24 Jan 2015 23:28:27 -0800 Subject: [PATCH 1/6] Update schema_document.py --- mongokit/schema_document.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mongokit/schema_document.py b/mongokit/schema_document.py index b26e481..180fb7b 100644 --- a/mongokit/schema_document.py +++ b/mongokit/schema_document.py @@ -650,7 +650,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 +660,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 "%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: From 947baec8cf9f5ebd547b6e4de1ed580cd88cf364 Mon Sep 17 00:00:00 2001 From: Hassan Jannah Date: Tue, 27 Jan 2015 21:33:40 -0800 Subject: [PATCH 2/6] Update schema_document.py --- mongokit/schema_document.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mongokit/schema_document.py b/mongokit/schema_document.py index 180fb7b..9530fcd 100644 --- a/mongokit/schema_document.py +++ b/mongokit/schema_document.py @@ -660,7 +660,7 @@ 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 "%key + validator.__name__) + raise ValidationError("%s does not pass the validator " % key + validator.__name__) except Exception, e: self._raise_exception(ValidationError, key, unicode(e)) From 59fe89eba22195223a62e2691f556bfc919baaca Mon Sep 17 00:00:00 2001 From: Hassan Jannah Date: Tue, 27 Jan 2015 21:40:25 -0800 Subject: [PATCH 3/6] Update schema_document.py --- mongokit/schema_document.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mongokit/schema_document.py b/mongokit/schema_document.py index 9530fcd..6c41fca 100644 --- a/mongokit/schema_document.py +++ b/mongokit/schema_document.py @@ -660,10 +660,9 @@ 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 " % key + validator.__name__) + raise ValidationError("%s does not pass the validator %s" % (key, validator.__name__)) except Exception, e: - self._raise_exception(ValidationError, key, - unicode(e)) + self._raise_exception(ValidationError, key, unicode(e)) def _process_custom_type(self, target, doc, struct, path="", root_path=""): for key in struct: From 5716183d69cbc363906e0c3ecff058e67b3230be Mon Sep 17 00:00:00 2001 From: Hassan Jannah Date: Wed, 28 Jan 2015 00:19:02 -0800 Subject: [PATCH 4/6] Update schema_document.py --- mongokit/schema_document.py | 78 ++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 44 deletions(-) diff --git a/mongokit/schema_document.py b/mongokit/schema_document.py index 6c41fca..2855038 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' ] @@ -97,7 +97,6 @@ def validate(self, value, path): # field wich does not need to be declared into the structure STRUCTURE_KEYWORDS = [] - class SchemaDocumentError(Exception): pass @@ -146,16 +145,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 +189,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 +203,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 +225,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 +331,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 +344,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 +401,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 +471,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: @@ -660,9 +649,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 %s" % (key, validator.__name__)) + raise ValidationError("%s does not pass the validator %s" % (key , validator.__name__)) except Exception, e: - self._raise_exception(ValidationError, key, unicode(e)) + self._raise_exception(ValidationError, key, + unicode(e)) def _process_custom_type(self, target, doc, struct, path="", root_path=""): for key in struct: @@ -807,7 +797,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 +845,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 # From 72acac23e1ae68bce94bc66f7e95aac452c5652b Mon Sep 17 00:00:00 2001 From: Hassan Jannah Date: Wed, 28 Jan 2015 00:20:58 -0800 Subject: [PATCH 5/6] Update schema_document.py --- mongokit/schema_document.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mongokit/schema_document.py b/mongokit/schema_document.py index 2855038..b9738f2 100644 --- a/mongokit/schema_document.py +++ b/mongokit/schema_document.py @@ -649,7 +649,7 @@ 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 %s" % (key , validator.__name__)) + raise ValidationError("%s does not pass the validator %s" % (key, validator.__name__)) except Exception, e: self._raise_exception(ValidationError, key, unicode(e)) From cd85c95db2bbc12a5926b9cd2fefe53eeb438c04 Mon Sep 17 00:00:00 2001 From: Hassan Jannah Date: Wed, 28 Jan 2015 00:22:35 -0800 Subject: [PATCH 6/6] Update schema_document.py --- mongokit/schema_document.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mongokit/schema_document.py b/mongokit/schema_document.py index b9738f2..ec9fbf9 100644 --- a/mongokit/schema_document.py +++ b/mongokit/schema_document.py @@ -97,6 +97,7 @@ def validate(self, value, path): # field wich does not need to be declared into the structure STRUCTURE_KEYWORDS = [] + class SchemaDocumentError(Exception): pass