From f5a081744ba31beed226db828e0e16a01ec77c83 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Thu, 11 Oct 2007 17:10:30 -0300 Subject: [PATCH 1/1] Add a new, backward compatible, way to specify specific config dirs. Now _config_writer_cfg_dir could be a dictionary instead of a string. The dictionary maps template filenames to directories where to generate (render) the configure files. See the DnsHandler for an example. --- config.py | 5 ++++- pymin/services/dns/__init__.py | 2 +- pymin/services/util.py | 25 +++++++++++++++++++------ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/config.py b/config.py index ca0cc65..d868c41 100644 --- a/config.py +++ b/config.py @@ -15,7 +15,10 @@ class Root(Handler): config_dir = join(config_path, 'dhcp')) dns = DnsHandler( pickle_dir = join(pickle_path, 'dns'), - config_dir = join(config_path, 'dns')) + config_dir = { + 'named.conf': join(config_path, 'dns'), + 'zoneX.zone': join(config_path, 'dns', 'zones'), + }) firewall = FirewallHandler( pickle_dir = join(pickle_path, 'firewall'), config_dir = join(config_path, 'firewall')) diff --git a/pymin/services/dns/__init__.py b/pymin/services/dns/__init__.py index 13aaa08..32e1772 100644 --- a/pymin/services/dns/__init__.py +++ b/pymin/services/dns/__init__.py @@ -389,7 +389,7 @@ class DnsHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler, _config_writer_files = ('named.conf', 'zoneX.zone') _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates') - def __init__(self, pickle_dir='.', config_dir='.'): + def __init__(self, pickle_dir='.', config_dir={'named.conf':'.', 'zoneX.zone':'.'}): r"Initialize DnsHandler object, see class documentation for details." self._persistent_dir = pickle_dir self._config_writer_cfg_dir = config_dir diff --git a/pymin/services/util.py b/pymin/services/util.py index 5b89528..95a07e9 100644 --- a/pymin/services/util.py +++ b/pymin/services/util.py @@ -283,18 +283,24 @@ class ConfigWriter: class TestHandler(ConfigWriter): _config_writer_files = ('base.conf', 'custom.conf') - _config_writer_cfg_dir = '/etc/service' + _config_writer_cfg_dir = { + 'base.conf': '/etc/service', + 'custom.conf': '/etc/service/conf.d', + } _config_writer_tpl_dir = 'templates' The generated configuration files directory defaults to '.' and the templates directory to 'templates'. _config_writer_files has no default and must be specified in either way. It can be string or a tuple if more than - one configuration file must be generated. + one configuration file must be generated. _config_writer_cfg_dir could be a + dict mapping which file should be stored in which directory, or a single + string if all the config files should go to the same directory. The template filename and the generated configuration filename are both the same (so if you want to generate some /etc/config, you should have some templates/config template). That's why _config_writer_cfg_dir and - _config_writer_tpl_dir can't be the same. + _config_writer_tpl_dir can't be the same. This is not true for very + specific cases where _write_single_config() is used. When you write your Handler, you should call _config_build_templates() in you Handler constructor to build the templates. @@ -350,6 +356,15 @@ class ConfigWriter: vars = vars(template_name) return self._config_writer_templates[template_name].render(**vars) + def _get_config_path(self, template_name, config_filename=None): + r"Get a complete configuration path." + if not config_filename: + config_filename = template_name + if isinstance(self._config_writer_cfg_dir, basestring): + return path.join(self._config_writer_cfg_dir, config_filename) + return path.join(self._config_writer_cfg_dir[template_name], + config_filename) + def _write_single_config(self, template_name, config_filename=None, vars=None): r"""_write_single_config(template_name[, config_filename[, vars]]). @@ -362,8 +377,6 @@ class ConfigWriter: variables to replace in the templates, if not, it looks for a _get_config_vars() method to get it. """ - if not config_filename: - config_filename = template_name if vars is None: if hasattr(self, '_get_config_vars'): vars = self._get_config_vars(template_name) @@ -371,7 +384,7 @@ class ConfigWriter: vars = dict() elif callable(vars): vars = vars(template_name) - f = file(path.join(self._config_writer_cfg_dir, config_filename), 'w') + f = file(self._get_config_path(template_name, config_filename), 'w') ctx = Context(f, **vars) self._config_writer_templates[template_name].render_context(ctx) f.close() -- 2.43.0