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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
"""
Define the PluginConfig and Plugin classes, plus the SafetyMetaclass.
These are used in the plugin system added in poezio 0.7.5
(see plugin_manager.py)
"""
import os
from functools import partial
from configparser import RawConfigParser
from timed_events import TimedEvent, DelayedEvent
import config
import inspect
import traceback
class PluginConfig(config.Config):
"""
Plugin configuration object.
They are accessible inside the plugin with self.config
and behave like the core Config object.
"""
def __init__(self, filename, module_name):
self.file_name = filename
self.module_name = module_name
RawConfigParser.__init__(self, None)
self.read()
def get(self, option, default, section=None):
if not section:
section = self.module_name
return config.Config.get(self, option, default, section)
def set(self, option, default, section=None):
if not section:
section = self.module_name
return config.Config.set(self, option, default, section)
def read(self):
"""Read the config file"""
RawConfigParser.read(self, self.file_name)
if not self.has_section(self.module_name):
self.add_section(self.module_name)
def options(self, section=None):
"""
Return the options of the section
If no section is given, it defaults to the plugin name.
"""
if not section:
section = self.module_name
if not self.has_section(section):
self.add_section(section)
return config.Config.options(self, section)
def write(self):
"""Write the config to the disk"""
try:
fp = open(self.file_name, 'w')
RawConfigParser.write(self, fp)
fp.close()
return True
except IOError:
return False
class SafetyMetaclass(type):
# A hack
core = None
@staticmethod
def safe_func(f):
def helper(*args, **kwargs):
try:
return f(*args, **kwargs)
except:
if inspect.stack()[1][1] == inspect.getfile(f):
raise
elif SafetyMetaclass.core:
SafetyMetaclass.core.information(traceback.format_exc())
return None
return helper
def __new__(meta, name, bases, class_dict):
for k, v in class_dict.items():
if inspect.isfunction(v):
class_dict[k] = SafetyMetaclass.safe_func(v)
return type.__new__(meta, name, bases, class_dict)
class PluginWrap(object):
"""
A wrapper to implicitly pass the module name to PluginAPI
"""
def __init__(self, api, module):
self.api = api
self.module = module
def __getattribute__(self, name):
api = object.__getattribute__(self, 'api')
module = object.__getattribute__(self, 'module')
return partial(getattr(api, name), module)
class PluginAPI(object):
"""
The public API exposed to the plugins.
Its goal is to limit the use of the raw Core object
as much as possible.
"""
def __init__(self, core, plugin_manager):
self.core = core
self.plugin_manager = plugin_manager
def __getitem__(self, value):
return PluginWrap(self, value)
def send_message(self, _, *args, **kwargs):
"""
Send a message to the current tab.
:param str msg: The message to send.
"""
return self.core.send_message(*args, **kwargs)
def add_timed_event(self, _, *args, **kwargs):
"""
Schedule a timed event.
:param timed_events.TimedEvent event: The timed event to schedule.
"""
return self.core.add_timed_event(*args, **kwargs)
def remove_timed_event(self, _, *args, **kwargs):
"""
Unschedule a timed event.
:param timed_events.TimedEvent event: The event to unschedule.
"""
return self.core.remove_timed_event(*args, **kwargs)
def create_timed_event(self, _, *args, **kwargs):
"""
Create a timed event, but do not schedule it;
:py:func:`~PluginAPI.add_timed_event` must be used for that.
:param datetime.datetime date: The time at which the handler must be executed
:param function callback: The handler that will be executed
:param \*args: Optional arguments passed to the handler.
:return: The created event.
:rtype: :py:class:`timed_events.TimedEvent`
"""
return TimedEvent(*args, **kwargs)
def create_delayed_event(self, _, *args, **kwargs):
"""
Create a delayed event, but do not schedule it;
:py:func:`~PluginAPI.add_timed_event` must be used for that.
A delayed event is a timed event with a delay from the time
this function is called (instead of a datetime).
:param int delay: The number of seconds to schedule the execution
:param function callback: The handler that will be executed
:param \*args: Optional arguments passed to the handler.
:return: The created event.
:rtype: :py:class:`timed_events.DelayedEvent`
"""
return DelayedEvent(*args, **kwargs)
def information(self, _, *args, **kwargs):
"""
Display a new message in the information buffer.
:param str msg: The message to display.
:param str typ: The message type (e.g. Info, Error…)
"""
return self.core.information(*args, **kwargs)
def current_tab(self, _):
"""
Get the current Tab.
:returns: The current tab.
"""
return self.core.current_tab()
def run_command(self, _, *args, **kwargs):
"""
Run a command from the current tab.
(a command starts with a /, if not, it’s a message)
:param str line: The command to run.
"""
return self.core.current_tab().execute_command(*args, **kwargs)
def all_tabs(self, _):
"""
Return a list of all opened tabs
:returns list: The list of tabs.
"""
return self.core.tabs
def add_command(self, module, *args, **kwargs):
"""
Add a global command.
:param str name: The name of the command (/name)
:param function handler: The function called when the command is run.
:param str help: The complete help for that command.
:param str short: A short description of the command.
:param function completion: The completion function for that command
(optional)
:param str usage: A string showing the required and optional args
of the command. Optional args should be surrounded by []
and mandatory args should be surrounded by <>.
Example string: "<server> [port]"
:raises Exception: If the command already exists.
"""
return self.plugin_manager.add_command(module, *args, **kwargs)
def del_command(self, module, *args, **kwargs):
"""
Remove a global command.
:param str name: The name of the command to remove.
That command _must_ have been added by the same plugin
"""
return self.plugin_manager.del_command(module, *args, **kwargs)
def add_key(self, module, *args, **kwargs):
"""
Associate a global binding to a handler.
:param str key: The curses representation of the binding.
:param function handler: The function called when the binding is pressed.
:raise Exception: If the binding is already present.
"""
return self.plugin_manager.add_key(module, *args, **kwargs)
def del_key(self, module, *args, **kwargs):
"""
Remove a global binding.
:param str key: The binding to remove.
"""
return self.plugin_manager.del_key(module, *args, **kwargs)
def add_tab_key(self, module, *args, **kwargs):
"""
Associate a binding to a handler, but only for a certain tab type.
:param Tab tab_type: The type of tab to target.
:param str key: The binding to add.
:param function handler: The function called when the binding is pressed
"""
return self.plugin_manager.add_tab_key(module, *args, **kwargs)
def del_tab_key(self, module, *args, **kwargs):
"""
Remove a binding added with add_tab_key
:param tabs.Tab tab_type: The type of tab to target.
:param str key: The binding to remove.
"""
return self.plugin_manager.del_tab_key(module, *args, **kwargs)
def add_tab_command(self, module, *args, **kwargs):
"""
Add a command to only one type of tab.
:param tabs.Tab tab_type: The type of Tab to target.
:param str name: The name of the command (/name)
:param function handler: The function called when the command is run.
:param str help: The complete help for that command.
:param str short: A short description of the command.
:param function completion: The completion function for that command
(optional)
:param str usage: A string showing the required and optional args
of the command. Optional args should be surrounded by []
and mandatory args should be surrounded by <>.
Example string: "<server> [port]"
:raise Exception: If the command already exists.
"""
return self.plugin_manager.add_tab_command(module, *args, **kwargs)
def del_tab_command(self, module, *args, **kwargs):
"""
Remove a tab-specific command.
:param tabs.Tab tab_type: The type of tab to target.
:param str name: The name of the command to remove.
That command _must_ have been added by the same plugin
"""
return self.plugin_manager.del_tab_command(module, *args, **kwargs)
def add_event_handler(self, module, *args, **kwargs):
"""
Add an event handler for a poezio event.
:param str event_name: The event name.
:param function handler: The handler function.
:param int position: The position of that handler in the handler list.
This is useful for plugins like GPG or OTR, which must be the last
function called on the text.
Defaults to 0.
A complete list of those events can be found at
http://poezio.eu/doc/en/plugins.html#events-list
"""
return self.plugin_manager.add_event_handler(module, *args, **kwargs)
def del_event_handler(self, module, *args, **kwargs):
"""
Remove a handler for a poezio event.
:param str event_name: The name of the targeted event.
:param function handler: The function to remove from the handlers.
"""
return self.plugin_manager.del_event_handler(module, *args, **kwargs)
def add_sleek_event_handler(self, module, event_name, handler):
"""
Add an event handler for a sleekxmpp event.
:param str event_name: The event name.
:param function handler: The handler function.
A list of the SleekXMPP events can be found here
http://sleekxmpp.com/event_index.html
"""
self.core.xmmp.add_event_handler(event_name, handler)
def del_sleek_event_handler(self, module, event_name, handler):
"""
Remove a handler for a SleekXMPP event
:param str event_name: The name of the targeted event.
:param function handler: The function to remove from the handlers.
"""
self.core.xmpp.del_event_handler(event_name, handler)
class BasePlugin(object, metaclass=SafetyMetaclass):
"""
Class that all plugins derive from.
"""
def __init__(self, plugin_api, core, plugins_conf_dir):
self.core = core
# More hack; luckily we'll never have more than one core object
SafetyMetaclass.core = core
conf = os.path.join(plugins_conf_dir, self.__module__+'.cfg')
self.config = PluginConfig(conf, self.__module__)
self._api = plugin_api[self.name]
self.init()
@property
def name(self):
"""
Get the name (module name) of the plugin.
"""
return self.__module__
@property
def api(self):
return self._api
def init(self):
"""
Method called at the creation of the plugin.
Do not overwrite __init__ and use this instead.
"""
pass
def cleanup(self):
"""
Called when the plugin is unloaded.
Overwrite this if you want to erase or save things before the plugin is disabled.
"""
pass
def unload(self):
self.cleanup()
def add_command(self, name, handler, help, completion=None, short='', usage=''):
"""
Add a global command.
You cannot overwrite the existing commands.
"""
return self.api.add_command(name, handler, help,
completion=completion, short=short, usage=usage)
def del_command(self, name):
"""
Remove a global command.
This only works if the command was added by the plugin
"""
return self.api.del_command(name)
def add_key(self, key, handler):
"""
Add a global keybind
"""
return self.api.add_key(key, handler)
def del_key(self, key):
"""
Remove a global keybind
"""
return self.api.del_key(key)
def add_tab_key(self, tab_type, key, handler):
"""
Add a keybind only for a type of tab.
"""
return self.api.add_tab_key(tab_type, key, handler)
def del_tab_key(self, tab_type, key):
"""
Remove a keybind added through add_tab_key.
"""
return self.api.del_tab_key(tab_type, key)
def add_tab_command(self, tab_type, name, handler, help, completion=None, short='', usage=''):
"""
Add a command only for a type of tab.
"""
return self.api.add_tab_command(tab_type, name, handler, help,
completion=completion, short=short, usage=usage)
def del_tab_command(self, tab_type, name):
"""
Delete a command added through add_tab_command.
"""
return self.api.del_tab_command(tab_type, name)
def add_event_handler(self, event_name, handler, position=0):
"""
Add an event handler to the event event_name.
An optional position in the event handler list can be provided.
"""
return self.api.add_event_handler(event_name, handler, position)
def del_event_handler(self, event_name, handler):
"""
Remove 'handler' from the event list for 'event_name'.
"""
return self.api.del_event_handler(event_name, handler)
|