|
7 | 7 |
|
8 | 8 | plugin_folder = os.path.join(os.path.dirname(__file__), 'commands') |
9 | 9 |
|
| 10 | +_possible_plugin_folders = list(set([ |
| 11 | + os.path.join(os.path.dirname(__file__), 'commands'), |
| 12 | + os.path.join(click.get_app_dir("OpenPathSampling"), 'cli-plugins'), |
| 13 | + os.path.join(click.get_app_dir("OpenPathSampling", force_posix=True), |
| 14 | + 'cli-plugins'), |
| 15 | +])) |
| 16 | + |
| 17 | +OPSPlugin = collections.namedtuple("OPSPlugin", |
| 18 | + ['name', 'filename', 'func', 'section']) |
| 19 | + |
10 | 20 | class OpenPathSamplingCLI(click.MultiCommand): |
11 | 21 | def __init__(self, *args, **kwargs): |
12 | | - self.plugin_folders = [ |
| 22 | + # the logic here is all about loading the plugins |
| 23 | + plugin_folders = [ |
13 | 24 | os.path.join(os.path.dirname(__file__), 'commands'), |
14 | 25 | os.path.join(click.get_app_dir("OpenPathSampling", |
15 | 26 | force_posix=True), |
16 | 27 | 'cli-plugins') |
17 | 28 | ] |
| 29 | + self.plugin_folders = [f for f in _possible_plugin_folders |
| 30 | + if os.path.exists(f)] |
| 31 | + plugin_files = self._list_plugin_files(self.plugin_folders) |
| 32 | + self.plugins = self._load_plugin_files(plugin_files) |
| 33 | + |
18 | 34 | self._get_command = {} |
19 | 35 | self._sections = collections.defaultdict(list) |
20 | | - plugin_files, command_list = self._list_plugins() |
21 | | - for cmd, plugin_file in zip(command_list, plugin_files): |
22 | | - command, section = self._load_plugin(plugin_file) |
23 | | - self._get_command[cmd] = command |
24 | | - self._sections[section].append(cmd) |
| 36 | + for plugin in self.plugins: |
| 37 | + self._get_command[plugin.name] = plugin.func |
| 38 | + self._sections[plugin.section].append(plugin.name) |
| 39 | + |
25 | 40 | super(OpenPathSamplingCLI, self).__init__(*args, **kwargs) |
26 | 41 |
|
27 | | - def _load_plugin(self, name): |
| 42 | + @staticmethod |
| 43 | + def _list_plugin_files(plugin_folders): |
| 44 | + plugin_files = [] |
| 45 | + for folder in plugin_folders: |
| 46 | + files = [os.path.join(folder, f) for f in os.listdir(folder) |
| 47 | + if f.endswith(".py")] |
| 48 | + plugin_files += files |
| 49 | + return plugin_files |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def _filename_to_command_name(filename): |
| 53 | + command_name = filename[:-3] # get rid of .py |
| 54 | + command_name = command_name.replace('_', '-') # commands use - |
| 55 | + return command_name |
| 56 | + |
| 57 | + @staticmethod |
| 58 | + def _load_plugin(name): |
28 | 59 | ns = {} |
29 | | - fn = os.path.join(plugin_folder, name + '.py') |
30 | | - with open(fn) as f: |
31 | | - code = compile(f.read(), fn, 'exec') |
| 60 | + with open(name) as f: |
| 61 | + code = compile(f.read(), name, 'exec') |
32 | 62 | eval(code, ns, ns) |
33 | 63 | return ns['CLI'], ns['SECTION'] |
34 | 64 |
|
35 | | - def _list_plugins(self): |
36 | | - files = [] |
37 | | - commands = [] |
38 | | - for filename in os.listdir(plugin_folder): |
39 | | - if filename.endswith('.py'): |
40 | | - command = filename.replace('_', '-') |
41 | | - files.append(filename[:-3]) |
42 | | - commands.append(command[:-3]) |
43 | | - return files, commands |
| 65 | + def _load_plugin_files(self, plugin_files): |
| 66 | + plugins = [] |
| 67 | + for full_name in plugin_files: |
| 68 | + head, filename = os.path.split(full_name) |
| 69 | + command_name = self._filename_to_command_name(filename) |
| 70 | + func, section = self._load_plugin(full_name) |
| 71 | + plugins.append(OPSPlugin(name=command_name, filename=full_name, |
| 72 | + func=func, section=section)) |
| 73 | + return plugins |
44 | 74 |
|
45 | 75 | def list_commands(self, ctx): |
46 | 76 | return list(self._get_command.keys()) |
47 | 77 |
|
48 | 78 | def get_command(self, ctx, name): |
49 | | - name = name.replace('_', '-') # auto alias to allow - or _ |
| 79 | + name = name.replace('_', '-') # allow - or _ from user |
50 | 80 | return self._get_command.get(name) |
51 | 81 |
|
52 | 82 | def format_commands(self, ctx, formatter): |
|
0 commit comments