From: Thierry Parmentelat Date: Fri, 2 Sep 2011 13:25:54 +0000 (+0200) Subject: can select an alternate config. directory on the command-line X-Git-Tag: sface-0.1-18~1 X-Git-Url: http://git.onelab.eu/?p=sface.git;a=commitdiff_plain;h=dea293b69063b10026b56072ef3d1006df177d20 can select an alternate config. directory on the command-line or in the configure screen - although the layout could undoubtedly be improved --- diff --git a/sface/config.py b/sface/config.py index d1e944b..bf94cbc 100644 --- a/sface/config.py +++ b/sface/config.py @@ -24,9 +24,22 @@ class Config: ('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): + print 'WARNING - should pass config dir' + self.dirname=os.path.expanduser("~/.sfi/") + self.read_config() + + 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,26 +80,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 dirname (self): - return os.path.expanduser("~/.sfi/") - - def filename (self): - return self.dirname() + "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: @@ -95,11 +102,11 @@ class Config: print "%-20s: %r"%(k, self.get(k)) def save_config(self): - configdir = self.dirname() + configdir = self.get_dirname() if not os.path.exists(configdir): os.makedirs(configdir) - configfile = self.filename() + configfile = self.config_file() if not os.path.exists(configfile): open(configfile, "w").close() @@ -140,6 +147,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): @@ -153,6 +162,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) @@ -160,6 +172,8 @@ 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) @@ -173,20 +187,19 @@ class Config: # 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 os.path.expanduser("~/.sfi/%s.record" % self.getSlice()) + def getSliceRecordFile(self): return self.fullpath ("%s.record" % self.getSlice()) - def getAuthorityRecordFile(self): - return os.path.expanduser("~/.sfi/%s.record" % self.getAuthority()) + def getAuthorityRecordFile(self): return self.fullpath ("%s/%s.record" % self.getAuthority()) def getAuthorityListFile(self, i=None): if (i != None) and (i != 0): - return os.path.expanduser("~/.sfi/%s_list.record.%d" % (self.getAuthority(),i)) + return self.fullpath ("%s_list.record.%d" % (self.getAuthority(),i)) else: - return os.path.expanduser("~/.sfi/%s_list.record" % self.getAuthority()) + return self.fullpath ("%s_list.record" % self.getAuthority()) # configuration singleton diff --git a/sface/screens/configscreen.py b/sface/screens/configscreen.py index 1197a9f..dc64620 100644 --- a/sface/screens/configscreen.py +++ b/sface/screens/configscreen.py @@ -20,24 +20,48 @@ static_labels = { class ConfigWidget(QWidget): def __init__(self, parent): QWidget.__init__(self, parent) + # init can be called several times for when the config dir is changed + self.inited=False + self.init () + + def store_local (self, name, value): + setattr (self, 'widget_'+name, value) + def retrieve_local (self, name): + return getattr (self, 'widget_'+name, None) + + def init (self): + # if already inited we just need to set the values + if self.inited: + for (field,msg) in config.field_labels(): + edit = self.retrieve_local(field) + if isinstance (edit,QCheckBox): + if config.is_true(config.get(field)): edit.setCheckState (Qt.Checked) + else: edit.setCheckState (Qt.Unchecked) + else: + edit.setText (config.get(field) or "") + return + # otherwise we need to build the whole thing up glayout = QGridLayout() row = 0 for (field,msg) in config.field_labels(): - # edit : text or checkbox - default=config.field_default(field) + if static_labels.has_key(field): labels=static_labels[field] if not isinstance(labels,list): labels = [ labels, ] for label in labels: glayout.addWidget(QLabel(label),row,1) row+=1 + # edit : text or checkbox + default=config.field_default(field) if isinstance(default,bool): edit=QCheckBox(msg) + self.store_local (field, edit) if config.is_true(config.get(field)): edit.setCheckState(Qt.Checked) else: edit=QLineEdit(config.get(field) or "", self) + self.store_local (field, edit) edit.setAttribute(Qt.WA_MacShowFocusRect, 0) setattr(self,field,edit) @@ -48,22 +72,37 @@ class ConfigWidget(QWidget): hlayout = QHBoxLayout() hlayout.addStretch() - for (action,label) in [('apply','Apply'), - ('save','Apply && Save')]: + def conf_button (action,label): button=QPushButton(label, self) button.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum) - hlayout.addWidget(button, 0, Qt.AlignRight) + hlayout.addWidget(button) hlayout.addSpacing(10) self.connect(button, SIGNAL('clicked()'), getattr(self,action)) + conf_button ('load','Load Conf. Dir') + + # the config dir edit dialog + edit=QLineEdit (config.get_dirname(),self) + self.store_local('config_dirname',edit) + edit.setAttribute(Qt.WA_MacShowFocusRect, 0) + edit.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Maximum) + self.connect(edit,SIGNAL ('returnPressed()'), self.load) + hlayout.addWidget (edit) + hlayout.addSpacing(10) + + conf_button ('apply','Apply Only'), + conf_button ('save','Apply && Save') + layout = QVBoxLayout() layout.addLayout(glayout) layout.addLayout(hlayout) layout.addStretch() self.setLayout(layout) + self.inited=True def apply(self): + print 'applying' for field in config.fields(): widget=getattr(self,field) if isinstance(widget,QCheckBox): @@ -83,7 +122,20 @@ class ConfigWidget(QWidget): def save(self): self.apply() config.save_config() - self.parent().setStatus("Configuration saved in %s !"%config.filename(), timeout=5000) + self.parent().setStatus("Configuration saved in %s !"%config.config_file(), timeout=5000) + + # switch to another config dir + def load(self): + # obtain new dor somehow + + edit=self.retrieve_local('config_dirname') + newdir=str(edit.text()) + newdir+='/' + print 'installing',newdir + config.set_dirname (newdir) + self.init() + self.parent().signalAll('configurationChanged') + class ConfigScreen(SfaScreen): def __init__(self, parent): diff --git a/sface/sfiprocess.py b/sface/sfiprocess.py index 8eb0408..f794c63 100644 --- a/sface/sfiprocess.py +++ b/sface/sfiprocess.py @@ -41,6 +41,9 @@ class SfiProcess(QObject): def __init_command(self, args): self.args = QStringList() + self.args << "-d" + self.args << config.get_dirname() + if config.debug: # this shows xmlrpc conversation, see sfi.py docs. self.args << QString('-D')