Skip to content

Commit a7fb85b

Browse files
committed
Merge branch 'master' into develop
2 parents 2351b37 + 71a6ad0 commit a7fb85b

File tree

14 files changed

+423
-251
lines changed

14 files changed

+423
-251
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ language: python
22
python:
33
- '2.7'
44
install:
5-
- pip install -r requirements.txt
65
- pip install tox
76
script: tox
87
deploy:

python_jsonschema_objects/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from jsonschema.compat import iteritems
44
import json
55
import codecs
6+
import copy
67
import os.path
78
import inflection
89
import six
@@ -28,7 +29,7 @@ def __init__(self, schema_uri, resolved={}):
2829
if isinstance(schema_uri, six.string_types):
2930
uri = os.path.normpath(schema_uri)
3031
self.basedir = os.path.dirname(uri)
31-
with open(uri) as fin:
32+
with codecs.open(uri, 'r', 'utf-8') as fin:
3233
self.schema = json.loads(fin.read())
3334
else:
3435
self.schema = schema_uri
@@ -52,6 +53,17 @@ def __init__(self, schema_uri, resolved={}):
5253
self._classes = None
5354
self._resolved = None
5455

56+
@property
57+
def schema(self):
58+
try:
59+
return copy.deepcopy(self._schema)
60+
except AttributeError:
61+
raise ValidationError("No schema provided")
62+
63+
@schema.setter
64+
def schema(self, val):
65+
setattr(self, '_schema', val)
66+
5567
@property
5668
def classes(self):
5769
if self._classes is None:

python_jsonschema_objects/classbuilder.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import sys
99

1010
import logging
11+
12+
import python_jsonschema_objects.wrapper_types
13+
1114
logger = logging.getLogger(__name__)
1215

1316
logger.addHandler(logging.NullHandler())
@@ -64,6 +67,9 @@ def for_json(self):
6467
return self.as_dict()
6568

6669
def __eq__(self, other):
70+
if not isinstance(other, ProtocolBase):
71+
return False
72+
6773
return self.as_dict() == other.as_dict()
6874

6975
def __str__(self):
@@ -153,7 +159,6 @@ def __init__(self, **props):
153159
six.moves.xrange(len(self.__prop_names__))]))
154160

