diff --git a/sgschema/entity.py b/sgschema/entity.py index e8f9373..ab827a5 100644 --- a/sgschema/entity.py +++ b/sgschema/entity.py @@ -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: diff --git a/sgschema/field.py b/sgschema/field.py index e1ef4cb..080f944 100644 --- a/sgschema/field.py +++ b/sgschema/field.py @@ -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), diff --git a/sgschema/schema.py b/sgschema/schema.py index 218e944..36d7e33 100644 --- a/sgschema/schema.py +++ b/sgschema/schema.py @@ -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), @@ -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.""" @@ -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. @@ -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', {}))