From fe3a999cb27f99e6a47c310db6808b9771827ab5 Mon Sep 17 00:00:00 2001 From: Alexander Kukushkin Date: Thu, 16 Jun 2016 10:26:06 +0200 Subject: [PATCH] Enforce name requirements for dcs implementations Class implementing AbstractDCS must have name similar to the module name. I.e. Patroni will load ZooKeeper from zookeeper.py, but not from exhibitor.py, although it (ZooKeeper) is also available there. --- patroni/dcs/__init__.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/patroni/dcs/__init__.py b/patroni/dcs/__init__.py index 9662a708..c1c31174 100644 --- a/patroni/dcs/__init__.py +++ b/patroni/dcs/__init__.py @@ -32,21 +32,21 @@ def parse_connection_string(value): def get_dcs(config): available_implementations = set() - for name in os.listdir(os.path.dirname(__file__)): - if name.endswith('.py') and not name.startswith('__'): # find module - module = importlib.import_module(__package__ + '.' + name[:-3]) - for name in dir(module): # iterate through module content - if not name.startswith('__'): # skip internal stuff - value = getattr(module, name) - name = name.lower() - # try to find implementation of AbstractDCS interface - if inspect.isclass(value) and issubclass(value, AbstractDCS) and value != AbstractDCS: - available_implementations.add(name) - if name in config: # which has configuration section in the config file - # propagate some parameters - config[name].update({p: config[p] for p in ('namespace', 'name', - 'scope', 'ttl', 'retry_timeout') if p in config}) - return value(config[name]) + for module in os.listdir(os.path.dirname(__file__)): + if module.endswith('.py') and not module.startswith('__'): # find module + module_name = module[:-3].lower() + module = importlib.import_module(__package__ + '.' + module[:-3]) + for name in filter(lambda name: not name.startswith('__'), dir(module)): # iterate through module content + value = getattr(module, name) + name = name.lower() + # try to find implementation of AbstractDCS interface, class name must match with module_name + if inspect.isclass(value) and issubclass(value, AbstractDCS) and name == module_name: + available_implementations.add(name) + if name in config: # which has configuration section in the config file + # propagate some parameters + config[name].update({p: config[p] for p in ('namespace', 'name', + 'scope', 'ttl', 'retry_timeout') if p in config}) + return value(config[name]) raise PatroniException("""Can not find suitable configuration of distributed configuration store Available implementations: """ + ', '.join(available_implementations))