diff options
author | Rob Austein <sra@hactrn.net> | 2007-12-24 09:31:48 +0000 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2007-12-24 09:31:48 +0000 |
commit | 4d7072bd10f807558dfd60c2a3e65fe6584bcdb3 (patch) | |
tree | 291536b8a8e9c26ff5ac46d988b5587f2d9fc75d /scripts/rpki | |
parent | 9bd1ce773943b587f83c91c77de7e379df421c63 (diff) |
Teach config parser about default sections.
svn path=/scripts/Makefile; revision=1439
Diffstat (limited to 'scripts/rpki')
-rw-r--r-- | scripts/rpki/config.py | 13 | ||||
-rw-r--r-- | scripts/rpki/sql.py | 8 |
2 files changed, 13 insertions, 8 deletions
diff --git a/scripts/rpki/config.py b/scripts/rpki/config.py index 962f28fd..7a60f95b 100644 --- a/scripts/rpki/config.py +++ b/scripts/rpki/config.py @@ -9,20 +9,23 @@ import ConfigParser class parser(ConfigParser.RawConfigParser): - def __init__(self, file=None): + def __init__(self, file = None, section = None): """Initialize this parser.""" ConfigParser.RawConfigParser.__init__(self) if file: self.read(file) + self.default_section = section - def multiget(self, section, option): + def multiget(self, option, section = None): """Parse OpenSSL-style foo.0, foo.1, ... subscripted options. Returns a list of values matching the specified option name. """ matches = [] + if section is None: + section = self.default_section if self.has_option(section, option): - matches.append((-1, self.get(section, option))) + matches.append((-1, self.get(option, section = section))) for key, value in self.items(section): s = key.rsplit(".", 1) if len(s) == 2 and s[0] == option and s[1].isdigit(): @@ -30,8 +33,10 @@ class parser(ConfigParser.RawConfigParser): matches.sort() return [match[1] for match in matches] - def get(self, section, option, default=None): + def get(self, option, default = None, section = None): """Get an option, perhaps with a default value.""" + if section is None: + section = self.default_section if default is None or self.has_option(section, option): return ConfigParser.RawConfigParser.get(self, section, option) else: diff --git a/scripts/rpki/sql.py b/scripts/rpki/sql.py index b461a38c..6360fc83 100644 --- a/scripts/rpki/sql.py +++ b/scripts/rpki/sql.py @@ -3,13 +3,13 @@ import MySQLdb, time, traceback import rpki.x509, rpki.resource_set, rpki.sundial -def connect(cfg, section="sql"): +def connect(cfg): """Connect to a MySQL database using connection parameters from an rpki.config.parser object. """ - return MySQLdb.connect(user = cfg.get(section, "sql-username"), - db = cfg.get(section, "sql-database"), - passwd = cfg.get(section, "sql-password")) + return MySQLdb.connect(user = cfg.get("sql-username"), + db = cfg.get("sql-database"), + passwd = cfg.get("sql-password")) class template(object): """SQL template generator.""" |