Fixed initialization bug
authorAndy Bavier <acb@acb-imac.cs.princeton.edu>
Wed, 1 Jun 2011 20:07:05 +0000 (16:07 -0400)
committerAndy Bavier <acb@acb-imac.cs.princeton.edu>
Wed, 1 Jun 2011 20:07:05 +0000 (16:07 -0400)
SFA-36
If ~/.sfi/ is not present, it will be created.  A missing ~/.sfi/sfi.config will not
cause an error.

sface/config.py

index 781d0a8..885e6b6 100644 (file)
@@ -70,8 +70,11 @@ class Config:
     def __init__(self):
         self.read_config()
 
+    def dirname (self):
+        return os.path.expanduser("~/.sfi/")
+
     def filename (self):
-        return os.path.expanduser("~/.sfi/sfi_config")
+        return self.dirname() + "sfi_config"
 
     def read_config(self):
         tmp={}
@@ -92,14 +95,21 @@ class Config:
                 print "%-20s: %r"%(k, self.get(k))
 
     def save_config(self):
+        configdir = self.dirname()
+        if not os.path.exists(configdir):
+            os.makedirs(configdir)
+
         configfile = self.filename()
+        if not os.path.exists(configfile):
+            open(configfile, "w").close()
+
         tmpfile = configfile + ".tmp"
 
         out = open(tmpfile, "w")
         lineno = 0
         written_fields = []
         fields = self.fields()
-        for line in open(configfile):
+        for line in open(configfile, "r"):
             lineno += 1
             try:
                 sfi, val = line.split('=')