def save(self, obj, typename=None): """ Save the item to this session. Parameters ---------- obj : object Object to save to this session. """ if not Session._is_known_type(obj): raise Exception("Trying to save an object of unknown type.") name = obj.name typename = obj._typename if not typename else typename savedir = self.location if _internal_utils.is_gl_artifact_class(obj.__class__): key = Key.from_tuple(name, typename) if not obj._session_registration_date: obj._session_registration_date = _datetime.datetime.now() filename = "%s.%s" % (self._get_filename_from_name(obj.name), typename) obj._modified_since_last_saved = False # Persist the object. with self._lock: obj._save_to_file(_os.path.join(savedir, filename)) # Remove from unsaved objects (only if it exists) self._delete_key_from_unsaved_objects(key, silent=True) # Update the index file with the object metadata. metadata = obj._get_metadata() metadata.update({'filename': filename}) # Update the shelve. index = _shelve.open(self._index_file) index[str(key)] = metadata index.close() return
def _is_known_type(obj): """ Helper method that returns True when type is supported by Session. """ return _internal_utils.is_gl_artifact_class(obj.__class__)