def test_warning_logged(): ''' warning is logged if we try to get a class from 'obj_type' that is not in the gnome namespace ''' with LogCapture() as l: with pytest.raises(AttributeError): class_from_objtype('os.path') l.check(('gnome.persist.save_load', 'WARNING', 'os.path is not part of gnome namespace'))
def _deserialize_nested_obj(cls, json_): if json_ is not None: py_class = class_from_objtype(json_['obj_type']) return py_class.deserialize(json_) else: return None
def deserialize(cls, json_): """ append correct schema for current mover """ schema = cls._schema() _to_dict = schema.deserialize(json_) if 'ice_mover' in json_ and json_['ice_mover'] is not None: cm_cls = class_from_objtype(json_['ice_mover']['obj_type']) cm_dict = cm_cls.deserialize(json_['ice_mover']) _to_dict['ice_mover'] = cm_dict return _to_dict
def deserialize(cls, json_): """ Instead of creating schema dynamically for Spill() before deserialization, call nested object's serialize/deserialize methods We also need to accept sparse json objects, in which case we will not treat them, but just send them back. """ if not cls.is_sparse(json_): schema = cls._schema() dict_ = schema.deserialize(json_) relcls = class_from_objtype(json_['release']['obj_type']) dict_['release'] = relcls.deserialize(json_['release']) if json_['json_'] == 'webapi': ''' save files store a reference to element_type so it will get deserialized, created and added to this dict by load method ''' etcls = class_from_objtype(json_['element_type']['obj_type']) dict_['element_type'] = etcls.deserialize(json_['element_type']) if 'water' in json_: w_cls = class_from_objtype(json_['water']['obj_type']) dict_['water'] = w_cls.deserialize(json_['water']) else: ''' Convert nested dict (release object) back into object. The ElementType is now saved as a reference so it is taken care of by load method For the 'webapi', we're not always creating a new object so do this only for 'save' files ''' obj = relcls.new_from_dict(dict_.pop('release')) dict_['release'] = obj return dict_ else: return json_
def deserialize(cls, json_): """ append correct schema for waves object """ if not cls.is_sparse(json_): schema = cls._schema() dict_ = schema.deserialize(json_) if 'waves' in json_: waves = class_from_objtype(json_['waves'].pop('obj_type')) dict_['waves'] = waves.deserialize(json_['waves']) return dict_ else: return json_
def deserialize(cls, json_): """ append correct schema for current mover """ schema = cls._schema() _to_dict = schema.deserialize(json_) if 'ice_movers' in json_: _to_dict['ice_movers'] = [] for i, cm in enumerate(json_['ice_movers']): cm_cls = class_from_objtype(cm['obj_type']) cm_dict = cm_cls.deserialize(json_['ice_movers'][i]) _to_dict['ice_movers'].append(cm_dict) return _to_dict
def deserialize(cls, json_): 'Add distribution schema based on "distribution" - then deserialize' dcls = class_from_objtype(json_['distribution']['obj_type']) # note: it is important to change the 'name' attribute to distribution # that's the key we look for in json_ schema = cls._schema(name=cls.__name__, distribution=dcls._schema(name='distribution')) dict_ = schema.deserialize(json_) # convert nested object ['distribution'] saved as a # dict, back to an object if json_ == 'save' here itself if json_['json_'] == 'save': distribution = dict_.get('distribution') dict_['distribution'] = dcls.new_from_dict(distribution) return dict_
def new_from_dict(cls, dict_): """ change projection_type from string to correct type for loading from save file """ if 'projection' in dict_: # todo: # The 'projection' isn't stored as a nested object - should # revisit this and see if we can make it consistent with nested # objects ... but this works! # creates an instance of the projection class proj_inst = class_from_objtype(dict_.pop('projection'))() # then creates the object obj = cls(projection=proj_inst, **dict_) else: obj = super(Renderer, cls).new_from_dict(dict_) return obj
def deserialize(cls, json_): """ deserialize each object in the 'initializers' dict, then add it to deserialized ElementType dict We also need to accept sparse json objects, in which case we will not treat them, but just send them back. """ if not cls.is_sparse(json_): et_schema = cls._schema() # replace substance with just the oil record ID for now since # we don't have a way to construct to object fromjson() dict_ = et_schema.deserialize(json_) if 'substance' in json_ and json_['substance'] is not {}: # no colander validation for oil object dict_['substance'] = json_['substance'] d_init = [] for i_val in json_['initializers']: i_cls = class_from_objtype(i_val['obj_type']) deserial = i_cls.deserialize(i_val) if json_['json_'] == 'save': ''' If loading from save file, convert the dict_ to new object here itself ''' obj = i_cls.new_from_dict(deserial) d_init.append(obj) else: d_init.append(deserial) dict_['initializers'] = d_init return dict_ else: return json_
def new_from_dict(cls, dict_): """ change projection_type from string to correct type for loading from save file """ if 'projection_class' in dict_: ''' assume dict_ is from a save file since only the save file stores the 'projection_class' todo: The 'projection_class' isn't stored as a nested object - should revisit this and see if we can make it consistent with nested objects .. but this works! ''' proj = class_from_objtype(dict_.pop('projection_class')) viewport = dict_.pop('viewport') obj = cls(projection_class=proj, **dict_) obj.viewport = viewport obj = super(Renderer, cls).new_from_dict(dict_) return obj
def new_from_dict(cls, dict_): """ change projection_type from string to correct type for loading from save file """ if "projection_class" in dict_: """ assume dict_ is from a save file since only the save file stores the 'projection_class' todo: The 'projection_class' isn't stored as a nested object - should revisit this and see if we can make it consistent with nested objects .. but this works! """ proj = class_from_objtype(dict_.pop("projection_class")) viewport = dict_.pop("viewport") obj = cls(projection_class=proj, **dict_) obj.viewport = viewport obj = super(Renderer, cls).new_from_dict(dict_) return obj
def test_class_from_objtype(): ''' test that correct class is returned by class_from_objtype ''' cls = class_from_objtype('gnome.movers.WindMover') assert cls is movers.WindMover