-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path01_plugin.py
More file actions
105 lines (98 loc) · 2.73 KB
/
Copy path01_plugin.py
File metadata and controls
105 lines (98 loc) · 2.73 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Plugins = []
class Plugin:
instance = None
def __init__(self):
self.__class__.instance = self
self._hooks = {}
self._commands = {}
self._timers = {}
def _initialize(self):
puts("Initializing " + self.__class__.__name__)
def initialize(self):
puts("Disposing " + self.__class__.__name__)
def _dispose(self):
self.unhook_all()
self.remove_all_commands()
self.remove_all_timers()
def dispose(self):
pass
def hook(self, handler_collection, f, *args):
if self._hooks.ContainsKey(f):
self.unhook(f)
puts('hook is already registered, reregistering...')
self._hooks[f] = handler_collection
_hook(handler_collection, f, *args)
def unhook(self, f):
if f in self._hooks:
_unhook(self._hooks[f], f)
self._hooks.Remove(f)
else:
raise Exception("Can't deregister hook")
def unhook_all(self):
for hookf in self._hooks.copy():
self.unhook(hookf)
def add_command(self, permission, f, *names):
if self._commands.ContainsKey(f):
self.remove_command(f)
puts('command is already registered, reregistering...')
def cmdF(args):
try:
f(args)
except:
ptraceback()
command = _add_command(permission, cmdF, names)
self._commands[f] = command
return command
def remove_command(self, f):
if f in self._commands:
_remove_command(self._commands[f])
self._commands.Remove(f)
else:
raise Exception("Can't deregister command")
def remove_all_commands(self):
for commandf in self._commands.copy():
self.remove_command(commandf)
def add_timer(self, ms, f):
if self._timers.ContainsKey(f):
self.remove_timer(f)
puts('timer is already added, readding...')
timer = self._timers[f] = create_timer(ms, f, True)
return timer
def remove_timer(self, f):
if f in self._timers:
self._timers[f].Stop()
self._timers.Remove(f)
else:
raise Exception("Can't remove timer")
def remove_all_timers(self):
for f in self._timers.copy():
self.remove_timer(f)
@staticmethod
def Initialize():
puts('IronPython plugins begin ========================')
for name, plugin_class in list(globals().items()):
if (type(plugin_class) == type(Plugin) and plugin_class != Plugin and len(plugin_class.__bases__) > 0
and plugin_class.__bases__[0] == Plugin and plugin_class.Load()):
try:
plugin = plugin_class()
Plugins.Add(plugin)
plugin._initialize()
plugin.initialize()
except:
ptraceback()
puts('IronPython plugins end ==========================')
@staticmethod
def Dispose():
for plugin in Plugins:
try:
# Removing all hooks, commands and timers after
plugin._dispose()
# Custom dispose first
plugin.dispose()
except:
ptraceback()
@staticmethod
def Load():
return True
def plugins():
return [p.__class__.__name__ for p in Plugins]