|
[Webware-checkins] CVS: Webware/MiscUtils Configurable.py,1.10,1.11
From: Ian Bicking <ianbicking@us...> - 2002-11-15 06:38
|
Update of /cvsroot/webware/Webware/MiscUtils
In directory usw-pr-cvs1:/tmp/cvs-serv28916/MiscUtils
Modified Files:
Configurable.py
Log Message:
Allow settings to be changed via the command line (overriding
configuration files), like:
./AppServer --AppServer.AutoReload=1
Index: Configurable.py
===================================================================
RCS file: /cvsroot/webware/Webware/MiscUtils/Configurable.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** Configurable.py 10 Nov 2002 12:37:15 -0000 1.10
--- Configurable.py 15 Nov 2002 01:42:23 -0000 1.11
***************
*** 1,3 ****
! import string, sys
from types import DictType
from MiscUtils import AbstractError, NoDefault
--- 1,3 ----
! import string, sys, os
from types import DictType
from MiscUtils import AbstractError, NoDefault
***************
*** 55,58 ****
--- 55,59 ----
self._config = self.defaultConfig()
self._config.update(self.userConfig())
+ self._config.update(self.commandLineConfig())
return self._config
***************
*** 75,78 ****
--- 76,86 ----
raise AbstractError, self.__class__
+ def configName(self):
+ """
+ Returns the name of the configuration file (the portion
+ before the '.config'). This is used on the command-line.
+ """
+ return os.path.splitext(os.path.basename(self.configFilename()))[0]
+
def configReplacementValues(self):
"""
***************
*** 122,123 ****
--- 130,171 ----
dest.write(string.ljust(key, width)+' = '+str(self.setting(key))+'\n')
dest.write('\n')
+
+ def commandLineConfig(self):
+ """
+ Settings that came from the command line (via
+ addCommandLineSetting).
+ """
+ return _settings.get(self.configName(), {})
+
+ _settings = {}
+ def addCommandLineSetting(name, value):
+ """
+ Take a setting, like --AppServer.Verbose=0, and call
+ addCommandLineSetting('AppServer.Verbose', '0'), and
+ it will override any settings in AppServer.config
+ """
+ configName, settingName = string.split(name, '.', 1)
+ value = convertValue(value)
+ if not _settings.has_key(configName):
+ _settings[configName] = {}
+ _settings[configName][settingName] = value
+
+ def convertValue(value):
+ """
+ Takes a string and tries to convert it into a Python
+ type (integer, list, etc.) -- if nothing works, then it
+ leaves it as a string.
+ """
+ if not value:
+ return ''
+ try:
+ return int(value)
+ except ValueError:
+ pass
+ try:
+ return float(value)
+ except ValueError:
+ pass
+ if value[0] in '[({"\'':
+ return eval(value)
+ return value
|
| Thread | Author | Date |
|---|---|---|
| [Webware-checkins] CVS: Webware/MiscUtils Configurable.py,1.10,1.11 | Ian Bicking <ianbicking@us...> |