gnome.persist.save_load¶
Save/load gnome objects
Attributes¶
Classes¶
Class to store and handle references during saving/loading. |
|
PyGnome objects like the PointWindMover contain other objects, eg Wind object. |
|
Create a mixin for save/load options for saving and loading serialized |
Functions¶
|
read json from file and load the appropriate object |
|
some basic checks on validity of zipfile. Primarily for checking save |
|
Get a list of the folders in our archive. |
|
Module Contents¶
- gnome.persist.save_load.log¶
- class gnome.persist.save_load.Refs¶
Bases:
dict
Class to store and handle references during saving/loading. Provides some convenience functions
Initialize self. See help(type(self)) for accurate signature.
- gen_default_name(obj)¶
Goes through the dict, finds all objects of obj.obj_type stored, and provides a unique name by appending length+1
- class gnome.persist.save_load.References¶
Bases:
object
PyGnome objects like the PointWindMover contain other objects, eg Wind object. When persisting a Model, the Wind object is not created by the PointWindMover, it is merely referenced by the PointWindMover. When persisting a Model, the referenced objects are saved in their own file and a reference is stored for it. This class manages these references.
- property files¶
- get_reference(obj)¶
return key if obj already exists in references list else return None
- reference(obj, name=None)¶
Get a unique reference to the object. By default this string is the filename in which the json for the object is stored If a reference to obj already exists, then it is returned
- Parameters:
obj – object for which a reference must be added
name=None – add an object reference specified by ‘name’ for filename
- retrieve(ref)¶
retrieve the object associated with the reference
- gnome.persist.save_load.load(saveloc, fname='Model.json', references=None)¶
read json from file and load the appropriate object This is a general purpose load method that looks at the json[‘obj_type’] and invokes json[‘obj_type’].loads(json) method
- Parameters:
saveloc – path to zipfile that contains data files and json files. It could also be a directory containing files - keep original support for location files.
fname – .json file to load. Default is ‘Model.json’. zipfile/dir must contain ‘fname’
references=None – References object that keeps track of objects in a dict as they are constructed, using the filename as the key
- Returns:
object constructed from the json
Note
Function first assumes saveloc is a directory and looks for saveloc/fname. If this fails, it checks if saveloc is a zipfile. If this fails, it checks if saveloc is a file and loads this assuming its json for a gnome object. If none of these work, it just returns None.
- class gnome.persist.save_load.Savable¶
Bases:
object
Create a mixin for save/load options for saving and loading serialized gnome objects. Mix this in with the Serializable class so all gnome objects can save/load themselves
- classmethod loads(json_data, saveloc=None, references=None)¶
loads object from json_data
load json for references from files
update paths of datafiles if needed
deserialize json_data
and create object with new_from_dict()
- json_data: dict containing json data. It has been parsed through the
json.loads() command. The json will be valided here when it gets deserialized. Its references and datafile paths will be recreated here prior to calling new_from_dict()
Optional parameter
- Parameters:
saveloc – location of data files or .json files for objects stored as references. If object requires no datafiles and does not need to read references from a .json file in saveloc, then this can be None.
references – references object - if this is called by the Model, it will pass a references object. It is not required.
- Returns:
object constructed from json_data.
- gnome.persist.save_load.is_savezip_valid(savezip)¶
some basic checks on validity of zipfile. Primarily for checking save zipfiles loaded from the Web. Following are the types of errors it checks:
- Returns:
True if zip is valid, False otherwise
Failed to open zipfile
CRC failed for a file in the archive - rejecting zip
Found a *.json with size > _max_json_filesize - rejecting
- Reject - found a file with:
uncompressed_size/compressed_size > _max_compress_ratio.
- Found a file in archive that has path outside of saveloc - rejecting
rejects zipfile if it contains an archive with ‘..’
Note
can change _max_json_filesize, _max_compress_ratio if required.
- gnome.persist.save_load.zipfile_folders(zip_file)¶
Get a list of the folders in our archive. - MAC OS X created zipfiles contain a folder named ‘__MACOSX/’,
which is intended to contain file and folder related metadata. we would like to ignore this folder.
- gnome.persist.save_load.extract_zipfile(zip_file, to_folder='.', prefix='')¶