X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/4eb6c6308a028e4d4d0673c8f2090e74294d6e36..f5a081744ba31beed226db828e0e16a01ec77c83:/pymin/services/util.py diff --git a/pymin/services/util.py b/pymin/services/util.py index bfdba1c..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() @@ -577,6 +590,88 @@ class SubHandler(Handler): r"Initialize the object, see the class documentation for details." self.parent = parent +class ListSubHandler(SubHandler): + r"""ListSubHandler(parent) -> ListSubHandler instance. + + This is a helper class to inherit from to automatically handle subcommands + that operates over a list parent attribute. + + The list attribute to handle and the class of objects that it contains can + be defined by calling the constructor or in a more declarative way as + class attributes, like: + + class TestHandler(ListSubHandler): + _list_subhandler_attr = 'some_list' + _list_subhandler_class = SomeClass + + This way, the parent's some_list attribute (self.parent.some_list) will be + managed automatically, providing the commands: add, update, delete, get, + list and show. New items will be instances of SomeClass, which should + provide a cmp operator to see if the item is on the list and an update() + method, if it should be possible to modify it. + """ + + def __init__(self, parent, attr=None, cls=None): + r"Initialize the object, see the class documentation for details." + self.parent = parent + if attr is not None: + self._list_subhandler_attr = attr + if cls is not None: + self._list_subhandler_class = cls + + def _list(self): + return getattr(self.parent, self._list_subhandler_attr) + + @handler(u'Add a new item') + def add(self, *args, **kwargs): + r"add(...) -> None :: Add an item to the list." + item = self._list_subhandler_class(*args, **kwargs) + if item in self._list(): + raise ItemAlreadyExistsError(item) + self._list().append(item) + + @handler(u'Update an item') + def update(self, index, *args, **kwargs): + r"update(index, ...) -> None :: Update an item of the list." + # TODO make it right with metaclasses, so the method is not created + # unless the update() method really exists. + # TODO check if the modified item is the same of an existing one + index = int(index) # TODO validation + if not hasattr(self._list_subhandler_class, 'update'): + raise CommandNotFoundError(('update',)) + try: + self._list()[index].update(*args, **kwargs) + except IndexError: + raise ItemNotFoundError(index) + + @handler(u'Delete an item') + def delete(self, index): + r"delete(index) -> None :: Delete an item of the list." + index = int(index) # TODO validation + try: + return self._list().pop(index) + except IndexError: + raise ItemNotFoundError(index) + + @handler(u'Get information about an item') + def get(self, index): + r"get(index) -> Host :: List all the information of an item." + index = int(index) # TODO validation + try: + return self._list()[index] + except IndexError: + raise ItemNotFoundError(index) + + @handler(u'Get how many items are in the list') + def len(self): + r"len() -> int :: Get how many items are in the list." + return len(self._list()) + + @handler(u'Get information about all items') + def show(self): + r"show() -> list of Hosts :: List all the complete items information." + return self._list() + class DictSubHandler(SubHandler): r"""DictSubHandler(parent) -> DictSubHandler instance. @@ -593,7 +688,9 @@ class DictSubHandler(SubHandler): This way, the parent's some_dict attribute (self.parent.some_dict) will be managed automatically, providing the commands: add, update, delete, get, - list and show. + list and show. New items will be instances of SomeClass, which should + provide a constructor with at least the key value and an update() method, + if it should be possible to modify it. """ def __init__(self, parent, attr=None, cls=None): @@ -651,7 +748,6 @@ class DictSubHandler(SubHandler): return self._dict().values() - if __name__ == '__main__': # Execution tests