155161
for prop in props:
156-
157162
try:
158163
logger.debug(util.lazy_format("Setting value for '{0}' to {1}", prop, props[prop]))
159164
setattr(self, prop, props[prop])
@@ -318,7 +323,11 @@ def __init__(self, value, typ=None):
318323
:value: @todo
319324
320325
"""
321-
self._value = value
326+
if isinstance(value, LiteralValue):
327+
self._value = value._value
328+
else:
329+
self._value = value
330+
322331
self.validate()
323332

324333
def as_dict(self):
@@ -456,7 +465,7 @@ def _construct(self, uri, clsdata, parent=(ProtocolBase,),**kw):
456465
elif clsdata.get('type') == 'array' and 'items' in clsdata:
457466
clsdata_copy = {}
458467
clsdata_copy.update(clsdata)
459-
self.resolved[uri] = validators.ArrayValidator.create(
468+
self.resolved[uri] = python_jsonschema_objects.wrapper_types.ArrayWrapper.create(
460469
uri,
461470
item_constraint=clsdata_copy.pop('items'),
462471
classbuilder=self,
@@ -530,6 +539,7 @@ def _build_object(self, nm, clsdata, parents,**kw):
530539
name_translation = {}
531540

532541
for prop, detail in properties.items():
542+
logger.debug(util.lazy_format("Handling property {0}.{1}",nm, prop))
533543
properties[prop]['raw_name'] = prop
534544
name_translation[prop] = prop.replace('@', '')
535545
prop = name_translation[prop]
@@ -550,6 +560,9 @@ def _build_object(self, nm, clsdata, parents,**kw):
550560
elif 'type' not in detail and '$ref' in detail:
551561
ref = detail['$ref']
552562
uri = util.resolve_ref_uri(self.resolver.resolution_scope, ref)
563+
logger.debug(util.lazy_format("Resolving reference {0} for {1}.{2}",
564+
ref, nm, prop
565+
))
553566
if uri not in self.resolved:
554567
with self.resolver.resolving(ref) as resolved:
555568
self.resolved[uri] = self.construct(
@@ -581,7 +594,7 @@ def _build_object(self, nm, clsdata, parents,**kw):
581594
typ = self.construct(uri, detail['items'])
582595
propdata = {
583596
'type': 'array',
584-
'validator': validators.ArrayValidator.create(
597+
'validator': python_jsonschema_objects.wrapper_types.ArrayWrapper.create(
585598
uri,
586599
item_constraint=typ)}
587600
else:
@@ -602,14 +615,14 @@ def _build_object(self, nm, clsdata, parents,**kw):
602615
else:
603616
typ = self.construct(uri, detail['items'])
604617
propdata = {'type': 'array',
605-
'validator': validators.ArrayValidator.create(uri, item_constraint=typ,
606-
addl_constraints=detail)}
618+
'validator': python_jsonschema_objects.wrapper_types.ArrayWrapper.create(uri, item_constraint=typ,
619+
addl_constraints=detail)}
607620
except NotImplementedError:
608621
typ = detail['items']
609622
propdata = {'type': 'array',
610-
'validator': validators.ArrayValidator.create(uri,
611-
item_constraint=typ,
612-
addl_constraints=detail)}
623+
'validator': python_jsonschema_objects.wrapper_types.ArrayWrapper.create(uri,
624+
item_constraint=typ,
625+
addl_constraints=detail)}
613626

614627
props[prop] = make_property(prop,
615628
propdata,
@@ -725,7 +738,7 @@ def setprop(self, val):
725738
val.validate()
726739
ok = True
727740
break
728-
elif util.safe_issubclass(typ, validators.ArrayValidator):
741+
elif util.safe_issubclass(typ, python_jsonschema_objects.wrapper_types.ArrayWrapper):
729742
try:
730743
val = typ(val)
731744
except Exception as e:
@@ -743,13 +756,14 @@ def setprop(self, val):
743756
"Object must be one of {0}: \n{1}".format(info['type'], errstr))
744757

745758
elif info['type'] == 'array':
746-
instance = info['validator'](val)
747-
val = instance.validate()
759+
val = info['validator'](val)
760+
val.validate()
748761

749-
elif util.safe_issubclass(info['type'], validators.ArrayValidator):
762+
elif util.safe_issubclass(info['type'],
763+
python_jsonschema_objects.wrapper_types.ArrayWrapper):
750764
# An array type may have already been converted into an ArrayValidator
751-
instance = info['type'](val)
752-
val = instance.validate()
765+
val = info['type'](val)
766+
val.validate()
753767

754768
elif getattr(info['type'], 'isLiteralClass', False) is True:
755769
if not isinstance(val, info['type']):

python_jsonschema_objects/pattern_properties.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import collections
77

88
import logging
9+
10+
import python_jsonschema_objects.wrapper_types
11+
912
logger = logging.getLogger(__name__)
1013

1114
PatternDef = collections.namedtuple('PatternDef', 'pattern schema_type')
@@ -70,7 +73,7 @@ def _make_type(self, typ, val):
7073
if util.safe_issubclass(typ, cb.ProtocolBase):
7174
return typ(**util.coerce_for_expansion(val))
7275

73-
if util.safe_issubclass(typ, validators.ArrayValidator):
76+
if util.safe_issubclass(typ, python_jsonschema_objects.wrapper_types.ArrayWrapper):
7477
return typ(val)
7578

7679
raise validators.ValidationError(

python_jsonschema_objects/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ class ProtocolJSONEncoder(json.JSONEncoder):
4444

4545
def default(self, obj):
4646
from python_jsonschema_objects import classbuilder
47-
from python_jsonschema_objects import validators
47+
from python_jsonschema_objects import wrapper_types
4848

4949
if isinstance(obj, classbuilder.LiteralValue):
5050
return obj._value
51-
if isinstance(obj, validators.ArrayValidator):
51+
if isinstance(obj, wrapper_types.ArrayWrapper):
5252
return obj.for_json()
5353
if isinstance(obj, classbuilder.ProtocolBase):
5454
props = {}

0 commit comments

Comments
 (0)