mailr9499 - /1.3/generic_fns/state.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on September 10, 2009 - 18:32:
Author: bugman
Date: Thu Sep 10 18:32:31 2009
New Revision: 9499

URL: http://svn.gna.org/viewcvs/relax?rev=9499&view=rev
Log:
Redesign of the relax state loading for adding support for a XML formatted 
state file.

Half of the load_state() is now in load_pickle(), and a new function 
load_xml() added.
The determine_format() function will tell if the file is a pickle or XML 
file, and then
load_pickle() will run the appropriate loading function.


Modified:
    1.3/generic_fns/state.py

Modified: 1.3/generic_fns/state.py
URL: 
http://svn.gna.org/viewcvs/relax/1.3/generic_fns/state.py?rev=9499&r1=9498&r2=9499&view=diff
==============================================================================
--- 1.3/generic_fns/state.py (original)
+++ 1.3/generic_fns/state.py Thu Sep 10 18:32:31 2009
@@ -25,6 +25,7 @@
 
 # Python module imports.
 from cPickle import dump, load
+from re import search
 
 # relax module imports.
 from data import Relax_data_store; ds = Relax_data_store()
@@ -32,23 +33,40 @@
 from relax_io import open_read_file, open_write_file
 
 
-def load_state(state=None, dir_name=None):
-    """Function for loading a saved program state.
+def determine_format(file):
+    """Determine the format of the state file.
 
-    @keyword state:     The saved state file.
-    @type state:        str
-    @keyword dir_name:  The path of the state file.
-    @type dir_name:     str
+    @keyword file:  The file object representing the state file.
+    @type file:     file object
+    @return:        The state format.  This can be 'xml' or 'pickle'.
+    @rtype:         str or None
     """
 
-    # Open the file for reading.
-    file = open_read_file(file_name=state, dir=dir_name)
+    # 1st line.
+    header = file.readline()
+    header = header[:-1]    # Strip the trailing newline.
+
+    # Be nice and go back to the start of the file.
+    file.seek(0)
+
+    # XML.
+    if search("<\?xml", header):
+        return 'xml'
+
+    # Pickle.
+    elif search("ccopy_reg", header):
+        return 'pickle'
+
+
+def load_pickle(file):
+    """Load the program state from the pickled file.
+
+    @param file:    The file object containing the relax state.
+    @type file:     file object
+    """
 
     # Unpickle the data class.
-    try:
-        state = load(file)
-    except:
-        raise RelaxError("The saved state " + repr(state) + " is not 
compatible with this version of relax.")
+    state = load(file)
 
     # Close the file.
     file.close()
@@ -79,6 +97,50 @@
     # Delete the state object.
     del state
 
+    # Success.
+    return True
+
+
+def load_state(state=None, dir_name=None):
+    """Function for loading a saved program state.
+
+    @keyword state:     The saved state file.
+    @type state:        str
+    @keyword dir_name:  The path of the state file.
+    @type dir_name:     str
+    """
+
+    # Open the file for reading.
+    file = open_read_file(file_name=state, dir=dir_name)
+
+    # Determine the format of the file.
+    format = determine_format(file)
+
+    # XML state.
+    if format == 'xml':
+        load_xml(file)
+
+    # Pickled state.
+    elif format == 'pickle':
+        load_pickle(file)
+
+    # Bad state file.
+    else:
+        raise RelaxError("The saved state " + repr(state) + " is not 
compatible with this version of relax.")
+
+
+def load_xml(file):
+    """Load the program state from the XML file.
+
+    @param file:    The file object containing the relax state.
+    @type file:     file object
+    """
+
+    # Make sure that the data pipe is empty.
+    if not cdp.is_empty():
+        raise RelaxError("The current data pipe is not empty.")
+
+
 
 def save_state(state=None, dir_name=None, force=False, compress_type=1):
     """Function for saving the program state.




Related Messages


Powered by MHonArc, Updated Thu Sep 10 18:40:03 2009