Source code for ImportExport

# CellTracking.py
# By MW, Jul 2013
# GPLv3+
#
# Class managing import/export routines.
#+I'm not sure that it will contain much stuff, but we'll see...

import os, logging, shutil, pickle

[docs]class ImportExport : def __init__(self) : self.VERSION = 0.4 # Version of the import/export format # Compatibility version: 0.4 # Real version: 0.6 self.movie = None self.filepath = None
[docs] def set_movie(self, movie) : self.movie = movie
[docs] def get_path(self) : return self.filepath
[docs] def set_path(self, p) : if os.path.isdir(os.path.dirname(p)) : self.filepath = p
[docs] def load(self) : """Loads the presets. The file should be provided using the function self.set_path""" fn = self.filepath if fn == None : logging.error("Trying to save but no path has been specified") else : if not os.path.isfile(self.filepath) : logging.error('File does not exist') else : f = open(fn, 'r') dic = pickle.load(f) logging.info('Save data read from file %s', fn) # Check version version = dic['infos']['value']['version']['value'] # tmp comment before stabilizing the export format if version != self.VERSION : logging.warning('File version does not correspond with the version of the program. Beware!') print "Version error" elif self.movie == None : logging.warning('No movie has been declared (self.movie is None)') print "Movie error" else : # Call movie.load_save_dict self.movie.load_save_dict(dic['movie'])
[docs] def save(self) : fn = self.filepath if fn == None : logging.error("Trying to save but no path has been specified") else : if os.path.isfile(self.filepath) : logging.warning("Saving but a file exist, moving previous file to a backup") ext = 1 # Generate filename while os.path.isfile(fn+'.backup.'+str(ext)) : ext += 1 shutil.move(fn, fn+'.backup.'+str(ext)) # Make backup copy dic = self.new_dict() dic['movie'] = self.movie.get_save_dict() # Get dict f = open(fn, 'w') # Pickle it pickle.dump(dic, f) f.close() logging.info('In theory (no error message displayed), the session should have been saved')
[docs] def new_dict(self) : """Returns an empty dict, initialized with essential parameters""" version = self.VERSION dicinfos = {} dicinfos['version'] = {'descr' : 'Version of the file format. Check in the documentation if the format is backward compatible', 'value' : version} dic_info = {'descr' : 'Essential information about the file format', 'value' : dicinfos} return {'descr' : 'Dictionary describing a movie', 'value' : None, 'infos' : dic_info}
[docs] def get_frames_object(self) : return {'type' : 'savefile', 'value' : self.filepath}