###
import os
+import types
from optparse import OptionParser
-class SfiConfig:
+class Config:
d_registry= "http://www.planet-lab.org:12345"
d_slicemgr= "http://www.planet-lab.org:12347"
]
def fields (self):
- return [ tup[0] for tup in SfiConfig.supported ]
+ return [ tup[0] for tup in Config.supported ]
def field_labels (self):
- return [ (tup[0],tup[5]) for tup in SfiConfig.supported ]
+ return [ (tup[0],tup[5]) for tup in Config.supported ]
def sfi_field (self, sfi):
- for tuple in SfiConfig.supported:
+ for tuple in Config.supported:
if tuple[1]==sfi: return tuple[0]
return None
def field_default (self, field):
- for tuple in SfiConfig.supported:
+ for tuple in Config.supported:
if tuple[0]==field: return tuple[2]
return None
# xxx todo - need for validators - not even sure this is still useful
def define_accessors (self):
- for (field,sfi,default,_,__,___) in SfiConfig.supported:
+ for (field,sfi,default,_,__,___) in Config.supported:
self.define_accessor (field,sfi,default)
def define_accessor (self,field,sfi,default):
get_name="get" + field.capitalize();
- if not hasattr(SfiConfig,get_name):
+ if not hasattr(Config,get_name):
def get_call (self): return getattr(self,field)
- setattr (SfiConfig, get_name, get_call)
+ setattr (Config, get_name, get_call)
set_name="set" + field.capitalize();
- if not hasattr(SfiConfig,set_name):
+ if not hasattr(Config,set_name):
def set_call (self, newvalue): setattr (self, field, newvalue)
- setattr (SfiConfig, set_name, set_call)
+ setattr (Config, set_name, set_call)
# the generic form of accessors
def get(self,field): return getattr(self,field)
except:
print "Warning - no config file found %s"%self.filename()
pass
- for (field,sfi,default,_,__,___) in SfiConfig.supported:
+ 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())
val = val.strip()
field=self.sfi_field(sfi)
if field:
- line = "%s = '%s'\n" % (sfi, getattr(self, field))
+ newval=getattr(self,field)
+ if not self.is_bool_field(field): newval="'%s'"%newval
+ line = "%s = %s\n" % (sfi, newval)
except:
if self.debug:
import traceback
os.unlink(configfile)
os.rename(tmpfile, configfile)
+ # check if a field is a boolean field
+ def is_bool_field(self, field):
+ for (f,_,default,__,___,____) in Config.supported:
+ if field==f: return isinstance(default,bool)
+ return None
+
+ # to accept strings as bools
+ def is_true(self,value):
+ if value==True: return True
+ if isinstance(value,types.StringTypes) and value.lower()=='true': return True
+
def add_options_to_OptionParser (self, parser):
- for (field,_,default,short,long,msg) in SfiConfig.supported:
- if default==True or default==False:
+ 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)
else:
parser.add_option(short,long,dest=field,action="store",default=None, help=msg)
setattr(self,field,getattr(optparse_options,field))
# def setUser(self, user):
-# SfiConfig.SFI_USER = user
+# Config.SFI_USER = user
#
# # Should probably get authority from user record instead...
# a = user.split('.')
-# SfiConfig.SFI_AUTH = '.'.join(a[:len(a)-1])
+# Config.SFI_AUTH = '.'.join(a[:len(a)-1])
def getSliceRSpecFile(self):
return os.path.expanduser("~/.sfi/%s.rspec" % self.getSlice())
# configuration singleton
-config = SfiConfig()
+config = Config()
config.define_accessors()
from PyQt4.QtCore import *
from PyQt4.QtGui import *
-from sface.sficonfig import config
+from sface.config import config
from sface.screens.sfascreen import SfaScreen
class ConfigWidget(QWidget):
default=config.field_default(field)
if isinstance(default,bool):
edit=QCheckBox(msg)
- if config.get(field) is True or config.get(field) == 'True':
+ if config.is_true(config.get(field)):
edit.setCheckState(Qt.Checked)
else:
edit=QLineEdit(config.get(field), self)
layout.addWidget (edit)
for (action,label) in [('apply','Apply'),
- ('save','Apply & Save')]:
+ ('save','Apply && Save')]:
button=QPushButton(label, self)
button.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
layout.addWidget(button, 0, Qt.AlignRight)
def save(self):
self.apply()
config.save_config()
- self.parent().setStatus("<font color='green'>Configuration saved!</font>", timeout=3000)
-
+ self.parent().setStatus("<font color='green'>Configuration saved in %s !</font>"%config.filename(), timeout=5000)
class ConfigScreen(SfaScreen):
def __init__(self, parent):