cleanup
[sface.git] / sface / config.py
index d74d239..f913771 100644 (file)
@@ -1,7 +1,7 @@
 # config for sface 
 # uses in this order
 # command-line args
-# ~/.sfi/sfi_config
+# <configdir>/sfi_config
 ###
 
 import os
@@ -13,20 +13,32 @@ class Config:
 
     d_registry= "http://www.planet-lab.org:12345"
     d_slicemgr= "http://www.planet-lab.org:12347"
-    d_aggmgr=   "http://www.planet-lab.org:12346"
+#    d_aggmgr=   "http://www.planet-lab.org:12346"
 
     supported = [
         # local code name, config variable name, default
         ('slice',      'SFI_SLICE' ,   None,           '-s','--slice',         "slice HRN"),
         ('user',       'SFI_USER',     None,           '-u','--user',          "user HRN"),
         ('authority',  'SFI_AUTH',     None,           '-t','--auth',          "users's authority HRN"),
-        ('registry',   'SFI_REGISTRY', d_registry,     '-r','--registry',      "registry's URL"),
-        ('slicemgr',   'SFI_SM' ,      d_slicemgr,     '-m','--slicemgr',      "slice manager's URL"),
-        ('aggmgr',     'SFI_AM',       d_aggmgr,       '-a','--aggregate',     "aggregate manager's URL"),
+        ('registry',   'SFI_REGISTRY', d_registry,     '-r','--registry',      "registry API URL"),
+        ('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):
+        # no need to do anything here, we need set_dirname later on
+        pass
+
+    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,23 +79,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 filename (self):
-        return os.path.expanduser("~/.sfi/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:
@@ -92,14 +101,21 @@ class Config:
                 print "%-20s: %r"%(k, self.get(k))
 
     def save_config(self):
-        configfile = self.filename()
+        configdir = self.get_dirname()
+        if not os.path.exists(configdir):
+            os.makedirs(configdir)
+
+        configfile = self.config_file()
+        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('=')
@@ -130,6 +146,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):
@@ -143,6 +161,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)
@@ -150,23 +171,30 @@ 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)
             if value is not None:
                 setattr(self,field,getattr(optparse_options,field))
         
-#    def setUser(self, user):
-#        Config.SFI_USER = user
-#
-#        # Should probably get authority from user record instead...
-#        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 self.fullpath ("%s.record" % self.getSlice())
+
+    def getAuthorityRecordFile(self): return self.fullpath ("%s/%s.record" % self.getAuthority())
+
+    def getAuthorityListFile(self, i=None):
+        if (i != None) and (i != 0):
+            return self.fullpath ("%s_list.record.%d" % (self.getAuthority(),i))
+        else:
+            return self.fullpath ("%s_list.record" % self.getAuthority())
+
 
 # configuration singleton
 config = Config()
 config.define_accessors()
+