add save_config method to SfaData
authorBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Wed, 8 Sep 2010 15:17:46 +0000 (17:17 +0200)
committerBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Wed, 8 Sep 2010 15:17:46 +0000 (17:17 +0200)
sfadata.py

index 433868e..dbf7f30 100644 (file)
@@ -14,6 +14,9 @@ class SfaData:
                  }
 
     def __init__(self):
+        self.read_config()
+
+    def read_config(self):
         filename = os.path.expanduser("~/.sfi/sfi_config")
         execfile(filename, SfaData.__dict__)
         for (k,v) in SfaData.defaults.items():
@@ -23,6 +26,28 @@ class SfaData:
             for (k,v) in SfaData.defaults.items():
                 print "%-20s: %r"%(k,getattr(SfaData,k))
 
+    def save_config(self):
+        config_keys = SfaData.defaults.keys()
+        configfile = os.path.expanduser("~/.sfi/sfi_config")
+        tmpfile = configfile + ".tmp"
+
+        out = open(tmpfile, "w")
+        for line in open(os.path.expanduser("~/.sfi/sfi_config")):
+            try:
+                key, val = line.split('=')
+                key = key.strip()
+                val = val.strip()
+                if key in config_keys:
+                    line = "%s = '%s'\n" % (key, getattr(self, key))
+            except:
+                pass
+            out.write(line)
+        out.close()
+
+        os.unlink(configfile)
+        os.rename(tmpfile, configfile)
+                    
+
     def getAuthority(self):
         return SfaData.SFI_AUTH