From: Fred L. D. <fd...@us...> - 2003-07-11 18:39:06
|
Update of /cvsroot/cvs-syncmail/syncmail In directory sc8-pr-cvs1:/tmp/cvs-serv32120 Modified Files: Tag: new-config-branch syncmail Log Message: refactor to make it easier to get the configuration data for separate branches without re-loading the configuration file Index: syncmail =================================================================== RCS file: /cvsroot/cvs-syncmail/syncmail/syncmail,v retrieving revision 1.36.2.14 retrieving revision 1.36.2.15 diff -u -d -r1.36.2.14 -r1.36.2.15 --- syncmail 11 Jul 2003 17:51:33 -0000 1.36.2.14 +++ syncmail 11 Jul 2003 18:39:03 -0000 1.36.2.15 @@ -484,38 +484,28 @@ d[opt] = config.get(section, opt, raw=1) return d -def load_configfile(filename, cmdline, branch): - dicts = [] - if filename: - from ConfigParser import ConfigParser - class ConfigParser(ConfigParser): - # Regular expressions for parsing section headers and options, - # from the Python 2.3 version of ConfigParser. - SECTCRE = re.compile( - r'\[' # [ - r'(?P<header>[^]]+)' # very permissive! - r'\]' # ] - ) - # For compatibility with older versions: - __SECTCRE = SECTCRE - - cp = ConfigParser() - # We have to use this old method for compatibility with - # ancient versions of Python. - cp.read([filename]) - if branch: - dicts.append(get_section_as_dict(cp, "branch " + branch)) - dicts.append(get_section_as_dict(cp, "branch")) - dicts.append(cmdline) - dicts.append(get_section_as_dict(cp, "general")) - else: - dicts.append(cmdline) - dicts = filter(None, dicts) +from ConfigParser import ConfigParser +class ConfigParser(ConfigParser): + + # Regular expressions for parsing section headers and options, + # from the Python 2.3 version of ConfigParser. + + SECTCRE = re.compile( + r'\[' # [ + r'(?P<header>[^]]+)' # very permissive! + r'\]' # ] + ) + + # For compatibility with older versions: + __SECTCRE = SECTCRE + + +class ConfigurationDatabase: + # The defaults set covers what we need but might not get from the # command line or configuration file. defaults = { "context-lines": "2", - "cvsroot": "$CVSROOT", "diff-type": "unified", "email": "true", "from-host": "$HOSTNAME", @@ -524,8 +514,33 @@ "subject-prefix": "", "verbose": "true", } - dicts.append(defaults) - return OptionLookup(dicts, branch) + + def __init__(self, filename, cmdline, args): + self.args = args + self.cmdline = cmdline + self.config = None + if filename and os.path.isfile(filename): + self.config = ConfigParser() + if hasattr(cp, "readfp"): + self.config.readfp(open(filename), filename) + else: + # We have to use this old method for compatibility with + # ancient versions of Python. + self.config.read([filename]) + + def get_config(self, branch): + dicts = [] + if self.config: + if branch: + dicts.append(get_section_as_dict(cp, "branch " + branch)) + dicts.append(get_section_as_dict(cp, "branch")) + dicts.append(self.cmdline) + dicts.append(get_section_as_dict(cp, "general")) + else: + dicts.append(self.cmdline) + dicts = filter(None, dicts) + dicts.append(self.defaults) + return Options(OptionLookup(dicts, branch), self.args) def load_cmdline(args): try: @@ -546,7 +561,7 @@ if opt in ('-h', '--help'): usage(0) elif opt == '--cvsroot': - set('cvsroot', arg) + os.environ["CVSROOT"] = arg elif opt == "--config": set('config-file', arg) elif opt == '--no-config': @@ -605,17 +620,24 @@ def load_configuration(args, branch=None): cmdline, args = load_cmdline(args) + if os.environ.has_key("CVSROOT"): + defconfig = os.path.join(os.environ["CVSROOT"], + "CVSROOT", "syncmail.conf") + else: + defconfig = os.path.join("CVSROOT", "syncmail.conf") if cmdline.has_key('config-file'): cfgfile = cmdline['config-file'] del cmdline['config-file'] + elif os.path.isfile(defconfig): + cfgfile = defconfig else: - cfgfile = "syncmail.conf" - # cfgfile is specified relative to the CVSROOT directory; we need - # to transform the path appropriately, since that won't be the - # current directory when we try to read it. In fact, a wrong + cfgfile = None + # XXX cfgfile is specified relative to the CVSROOT directory; we + # need to transform the path appropriately, since that won't be + # the current directory when we try to read it. In fact, a wrong # config file may exist at the alternate location. - config = load_configfile(cfgfile, cmdline, branch) - return Options(config, args) + db = ConfigurationDatabase(cfgfile, cmdline, args) + return db.get_config(branch) def main(): |