From: Tony Mack <tmack@cs.princeton.edu>
Date: Wed, 4 Feb 2009 16:28:12 +0000 (+0000)
Subject: fixed bug in load(), If db_file doesnt exist create it and load an empty dict
X-Git-Tag: sfa-0.9-0@14641~672
X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=6925f361fd6f889562cbfa9ba1fab6fbf67d6b48;p=sfa.git

fixed bug in load(), If db_file doesnt exist create it and load an empty dict
---

diff --git a/geni/util/storage.py b/geni/util/storage.py
index 1fb2c9af..9dd094ea 100644
--- a/geni/util/storage.py
+++ b/geni/util/storage.py
@@ -1,3 +1,4 @@
+import os
 
 class SimpleStorage(dict):
 
@@ -13,8 +14,16 @@ class SimpleStorage(dict):
         self.db_filename = db_filename
     
     def load(self):
-        db_file = open(self.db_filename, 'r')
-        dict.__init__(self, eval(db_file.read()))    
+        if os.path.exists(self.db_filename) and os.path.isfile(self.db_filename):
+            db_file = open(self.db_filename, 'r')
+            dict.__init__(self, eval(db_file.read()))
+        elif os.path.exists(self.db_filename) and not os.path.isfile(self.db_filename):
+            raise IOError, '%s exists but is not a file. please remove it and try again' \
+                           % self.db_filename
+        else:
+            db_file = open(self.db_filename, 'w')
+            db_file.write('{}')
+            db_file.close()
  
     def write(self):
         db_file = open(self.db_filename, 'w')