Setting tag sface-0.9-9
[sface.git] / sface / config.py
1 # config for sface 
2 # uses in this order
3 # command-line args
4 # <configdir>/sfi_config
5 ###
6
7 import os
8 import types
9 from optparse import OptionParser
10
11
12 class Config:
13
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"
17
18     supported = [
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 API URL"),
24         ('slicemgr',    'SFI_SM' ,      d_slicemgr,     '-m','--slicemgr',      "slice API URL"),
25 #        ('aggmgr',     'SFI_AM',       d_aggmgr,       '-a','--aggregate',     "aggregate manager's URL"),
26         ('verbose',     'SFACE_VERBOSE',True,          '-v','--verbose',       "UI verbosity"),
27         ('debug',       'SFACE_DEBUG',  False,          '-D','--debug',         "UI debug flag"),
28         ]
29
30     def __init__ (self):
31         # no need to do anything here, we need set_dirname later on
32         pass
33
34     def get_dirname (self):
35         return self.dirname
36
37     # warning, might lose unsaved data..
38     def set_dirname (self, dirname):
39         self.dirname=dirname
40         self.read_config()
41             
42     def fields (self):
43         return [ tup[0] for tup in Config.supported ]
44
45     def field_labels (self):
46         return [ (tup[0],tup[5]) for tup in Config.supported ]
47
48     def sfi_field (self, sfi):
49         for tuple in Config.supported:
50             if tuple[1]==sfi: return tuple[0]
51         return None
52
53     def sfi_name(self, field):
54         for tuple in Config.supported:
55             if tuple[0] == field: return tuple[1]
56         return None
57
58     def field_default (self, field):
59         for tuple in Config.supported:
60             if tuple[0]==field: return tuple[2]
61         return None
62
63     # xxx todo - need for validators - not even sure this is still useful
64     def define_accessors (self):
65         for (field,sfi,default,_,__,___) in Config.supported:
66             self.define_accessor (field,sfi,default)
67
68     def define_accessor (self,field,sfi,default):
69         get_name="get" + field.capitalize();
70         if not hasattr(Config,get_name):
71             def get_call (self): return getattr(self,field)
72             setattr (Config, get_name, get_call)
73         set_name="set" + field.capitalize();
74         if not hasattr(Config,set_name):
75             def set_call (self, newvalue): setattr (self, field, newvalue)
76             setattr (Config, set_name, set_call)
77
78     # the generic form of accessors
79     def get(self,field): return getattr(self,field)
80     def set(self,field,value): setattr(self,field,value)
81
82     def config_file (self):
83         return os.path.join(self.get_dirname(),"sfi_config")
84
85     def read_config(self):
86         tmp={}
87         try:
88             execfile(self.config_file(), tmp)
89         except:
90             print "Warning - no config file found %s"%self.config_file()
91             pass
92         for (field,sfi,default,_,__,___) in Config.supported:
93             if tmp.has_key(sfi):setattr(self,field,tmp[sfi])
94             else:               setattr(self,field,default)
95         self.display("After reading config from %s"%self.config_file())
96
97     def display (self, msg):
98         if self.debug:
99             print msg
100             for k in self.fields():
101                 print "%-20s: %r"%(k, self.get(k))
102
103     def save_config(self):
104         configdir = self.get_dirname()
105         if not os.path.exists(configdir):
106             os.makedirs(configdir)
107
108         configfile = self.config_file()
109         if not os.path.exists(configfile):
110             open(configfile, "w").close()
111
112         tmpfile = configfile + ".tmp"
113
114         out = open(tmpfile, "w")
115         lineno = 0
116         written_fields = []
117         fields = self.fields()
118         for line in open(configfile, "r"):
119             lineno += 1
120             try:
121                 sfi, val = line.split('=')
122                 sfi = sfi.strip()
123                 val = val.strip()
124                 field = self.sfi_field(sfi)
125                 if field:
126                     written_fields.append(field)
127                     newval = self.get(field)
128                     if not self.is_bool_field(field):
129                         newval="'%s'"%newval
130                     line = "%s = %s\n" % (sfi, newval)
131             except:
132                 if self.debug:
133                     import traceback
134                     print 'line',lineno,'ignored',line
135             out.write(line)
136
137         # append other field in the end
138         new_fields = [f for f in fields if f not in written_fields]
139         for field in new_fields:
140             sfi = self.sfi_name(field)
141             val = self.get(field)
142             if not self.is_bool_field(field):
143                 val="'%s'" % val
144             out.write("%s = %s\n" % (sfi, val))
145
146         out.close()
147         os.unlink(configfile)
148         os.rename(tmpfile, configfile)
149         if self.debug: 
150             print 'stored config in %s'%configfile
151
152     # check if a field is a boolean field
153     def is_bool_field(self, field):
154         for (f,_,default,__,___,____) in Config.supported:
155             if field==f: return isinstance(default,bool)
156         return None
157
158     # to accept strings as bools
159     def is_true(self,value):
160         if value==True: return True
161         if isinstance(value,types.StringTypes) and value.lower()=='true': return True
162
163     def add_options_to_OptionParser (self, parser):
164         parser.add_option ("-d","--dir",dest='dirname',action='store',
165                            default=os.path.expanduser("~/.sfi/"),
166                            help="sfi config dir")
167         for (field,_,default,short,long,msg) in Config.supported:
168             if isinstance(default,bool):
169                 parser.add_option(short,long,dest=field,action="store_true",help=msg)
170             else:
171                 parser.add_option(short,long,dest=field,action="store",default=None, help=msg)
172
173     def update_from_OptionParser (self, optparse_options):
174         self.set_dirname(optparse_options.dirname)
175         print 'setting dir',self.dirname
176         for field in self.fields():
177             if not hasattr(optparse_options,field) : continue
178             value=getattr(optparse_options,field)
179             if value is not None:
180                 setattr(self,field,getattr(optparse_options,field))
181         
182     def fullpath (self, filename): return os.path.join(self.get_dirname(),filename)
183
184     def getSliceRSpecFile(self): return self.fullpath("%s.rspec" % self.getSlice())
185
186     def getResourcesRSpecFile(self): return self.fullpath("resources.rspec")
187
188     def getSliceRecordFile(self): return self.fullpath ("%s.record" % self.getSlice())
189
190     def getUserRecordFile(self): return self.fullpath ("%s.record" % self.getUser())
191
192     def getAuthorityRecordFile(self): return self.fullpath ("%s/%s.record" % self.getAuthority())
193
194     def getAuthorityListFile(self): return self.fullpath ("%s_list.xml" % self.getAuthority())
195
196
197 # configuration singleton
198 config = Config()
199 config.define_accessors()
200