can select an alternate config. directory on the command-line
[sface.git] / sface / config.py
index d1e944b..bf94cbc 100644 (file)
@@ -24,9 +24,22 @@ class Config:
         ('slicemgr',   'SFI_SM' ,      d_slicemgr,     '-m','--slicemgr',      "slice API URL"),
 #        ('aggmgr',    'SFI_AM',       d_aggmgr,       '-a','--aggregate',     "aggregate manager's URL"),
         ('verbose',    'SFACE_VERBOSE',True,          '-v','--verbose',       "UI verbosity"),
-        ('debug',      'SFACE_DEBUG',  False,          '-d','--debug',         "UI debug flag"),
+        ('debug',      'SFACE_DEBUG',  False,          '-D','--debug',         "UI debug flag"),
         ]
 
+    def __init__ (self):
+        print 'WARNING - should pass config dir'
+        self.dirname=os.path.expanduser("~/.sfi/")
+        self.read_config()
+
+    def get_dirname (self):
+        return self.dirname
+
+    # warning, might lose unsaved data..
+    def set_dirname (self, dirname):
+        self.dirname=dirname
+        self.read_config()
+            
     def fields (self):
         return [ tup[0] for tup in Config.supported ]
 
@@ -67,26 +80,20 @@ class Config:
     def get(self,field): return getattr(self,field)
     def set(self,field,value): setattr(self,field,value)
 
-    def __init__(self):
-        self.read_config()
-
-    def dirname (self):
-        return os.path.expanduser("~/.sfi/")
-
-    def filename (self):
-        return self.dirname() + "sfi_config"
+    def config_file (self):
+        return os.path.join(self.get_dirname(),"sfi_config")
 
     def read_config(self):
         tmp={}
         try:
-            execfile(self.filename(), tmp)
+            execfile(self.config_file(), tmp)
         except:
-            print "Warning - no config file found %s"%self.filename()
+            print "Warning - no config file found %s"%self.config_file()
             pass
         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())
+        self.display("After reading config from %s"%self.config_file())
 
     def display (self, msg):
         if self.debug:
@@ -95,11 +102,11 @@ class Config:
                 print "%-20s: %r"%(k, self.get(k))
 
     def save_config(self):
-        configdir = self.dirname()
+        configdir = self.get_dirname()
         if not os.path.exists(configdir):
             os.makedirs(configdir)
 
-        configfile = self.filename()
+        configfile = self.config_file()
         if not os.path.exists(configfile):
             open(configfile, "w").close()
 
@@ -140,6 +147,8 @@ class Config:
         out.close()
         os.unlink(configfile)
         os.rename(tmpfile, configfile)
+        if self.debug: 
+            print 'stored config in %s'%configfile
 
     # check if a field is a boolean field
     def is_bool_field(self, field):
@@ -153,6 +162,9 @@ class Config:
         if isinstance(value,types.StringTypes) and value.lower()=='true': return True
 
     def add_options_to_OptionParser (self, parser):
+        parser.add_option ("-d","--dir",dest='dirname',action='store',
+                           default=os.path.expanduser("~/.sfi/"),
+                           help="sfi config dir")
         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)
@@ -160,6 +172,8 @@ class Config:
                 parser.add_option(short,long,dest=field,action="store",default=None, help=msg)
 
     def update_from_OptionParser (self, optparse_options):
+        self.set_dirname(optparse_options.dirname)
+        print 'setting dir',self.dirname
         for field in self.fields():
             if not hasattr(optparse_options,field) : continue
             value=getattr(optparse_options,field)
@@ -173,20 +187,19 @@ class Config:
 #        a = user.split('.')
 #        Config.SFI_AUTH = '.'.join(a[:len(a)-1])
 
-    def getSliceRSpecFile(self):
-        return os.path.expanduser("~/.sfi/%s.rspec" % self.getSlice())
+    def fullpath (self, filename): return os.path.join(self.get_dirname(),filename)
+
+    def getSliceRSpecFile(self): return self.fullpath("%s.rspec" % self.getSlice())
 
-    def getSliceRecordFile(self):
-        return os.path.expanduser("~/.sfi/%s.record" % self.getSlice())
+    def getSliceRecordFile(self): return self.fullpath ("%s.record" % self.getSlice())
 
-    def getAuthorityRecordFile(self):
-        return os.path.expanduser("~/.sfi/%s.record" % self.getAuthority())
+    def getAuthorityRecordFile(self): return self.fullpath ("%s/%s.record" % self.getAuthority())
 
     def getAuthorityListFile(self, i=None):
         if (i != None) and (i != 0):
-            return os.path.expanduser("~/.sfi/%s_list.record.%d" % (self.getAuthority(),i))
+            return self.fullpath ("%s_list.record.%d" % (self.getAuthority(),i))
         else:
-            return os.path.expanduser("~/.sfi/%s_list.record" % self.getAuthority())
+            return self.fullpath ("%s_list.record" % self.getAuthority())
 
 
 # configuration singleton