9 from optparse import OptionParser
14 d_registry= "http://www.planet-lab.org:12345"
15 d_slicemgr= "http://www.planet-lab.org:12347"
16 d_aggmgr= "http://www.planet-lab.org:12346"
19 # local code name, config variable name, default
20 ('slice', 'SFI_SLICE' , None, '-s','--slice', "slice HRN"),
21 ('user', 'SFI_USER', None, '-u','--user', "user HRN"),
22 ('authority', 'SFI_AUTH', None, '-t','--auth', "users's authority HRN"),
23 ('registry', 'SFI_REGISTRY', d_registry, '-r','--registry', "registry's URL"),
24 ('slicemgr', 'SFI_SM' , d_slicemgr, '-m','--slicemgr', "slice manager's URL"),
25 ('aggmgr', 'SFI_AM', d_aggmgr, '-a','--aggregate', "aggregate manager's URL"),
26 ('verbose', 'SFACE_VERBOSE',False, '-v','--verbose', "UI verbosity"),
27 ('debug', 'SFACE_DEBUG', False, '-d','--debug', "UI debug flag"),
31 return [ tup[0] for tup in Config.supported ]
33 def field_labels (self):
34 return [ (tup[0],tup[5]) for tup in Config.supported ]
36 def sfi_field (self, sfi):
37 for tuple in Config.supported:
38 if tuple[1]==sfi: return tuple[0]
41 def field_default (self, field):
42 for tuple in Config.supported:
43 if tuple[0]==field: return tuple[2]
46 # xxx todo - need for validators - not even sure this is still useful
47 def define_accessors (self):
48 for (field,sfi,default,_,__,___) in Config.supported:
49 self.define_accessor (field,sfi,default)
51 def define_accessor (self,field,sfi,default):
52 get_name="get" + field.capitalize();
53 if not hasattr(Config,get_name):
54 def get_call (self): return getattr(self,field)
55 setattr (Config, get_name, get_call)
56 set_name="set" + field.capitalize();
57 if not hasattr(Config,set_name):
58 def set_call (self, newvalue): setattr (self, field, newvalue)
59 setattr (Config, set_name, set_call)
61 # the generic form of accessors
62 def get(self,field): return getattr(self,field)
63 def set(self,field,value): setattr(self,field,value)
69 return os.path.expanduser("~/.sfi/sfi_config")
71 def read_config(self):
74 execfile(self.filename(), tmp)
76 print "Warning - no config file found %s"%self.filename()
78 for (field,sfi,default,_,__,___) in Config.supported:
79 if tmp.has_key(sfi):setattr(self,field,tmp[sfi])
80 else: setattr(self,field,default)
81 self.display("After reading config from %s"%self.filename())
83 def display (self, msg):
86 for k in self.fields():
87 print "%-20s: %r"%(k,getattr(self,k))
89 def save_config(self):
90 configfile = self.filename()
91 tmpfile = configfile + ".tmp"
93 out = open(tmpfile, "w")
96 for line in open(configfile):
99 sfi, val = line.split('=')
102 field=self.sfi_field(sfi)
104 newval=getattr(self,field)
105 if not self.is_bool_field(field): newval="'%s'"%newval
106 line = "%s = %s\n" % (sfi, newval)
110 print 'line',lineno,'ignored',line
114 os.unlink(configfile)
115 os.rename(tmpfile, configfile)
117 # check if a field is a boolean field
118 def is_bool_field(self, field):
119 for (f,_,default,__,___,____) in Config.supported:
120 if field==f: return isinstance(default,bool)
123 # to accept strings as bools
124 def is_true(self,value):
125 if value==True: return True
126 if isinstance(value,types.StringTypes) and value.lower()=='true': return True
128 def add_options_to_OptionParser (self, parser):
129 for (field,_,default,short,long,msg) in Config.supported:
130 if isinstance(default,bool):
131 parser.add_option(short,long,dest=field,action="store_true",help=msg)
133 parser.add_option(short,long,dest=field,action="store",default=None, help=msg)
135 def update_from_OptionParser (self, optparse_options):
136 for field in self.fields():
137 if not hasattr(optparse_options,field) : continue
138 value=getattr(optparse_options,field)
139 if value is not None:
140 setattr(self,field,getattr(optparse_options,field))
142 # def setUser(self, user):
143 # Config.SFI_USER = user
145 # # Should probably get authority from user record instead...
146 # a = user.split('.')
147 # Config.SFI_AUTH = '.'.join(a[:len(a)-1])
149 def getSliceRSpecFile(self):
150 return os.path.expanduser("~/.sfi/%s.rspec" % self.getSlice())
153 # configuration singleton
155 config.define_accessors()