renamed sficonfig into just config
[sface.git] / sface / config.py
similarity index 76%
rename from sface/sficonfig.py
rename to sface/config.py
index 542734e..cb29cbb 100644 (file)
@@ -5,10 +5,11 @@
 ###
 
 import os
+import types
 from optparse import OptionParser
 
 
-class SfiConfig:
+class Config:
 
     d_registry= "http://www.planet-lab.org:12345"
     d_slicemgr= "http://www.planet-lab.org:12347"
@@ -27,35 +28,35 @@ class SfiConfig:
         ]
 
     def fields (self):
-        return [ tup[0] for tup in SfiConfig.supported ]
+        return [ tup[0] for tup in Config.supported ]
 
     def field_labels (self):
-        return [ (tup[0],tup[5]) for tup in SfiConfig.supported ]
+        return [ (tup[0],tup[5]) for tup in Config.supported ]
 
     def sfi_field (self, sfi):
-        for tuple in SfiConfig.supported:
+        for tuple in Config.supported:
             if tuple[1]==sfi: return tuple[0]
         return None
 
     def field_default (self, field):
-        for tuple in SfiConfig.supported:
+        for tuple in Config.supported:
             if tuple[0]==field: return tuple[2]
         return None
 
     # xxx todo - need for validators - not even sure this is still useful
     def define_accessors (self):
-        for (field,sfi,default,_,__,___) in SfiConfig.supported:
+        for (field,sfi,default,_,__,___) in Config.supported:
             self.define_accessor (field,sfi,default)
 
     def define_accessor (self,field,sfi,default):
         get_name="get" + field.capitalize();
-        if not hasattr(SfiConfig,get_name):
+        if not hasattr(Config,get_name):
             def get_call (self): return getattr(self,field)
-            setattr (SfiConfig, get_name, get_call)
+            setattr (Config, get_name, get_call)
         set_name="set" + field.capitalize();
-        if not hasattr(SfiConfig,set_name):
+        if not hasattr(Config,set_name):
             def set_call (self, newvalue): setattr (self, field, newvalue)
-            setattr (SfiConfig, set_name, set_call)
+            setattr (Config, set_name, set_call)
 
     # the generic form of accessors
     def get(self,field): return getattr(self,field)
@@ -74,7 +75,7 @@ class SfiConfig:
         except:
             print "Warning - no config file found %s"%self.filename()
             pass
-        for (field,sfi,default,_,__,___) in SfiConfig.supported:
+        for (field,sfi,default,_,__,___) in Config.supported:
             if tmp.has_key(sfi):setattr(self,field,tmp[sfi])
             else:               setattr(self,field,default)
         self.display("After reading config from %s"%self.filename())
@@ -100,7 +101,9 @@ class SfiConfig:
                 val = val.strip()
                 field=self.sfi_field(sfi)
                 if field:
-                    line = "%s = '%s'\n" % (sfi, getattr(self, field))
+                    newval=getattr(self,field)
+                    if not self.is_bool_field(field): newval="'%s'"%newval
+                    line = "%s = %s\n" % (sfi, newval)
             except:
                 if self.debug:
                     import traceback
@@ -111,9 +114,20 @@ class SfiConfig:
         os.unlink(configfile)
         os.rename(tmpfile, configfile)
 
+    # check if a field is a boolean field
+    def is_bool_field(self, field):
+        for (f,_,default,__,___,____) in Config.supported:
+            if field==f: return isinstance(default,bool)
+        return None
+
+    # to accept strings as bools
+    def is_true(self,value):
+        if value==True: return True
+        if isinstance(value,types.StringTypes) and value.lower()=='true': return True
+
     def add_options_to_OptionParser (self, parser):
-        for (field,_,default,short,long,msg) in SfiConfig.supported:
-            if default==True or default==False:
+        for (field,_,default,short,long,msg) in Config.supported:
+            if isinstance(default,bool):
                 parser.add_option(short,long,dest=field,action="store_true",help=msg)
             else:
                 parser.add_option(short,long,dest=field,action="store",default=None, help=msg)
@@ -126,16 +140,16 @@ class SfiConfig:
                 setattr(self,field,getattr(optparse_options,field))
         
 #    def setUser(self, user):
-#        SfiConfig.SFI_USER = user
+#        Config.SFI_USER = user
 #
 #        # Should probably get authority from user record instead...
 #        a = user.split('.')
-#        SfiConfig.SFI_AUTH = '.'.join(a[:len(a)-1])
+#        Config.SFI_AUTH = '.'.join(a[:len(a)-1])
 
     def getSliceRSpecFile(self):
         return os.path.expanduser("~/.sfi/%s.rspec" % self.getSlice())
         
 
 # configuration singleton
-config = SfiConfig()
+config = Config()
 config.define_accessors()