# config for sface
# uses in this order
# command-line args
-# ~/.sfi/sfi_config
+# <configdir>/sfi_config
###
import os
('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 ]
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:
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('=')
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):
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)
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 getResourcesRSpecFile(self): return self.fullpath("resources.rspec")
+
+ def getSliceRecordFile(self): return self.fullpath ("%s.record" % self.getSlice())
+
+ def getAuthorityRecordFile(self): return self.fullpath ("%s/%s.record" % self.getAuthority())
+
+ def getAuthorityListRecordFile(self, i=None):
+ # DEPRECATED
+ 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())
+
+ def getAuthorityListFile(self):
+ return self.fullpath ("%s_list.xml" % self.getAuthority())
+
# configuration singleton
config = Config()
config.define_accessors()
+