[Yt-svn] yt-commit r1030 - in branches/yt-object-serialization/yt: fido lagos

mturk at wrangler.dreamhost.com mturk at wrangler.dreamhost.com
Tue Dec 23 19:51:19 PST 2008


Author: mturk
Date: Tue Dec 23 19:51:19 2008
New Revision: 1030
URL: http://yt.spacepope.org/changeset/1030

Log:
Parameter file storage now sort of works, set up row factories, added a hash
function.



Modified:
   branches/yt-object-serialization/yt/fido/ParameterFileStorage.py
   branches/yt-object-serialization/yt/fido/__init__.py
   branches/yt-object-serialization/yt/lagos/OutputTypes.py

Modified: branches/yt-object-serialization/yt/fido/ParameterFileStorage.py
==============================================================================
--- branches/yt-object-serialization/yt/fido/ParameterFileStorage.py	(original)
+++ branches/yt-object-serialization/yt/fido/ParameterFileStorage.py	Tue Dec 23 19:51:19 2008
@@ -30,24 +30,72 @@
 
 ## Non-functional right now.
 
+## Our table layout:
+##  ParameterFiles
+##      Filename        fn      text
+##      Last known path path    text
+##      Sim time        time    real    
+##      CurrentTimeID   ctid    real
+##      Hash            hash    text
+
 from yt.fido import *
-import sqlite
+import sqlite3
+import yt.lagos.OutputTypes
+import os.path
+
+#sqlite3.register_adapter(yt.lagos.OutputTypes.EnzoStaticOutput, _adapt_pf)
+#sqlite3.register_converter("pfile", _convert_pf)
 
 class ParameterFileStore(object):
-    _shared_state = {} # Do we need this?
 
-    def __new__(cls, *args, **kwargs):
-        pass
+    def __init__(self, in_memory = False):
+        self._conn = sqlite3.connect(self._get_db_name(),
+                detect_types=sqlite3.PARSE_DECLTYPES)
+        self._conn.row_factory = self._convert_pf
+        self._initialize_new()
         
     def _get_db_name(self):
         return os.path.expanduser("~/.yt/pfdb.sql")
 
     def _initialize_new(self, filename = None):
-        self._conn = sqlite.connect(self._get_db_name())
-
-    def get_pf(self, hash):
-        pass
-
-    def check_pf(self, fn = None, ctid = None, basedir = None,
-                 initial_time = None, hash = None):
-        pass
+        c = self._conn.cursor()
+        try:
+            c.execute("""create table parameter_files
+                            (pf text, path text, time real,
+                             ctid real, hash text)""")
+        except sqlite3.OperationalError:
+            pass
+        self._conn.commit()
+        c.close()
+
+    def get_pf_hash(self, hash):
+        c = self._conn.cursor()
+        c.execute("""select * from parameter_files where hash=?""",
+                  (hash,))
+        return c.fetchall()
+
+    def _adapt_pf(self, pf):
+        print "ADAPTING"
+        return (pf.basename, pf.fullpath,
+                pf["InitialTime"], pf["CurrentTimeIdentifier"],
+                pf._hash())
+
+    def _convert_pf(self, cursor, row):
+        print "CONVERTING"
+        bn, fp, t1, ctid, hash = row
+        fn = os.path.join(fp, bn)
+        print "Trying", fn
+        if os.path.exists(fn):
+            pf = yt.lagos.EnzoStaticOutput(
+                os.path.join(fp, bn))
+        else:
+            raise IOError
+        return pf
+
+
+    def insert_pf(self, pf):
+        c = self._conn.cursor()
+        c.execute("""insert into parameter_files values
+                     (?,?,?,?,?)""", self._adapt_pf(pf))
+        self._conn.commit()
+        c.close()

Modified: branches/yt-object-serialization/yt/fido/__init__.py
==============================================================================
--- branches/yt-object-serialization/yt/fido/__init__.py	(original)
+++ branches/yt-object-serialization/yt/fido/__init__.py	Tue Dec 23 19:51:19 2008
@@ -54,3 +54,4 @@
 from FileHandling import *
 from OutputWatcher import *
 from RunStandalones import *
+from ParameterFileStorage import *

Modified: branches/yt-object-serialization/yt/lagos/OutputTypes.py
==============================================================================
--- branches/yt-object-serialization/yt/lagos/OutputTypes.py	(original)
+++ branches/yt-object-serialization/yt/lagos/OutputTypes.py	Tue Dec 23 19:51:19 2008
@@ -69,6 +69,13 @@
     def __repr__(self):
         return self.basename
 
+    def _hash(self):
+        import md5
+        s = "%s;%s;%s" % (self.basename,
+            self["InitialTime"], self["CurrentTimeIdentifier"])
+        return md5.md5(s).hexdigest()
+
+
     def __getitem__(self, key):
         """
         Returns _units, parameters, or _conversion_factors in that order



More information about the yt-svn mailing list