/
mixins.py
352 lines (303 loc) · 12.3 KB
/
mixins.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
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
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)
"""
Spyder API helper mixins.
"""
# Standard library imports
import logging
from typing import Any, Union, Optional
import warnings
# Third-party imports
from qtpy.QtWidgets import QAction, QWidget
# Local imports
from spyder.config.gui import Shortcut
from spyder.config.manager import CONF
from spyder.config.types import ConfigurationKey
from spyder.config.user import NoDefault
logger = logging.getLogger(__name__)
BasicTypes = Union[bool, int, str, tuple, list, dict]
class SpyderConfigurationAccessor:
"""
Mixin used to access options stored in the Spyder configuration system.
"""
# Name of the configuration section that's going to be
# used to record the object's permanent data in Spyder
# config system.
CONF_SECTION = None
def get_conf(self,
option: ConfigurationKey,
default: Union[NoDefault, BasicTypes] = NoDefault,
section: Optional[str] = None):
"""
Get an option from the Spyder configuration system.
Parameters
----------
option: ConfigurationKey
Name/Tuple path of the option to get its value from.
default: Union[NoDefault, BasicTypes]
Fallback value to return if the option is not found on the
configuration system.
section: str
Section in the configuration system, e.g. `shortcuts`. If None,
then the value of `CONF_SECTION` is used.
Returns
-------
value: BasicTypes
Value of the option in the configuration section.
Raises
------
configparser.NoOptionError
If the section does not exist in the configuration.
"""
section = self.CONF_SECTION if section is None else section
if section is None:
raise AttributeError(
'A SpyderConfigurationAccessor must define a `CONF_SECTION` '
'class attribute!'
)
return CONF.get(section, option, default)
def get_conf_options(self, section: Optional[str] = None):
"""
Get all options from the given section.
Parameters
----------
section: Optional[str]
Section in the configuration system, e.g. `shortcuts`. If None,
then the value of `CONF_SECTION` is used.
Returns
-------
values: BasicTypes
Values of the option in the configuration section.
Raises
------
configparser.NoOptionError
If the section does not exist in the configuration.
"""
section = self.CONF_SECTION if section is None else section
if section is None:
raise AttributeError(
'A SpyderConfigurationAccessor must define a `CONF_SECTION` '
'class attribute!'
)
return CONF.options(section)
def set_conf(self,
option: ConfigurationKey,
value: BasicTypes,
section: Optional[str] = None,
recursive_notification: bool = True):
"""
Set an option in the Spyder configuration system.
Parameters
----------
option: ConfigurationKey
Name/Tuple path of the option to set its value.
value: BasicTypes
Value to set on the configuration system.
section: Optional[str]
Section in the configuration system, e.g. `shortcuts`. If None,
then the value of `CONF_SECTION` is used.
recursive_notification: bool
If True, all objects that observe all changes on the
configuration section and objects that observe partial tuple paths
are notified. For example if the option `opt` of section `sec`
changes, then the observers for section `sec` are notified.
Likewise, if the option `(a, b, c)` changes, then observers for
`(a, b, c)`, `(a, b)` and a are notified as well.
"""
section = self.CONF_SECTION if section is None else section
if section is None:
raise AttributeError(
'A SpyderConfigurationAccessor must define a `CONF_SECTION` '
'class attribute!'
)
CONF.set(
section,
option,
value,
recursive_notification=recursive_notification
)
def remove_conf(self,
option: ConfigurationKey,
section: Optional[str] = None):
"""
Remove an option in the Spyder configuration system.
Parameters
----------
option: ConfigurationKey
Name/Tuple path of the option to remove its value.
section: Optional[str]
Section in the configuration system, e.g. `shortcuts`. If None,
then the value of `CONF_SECTION` is used.
"""
section = self.CONF_SECTION if section is None else section
if section is None:
raise AttributeError(
'A SpyderConfigurationAccessor must define a `CONF_SECTION` '
'class attribute!'
)
CONF.remove_option(section, option)
def get_conf_default(self,
option: ConfigurationKey,
section: Optional[str] = None):
"""
Get an option default value in the Spyder configuration system.
Parameters
----------
option: ConfigurationKey
Name/Tuple path of the option to remove its value.
section: Optional[str]
Section in the configuration system, e.g. `shortcuts`. If None,
then the value of `CONF_SECTION` is used.
"""
section = self.CONF_SECTION if section is None else section
if section is None:
raise AttributeError(
'A SpyderConfigurationAccessor must define a `CONF_SECTION` '
'class attribute!'
)
return CONF.get_default(section, option)
def get_shortcut(
self, name: str, context: Optional[str] = None,
plugin_name: Optional[str] = None) -> str:
"""
Get a shortcut sequence stored under the given name and context.
Parameters
----------
name: str
Key identifier under which the shortcut is stored.
context: Optional[str]
Name of the shortcut context.
plugin: Optional[str]
Name of the plugin where the shortcut is defined.
Returns
-------
shortcut: str
Key sequence of the shortcut.
Raises
------
configparser.NoOptionError
If the section does not exist in the configuration.
"""
context = self.CONF_SECTION if context is None else context
return CONF.get_shortcut(context, name, plugin_name)
def config_shortcut(
self, action: QAction, name: str, parent: QWidget,
context: Optional[str] = None) -> Shortcut:
"""
Create a Shortcut namedtuple for a widget.
The data contained in this tuple will be registered in our shortcuts
preferences page.
Parameters
----------
action: QAction
Action that will use the shortcut.
name: str
Key identifier under which the shortcut is stored.
parent: QWidget
Parent widget for the shortcut.
context: Optional[str]
Name of the context (plugin) where the shortcut was defined.
Returns
-------
shortcut: Shortcut
Namedtuple with the information of the shortcut as used for the
shortcuts preferences page.
"""
shortcut_context = self.CONF_SECTION if context is None else context
return CONF.config_shortcut(
action,
shortcut_context,
name,
parent
)
@property
def old_conf_version(self):
"""Get old Spyder configuration version."""
return CONF.old_spyder_version
class SpyderConfigurationObserver(SpyderConfigurationAccessor):
"""
Concrete implementation of the protocol
:class:`spyder.config.types.ConfigurationObserver`.
This mixin enables a class to receive configuration updates seamlessly,
by registering methods using the
:function:`spyder.api.config.decorators.on_conf_change` decorator, which
receives a configuration section and option to observe.
When a change occurs on any of the registered configuration options,
the corresponding registered method is called with the new value.
"""
def __init__(self):
super().__init__()
if self.CONF_SECTION is None:
warnings.warn(
'A SpyderConfigurationObserver must define a `CONF_SECTION` '
f'class attribute! Hint: {self} or its parent should define '
'the section.'
)
self._configuration_listeners = {}
self._multi_option_listeners = set({})
self._gather_observers()
self._merge_none_observers()
# Register class to listen for changes in all registered options
for section in self._configuration_listeners:
section = self.CONF_SECTION if section is None else section
observed_options = self._configuration_listeners[section]
for option in observed_options:
logger.debug(f'{self} is observing {option} '
f'in section {section}')
CONF.observe_configuration(self, section, option)
def __del__(self):
# Remove object from the configuration observer
CONF.unobserve_configuration(self)
def _gather_observers(self):
"""Gather all the methods decorated with `on_conf_change`."""
for method_name in dir(self):
method = getattr(self, method_name, None)
if hasattr(method, '_conf_listen'):
info = method._conf_listen
if len(info) > 1:
self._multi_option_listeners |= {method_name}
for section, option in info:
section_listeners = self._configuration_listeners.get(
section, {})
option_listeners = section_listeners.get(option, [])
option_listeners.append(method_name)
section_listeners[option] = option_listeners
self._configuration_listeners[section] = section_listeners
def _merge_none_observers(self):
"""Replace observers that declared section as None by CONF_SECTION."""
default_selectors = self._configuration_listeners.get(None, {})
section_selectors = self._configuration_listeners.get(
self.CONF_SECTION, {})
for option in default_selectors:
default_option_receivers = default_selectors.get(option, [])
section_option_receivers = section_selectors.get(option, [])
merged_receivers = (
default_option_receivers + section_option_receivers)
section_selectors[option] = merged_receivers
self._configuration_listeners[self.CONF_SECTION] = section_selectors
self._configuration_listeners.pop(None, None)
def on_configuration_change(self, option: ConfigurationKey, section: str,
value: Any):
"""
Handle configuration updates for the option `option` on the section
`section`, whose new value corresponds to `value`.
Parameters
----------
option: ConfigurationKey
Configuration option that did change.
section: str
Name of the section where `option` is contained.
value: Any
New value of the configuration option that produced the event.
"""
section_receivers = self._configuration_listeners.get(section, {})
option_receivers = section_receivers.get(option, [])
for receiver in option_receivers:
method = getattr(self, receiver)
if receiver in self._multi_option_listeners:
method(option, value)
else:
method(value)