Skip to content
This repository has been archived by the owner on Jul 14, 2020. It is now read-only.

Update schema_document.py #229

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 35 additions & 44 deletions mongokit/schema_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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', []):
Expand All @@ -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']:
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
#
Expand Down