-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
decorators.py
83 lines (66 loc) · 2.41 KB
/
decorators.py
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
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)
"""
Spyder API plugin registration decorators.
"""
# Standard library imports
import functools
from typing import Callable, Optional
import inspect
def on_plugin_available(func: Callable = None,
plugin: Optional[str] = None):
"""
Method decorator used to handle plugin availability on Spyder.
The methods that use this decorator must have the following signature:
`def method(self)` when observing a single plugin or
`def method(self, plugin): ...` when observing multiple plugins or
all plugins that were listed as dependencies.
Parameters
----------
func: Callable
Method to decorate. Given by default when applying the decorator.
plugin: Optional[str]
Name of the requested plugin whose availability triggers the method.
Returns
-------
func: Callable
The same method that was given as input.
"""
if func is None:
return functools.partial(on_plugin_available, plugin=plugin)
if plugin is None:
# Use special __all identifier to signal that the function
# observes all plugins listed as dependencies.
plugin = '__all'
func._plugin_listen = plugin
return func
def on_plugin_teardown(func: Callable = None,
plugin: Optional[str] = None):
"""
Method decorator used to handle plugin teardown on Spyder.
This decorator will be called **before** the specified plugin is deleted
and also **before** the plugin that uses the decorator is destroyed.
The methods that use this decorator must have the following signature:
`def method(self)`.
Parameters
----------
func: Callable
Method to decorate. Given by default when applying the decorator.
plugin: str
Name of the requested plugin whose teardown triggers the method.
Returns
-------
func: Callable
The same method that was given as input.
"""
if func is None:
return functools.partial(on_plugin_teardown, plugin=plugin)
if plugin is None:
raise ValueError('on_plugin_teardown must have a well defined '
'plugin keyword argument value, '
'e.g., plugin=Plugins.Editor')
func._plugin_teardown = plugin
return func