|
4 | 4 | """ |
5 | 5 | # builds off the example of MultiCommand in click's docs |
6 | 6 | import collections |
| 7 | +import logging |
| 8 | +import logging.config |
7 | 9 | import os |
8 | 10 |
|
9 | 11 | import click |
@@ -36,16 +38,30 @@ def __init__(self, *args, **kwargs): |
36 | 38 | self.plugin_folders.append(folder) |
37 | 39 |
|
38 | 40 | plugin_files = self._list_plugin_files(self.plugin_folders) |
39 | | - self.plugins = self._load_plugin_files(plugin_files) |
| 41 | + plugins = self._load_plugin_files(plugin_files) |
40 | 42 |
|
41 | 43 | self._get_command = {} |
42 | 44 | self._sections = collections.defaultdict(list) |
43 | | - for plugin in self.plugins: |
44 | | - self._get_command[plugin.name] = plugin.func |
45 | | - self._sections[plugin.section].append(plugin.name) |
| 45 | + self.plugins = [] |
| 46 | + for plugin in plugins: |
| 47 | + self._register_plugin(plugin) |
46 | 48 |
|
47 | 49 | super(OpenPathSamplingCLI, self).__init__(*args, **kwargs) |
48 | 50 |
|
| 51 | + def _register_plugin(self, plugin): |
| 52 | + self.plugins.append(plugin) |
| 53 | + self._get_command[plugin.name] = plugin.func |
| 54 | + self._sections[plugin.section].append(plugin.name) |
| 55 | + |
| 56 | + def _deregister_plugin(self, plugin): |
| 57 | + # mainly used in testing |
| 58 | + self.plugins.remove(plugin) |
| 59 | + del self._get_command[plugin.name] |
| 60 | + self._sections[plugin.section].remove(plugin.name) |
| 61 | + |
| 62 | + def plugin_for_command(self, command_name): |
| 63 | + return {p.name: p for p in self.plugins}[name] |
| 64 | + |
49 | 65 | @staticmethod |
50 | 66 | def _list_plugin_files(plugin_folders): |
51 | 67 | def is_plugin(filename): |
@@ -118,16 +134,17 @@ def format_commands(self, ctx, formatter): |
118 | 134 | openpathsampling strip-snapshots --help |
119 | 135 | """ |
120 | 136 |
|
121 | | -OPS_CLI = OpenPathSamplingCLI( |
122 | | - name="openpathsampling", |
123 | | - help=_MAIN_HELP, |
124 | | - context_settings=CONTEXT_SETTINGS |
125 | | -) |
126 | | - |
127 | | -def main(): # no-cov |
128 | | - OPS_CLI() |
| 137 | +@click.command(cls=OpenPathSamplingCLI, name="openpathsampling", |
| 138 | + help=_MAIN_HELP, context_settings=CONTEXT_SETTINGS) |
| 139 | +@click.option('--log', type=click.Path(exists=True, readable=True), |
| 140 | + help="logging configuration file") |
| 141 | +def main(log): |
| 142 | + if log: |
| 143 | + logging.config.fileConfig(log, disable_existing_loggers=False) |
| 144 | + # TODO: if log not given, check for logging.conf in .openpathsampling/ |
129 | 145 |
|
| 146 | + logger = logging.getLogger(__name__) |
| 147 | + logger.debug("About to run command") # TODO: maybe log invocation? |
130 | 148 |
|
131 | 149 | if __name__ == '__main__': # no-cov |
132 | | - main() |
133 | | - # print("list commands:", cli.list_commands()) |
| 150 | + cli() |
0 commit comments