-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool_objects.py
More file actions
76 lines (49 loc) · 1.97 KB
/
tool_objects.py
File metadata and controls
76 lines (49 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#
import traceback
import pdb
class ToolTemplate(object):
# object new Tools should be subclassed from
def __init__ (self):
self.tool_guid = None
self.name = None
self.menu_name = None
self.menu_tip = None
self.menu_path = None
self.stock_id = None
def run (self):
raise NotImplementedError("The run method MUST be defined in the subclass!!")
# we need these dicts to lookup the tool object on callback
python_tools_by_name = {}
python_tools_by_guid = {}
def load_python_tools ():
# yes we need the global to write into these objects
global python_tools_by_name
global python_tools_by_guid
#pdb.set_trace()
python_tools_by_name = {}
python_tools_by_gui = {}
if False:
try:
from tools.hello_world import HelloWorld
python_tools_by_name['HelloWorld'] = HelloWorld()
except Exception as errexc:
traceback.print_exc()
pdb.set_trace()
if True:
# code to try for autoimporting - so can just add new tools
# only works assuming always use class name as tools name in python_tools
try:
import tools
except Exception as errexc:
traceback.print_exc()
pdb.set_trace()
# if we want to find all new classes another way is to use introspection
# and find all classes whose __module__ attribute is the module name it was imported
# under
# this is a very sneaky way to find new classes if all classes you want
# are guaranteed to be subclasses of a specific class
__all__classes = [ cls for cls in ToolTemplate.__subclasses__() ]
# instantiate all tool objects and save in the lookup dicts
for tool_cls in __all__classes:
python_tools_by_name[tool_cls.__name__] = tool_cls()
python_tools_by_guid[python_tools_by_name[tool_cls.__name__].tool_guid] = python_tools_by_name[tool_cls.__name__]