This fixes some issues with global_config relating to the fact
that before all you could get were strings.
From: Travis Miller <raphtee@google.com>
git-svn-id: http://test.kernel.org/svn/autotest/trunk@1326 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/client/common_lib/global_config.py b/client/common_lib/global_config.py
index a840d98..0411cbd 100644
--- a/client/common_lib/global_config.py
+++ b/client/common_lib/global_config.py
@@ -10,29 +10,50 @@
import ConfigParser
import error
+
class ConfigError(error.AutotestError):
pass
+class ConfigValueError(ConfigError):
+ pass
+
+
class global_config(object):
config = None
- def get_config_value(self, section, key, default=None):
+ def get_config_value(self, section, key, type=str, default=None):
if self.config == None:
self.parse_config_file()
try:
- return self.config.get(section, key)
- except:
- if default == None:
- msg = ("Value '%s' not found in section '%s'" %
- (key, section))
- raise ConfigError(msg)
- else:
- return default
-
-
+ val = self.config.get(section, key)
+ except:
+ if default == None:
+ msg = ("Value '%s' not found in section '%s'" %
+ (key, section))
+ raise ConfigError(msg)
+ else:
+ return default
+
+ return self.convert_value(key, section, val, type, default)
+
+
+ def merge_configs(self, shadow_config):
+ # overwrite whats in config with whats in shadow_config
+ sections = shadow_config.sections()
+ for section in sections:
+ # add the section if need be
+ if not self.config.has_section(section):
+ self.config.add_section(section)
+ # now run through all options and set them
+ options = shadow_config.options(section)
+ for option in options:
+ val = shadow_config.get(section, option)
+ self.config.set(section, option, val)
+
+
def parse_config_file(self):
dirname = os.path.dirname(sys.modules[__name__].__file__)
root = os.path.abspath(os.path.join(dirname, "../../"))
@@ -40,8 +61,52 @@
self.config = ConfigParser.ConfigParser()
self.config.read(config_file)
+ # now also read the shadow file if there is one
+ # this will overwrite anything that is found in the
+ # other config
+ config_file = os.path.join(root, "shadow_config.ini")
+ if os.path.exists(config_file):
+ shadow_config = ConfigParser.ConfigParser()
+ shadow_config.read(config_file)
+ # now we merge shadow into global
+ self.merge_configs(shadow_config)
+
+
+ # the values that are pulled from ini
+ # are strings. But we should attempt to
+ # convert them to other types if needed.
+ def convert_value(self, key, section, value, type, default):
+ # strip off leading and trailing white space
+ sval = value.strip()
-
+ # if length of string is zero then return None
+ if len(sval) == 0:
+ if type == str:
+ return ""
+ elif type == bool:
+ return False
+ elif type == int:
+ return 0
+ elif type == float:
+ return 0.0
+ else:
+ return None
+
+ if type == bool:
+ if sval.lower() == "false":
+ return False
+ else:
+ return True
+
+ try:
+ conv_val = type(sval)
+ return conv_val
+ except:
+ msg = ("Could not covert %s in section %s" %
+ (key, section))
+ raise ConfigValueError(msg)
+
+
# insure the class is a singleton. Now the symbol global_config
# will point to the one and only one instace of the class
global_config = global_config()
diff --git a/global_config.ini b/global_config.ini
index e2f19ec..4fe9a87 100644
--- a/global_config.ini
+++ b/global_config.ini
@@ -9,5 +9,6 @@
host: localhost
database: autotest_web
db_type: mysql
-user: autotest
+user: nobody
password:
+
diff --git a/scheduler/monitor_db b/scheduler/monitor_db
index 48fa107..862eb23 100755
--- a/scheduler/monitor_db
+++ b/scheduler/monitor_db
@@ -155,16 +155,15 @@
# get global config and parse for info
c = global_config.global_config
dbase = "AUTOTEST_WEB"
- DB_HOST = c.get_config_value(dbase, "host", "localhost")
- DB_SCHEMA = c.get_config_value(dbase, "database",
- "autotest_web")
+ DB_HOST = c.get_config_value(dbase, "host")
+ DB_SCHEMA = c.get_config_value(dbase, "database")
global _testing_mode
if _testing_mode:
DB_SCHEMA = 'stresstest_autotest_web'
- DB_USER = c.get_config_value(dbase, "user", "autotest")
- DB_PASS = c.get_config_value(dbase, "password", "google")
+ DB_USER = c.get_config_value(dbase, "user")
+ DB_PASS = c.get_config_value(dbase, "password")
while not self.conn:
try:
diff --git a/tko/db.py b/tko/db.py
index 789517a..125798e 100644
--- a/tko/db.py
+++ b/tko/db.py
@@ -15,15 +15,15 @@
# grab the host, database
if not host:
- host = c.get_config_value("TKO", "host", 'localhost')
+ host = c.get_config_value("TKO", "host")
if not database:
- database = c.get_config_value("TKO", "database", 'tko')
+ database = c.get_config_value("TKO", "database")
# grab the user and password
if not user:
- user = c.get_config_value("TKO", "user", 'nobody')
+ user = c.get_config_value("TKO", "user")
if not password:
- password = c.get_config_value("TKO", "password", '')
+ password = c.get_config_value("TKO", "password")
self.con = self.connect(host, database, user, password)
self.cur = self.con.cursor()