Commit 5be3e8612ffd7737eed14c5079bb5536b7cba554

Authored by Thiago Franco de Moraes
1 parent 695d4374
Exists in master

Finding plugin recursively on plugins folders

Showing 1 changed file with 25 additions and 19 deletions   Show diff stats
invesalius/plugins.py
... ... @@ -17,15 +17,17 @@
17 17 # detalhes.
18 18 # --------------------------------------------------------------------
19 19  
  20 +import glob
20 21 import importlib.util
21 22 import json
  23 +import pathlib
22 24 import sys
  25 +from itertools import chain
23 26  
24 27 from pubsub import pub as Publisher
25 28  
26 29 import invesalius.constants as consts
27 30 from invesalius import inv_paths
28   -from itertools import chain
29 31  
30 32  
31 33 def import_source(module_name, module_file_path):
... ... @@ -45,24 +47,28 @@ class PluginManager:
45 47  
46 48 def find_plugins(self):
47 49 self.plugins = {}
48   - for p in sorted(chain(inv_paths.USER_PLUGINS_DIRECTORY.glob("*"),\
49   - inv_paths.PLUGIN_DIRECTORY.glob("*"))):
50   - if p.is_dir():
51   - try:
52   - with p.joinpath("plugin.json").open() as f:
53   - jdict = json.load(f)
54   - plugin_name = jdict["name"]
55   - plugin_description = jdict["description"]
56   - enable_startup = jdict.get("enable-startup", False)
  50 + for p in sorted(
  51 + chain(
  52 + glob.glob(str(inv_paths.USER_PLUGINS_DIRECTORY.joinpath("**/plugin.json")), recursive=True),
  53 + glob.glob(str(inv_paths.PLUGIN_DIRECTORY.joinpath("**/plugin.json")), recursive=True),
  54 + )
  55 + ):
  56 + try:
  57 + p = pathlib.Path(p)
  58 + with p.open() as f:
  59 + jdict = json.load(f)
  60 + plugin_name = jdict["name"]
  61 + plugin_description = jdict["description"]
  62 + enable_startup = jdict.get("enable-startup", False)
57 63  
58   - self.plugins[plugin_name] = {
59   - "name": plugin_name,
60   - "description": plugin_description,
61   - "folder": p,
62   - "enable_startup": enable_startup,
63   - }
64   - except Exception as err:
65   - print("It was not possible to load plugin. Error: {}".format(err))
  64 + self.plugins[plugin_name] = {
  65 + "name": plugin_name,
  66 + "description": plugin_description,
  67 + "folder": p.parent,
  68 + "enable_startup": enable_startup,
  69 + }
  70 + except Exception as err:
  71 + print("It was not possible to load plugin. Error: {}".format(err))
66 72  
67 73 Publisher.sendMessage("Add plugins menu items", items=self.plugins)
68 74  
... ... @@ -72,5 +78,5 @@ class PluginManager:
72 78 plugin_name, self.plugins[plugin_name]["folder"].joinpath("__init__.py")
73 79 )
74 80 sys.modules[plugin_name] = plugin_module
75   - main = importlib.import_module(plugin_name + '.main')
  81 + main = importlib.import_module(plugin_name + ".main")
76 82 main.load()
... ...