Skip to content

Commit

Permalink
We don't actually support the pickle protocol, so don't use those met…
Browse files Browse the repository at this point in the history
…hods
  • Loading branch information
mikeboers committed Oct 26, 2015
1 parent 67a6da2 commit e93e678
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions sgschema/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ def _get_or_make_field(self, name):
def _reduce_raw(self, schema, raw_entity):
pass

def __getstate__(self):
def _dump(self):
return dict((k, v) for k, v in (
('fields', self.fields),
('field_aliases', self.field_aliases),
('field_tags', self.field_tags),
) if v)

def __setstate__(self, raw):
def _load(self, raw):
for name, value in raw.pop('fields', {}).iteritems():
self._get_or_make_field(name).__setstate__(value)
self._get_or_make_field(name)._load(value)
self.field_aliases.update(raw.pop('field_aliases', {}))
self.field_tags.update(raw.pop('field_tags', {}))
if raw:
Expand Down
4 changes: 2 additions & 2 deletions sgschema/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ def _reduce_raw(self, schema, raw_field):
types_ = raw_private['allowed_entity_types'] or []
self.allowed_entity_types = set(types_[:])

def __setstate__(self, raw):
def _load(self, raw):
self.allowed_entity_types.update(raw.pop('allowed_entity_types', ()))
self.data_type = raw.pop('data_type', self.data_type)
if raw:
raise ValueError('unknown field keys: %s' % ', '.join(sorted(raw)))

def __getstate__(self):
def _dump(self):
return dict((k, v) for k, v in (
('allowed_entity_types', sorted(self.allowed_entity_types)),
('data_type', self.data_type),
Expand Down
12 changes: 6 additions & 6 deletions sgschema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def _reduce_raw(self):
field = entity._get_or_make_field(field_name)
field._reduce_raw(self, raw_field)

def __getstate__(self):
def _dump(self):
return dict((k, v) for k, v in (
('entities', self.entities),
('entity_aliases', self.entity_aliases),
Expand All @@ -140,7 +140,7 @@ def dump(self, path):
"""
with open(path, 'w') as fh:
fh.write(json.dumps(self, indent=4, sort_keys=True, default=lambda x: x.__getstate__()))
fh.write(json.dumps(self, indent=4, sort_keys=True, default=lambda x: x._dump()))

def load_directory(self, dir_path):
"""Load all ``.json`` and ``.yaml`` files in the given directory."""
Expand Down Expand Up @@ -227,11 +227,11 @@ def update(self, *args, **kwargs):
for arg in args:
if not isinstance(arg, dict):
raise TypeError('Schema.update needs dict')
self.__setstate__(arg)
self._load(arg)
if kwargs:
self.__setstate__(kwargs)
self._load(kwargs)

def __setstate__(self, raw_schema):
def _load(self, raw_schema):

# We mutate this object, and aren't sure how any pickler will feel
# about it.
Expand All @@ -245,7 +245,7 @@ def __setstate__(self, raw_schema):
raw_schema = {'entities': raw_schema}

for type_name, value in raw_schema.pop('entities', {}).iteritems():
self._get_or_make_entity(type_name).__setstate__(value)
self._get_or_make_entity(type_name)._load(value)

merge_update(self.entity_aliases, raw_schema.pop('entity_aliases', {}))
merge_update(self.entity_tags , raw_schema.pop('entity_tags', {}))
Expand Down

0 comments on commit e93e678

Please sign in to comment.