-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathshim.py
303 lines (263 loc) · 11.5 KB
/
shim.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
from functools import wraps
from copy import deepcopy
from traitlets import TraitError
from traitlets.config.loader import (
Config,
)
from jupyter_core.application import JupyterApp
from jupyter_server.serverapp import ServerApp
from jupyter_server.extension.application import ExtensionApp
from .traits import NotebookAppTraits
def NBAPP_AND_SVAPP_SHIM_MSG(trait_name): return (
"'{trait_name}' was found in both NotebookApp "
"and ServerApp. This is likely a recent change. "
"This config will only be set in NotebookApp. "
"Please check if you should also config these traits in "
"ServerApp for your purpose.".format(
trait_name=trait_name,
)
)
def NBAPP_TO_SVAPP_SHIM_MSG(trait_name): return (
"'{trait_name}' has moved from NotebookApp to "
"ServerApp. This config will be passed to ServerApp. "
"Be sure to update your config before "
"our next release.".format(
trait_name=trait_name,
)
)
def EXTAPP_AND_NBAPP_AND_SVAPP_SHIM_MSG(trait_name, extapp_name): return (
"'{trait_name}' is found in {extapp_name}, NotebookApp, "
"and ServerApp. This is a recent change. "
"This config will only be set in {extapp_name}. "
"Please check if you should also config these traits in "
"NotebookApp and ServerApp for your purpose.".format(
trait_name=trait_name,
extapp_name=extapp_name
)
)
def EXTAPP_AND_SVAPP_SHIM_MSG(trait_name, extapp_name): return (
"'{trait_name}' is found in both {extapp_name} "
"and ServerApp. This is a recent change. "
"This config will only be set in {extapp_name}. "
"Please check if you should also config these traits in "
"ServerApp for your purpose.".format(
trait_name=trait_name,
extapp_name=extapp_name
)
)
def EXTAPP_AND_NBAPP_SHIM_MSG(trait_name, extapp_name): return (
"'{trait_name}' is found in both {extapp_name} "
"and NotebookApp. This is a recent change. "
"This config will only be set in {extapp_name}. "
"Please check if you should also config these traits in "
"NotebookApp for your purpose.".format(
trait_name=trait_name,
extapp_name=extapp_name
)
)
def NOT_EXTAPP_NBAPP_AND_SVAPP_SHIM_MSG(trait_name, extapp_name): return (
"'{trait_name}' is not found in {extapp_name}, but "
"it was found in both NotebookApp "
"and ServerApp. This is likely a recent change. "
"This config will only be set in ServerApp. "
"Please check if you should also config these traits in "
"NotebookApp for your purpose.".format(
trait_name=trait_name,
extapp_name=extapp_name
)
)
def EXTAPP_TO_SVAPP_SHIM_MSG(trait_name, extapp_name): return (
"'{trait_name}' has moved from {extapp_name} to "
"ServerApp. Be sure to update your config before "
"our next release.".format(
trait_name=trait_name,
extapp_name=extapp_name
)
)
def EXTAPP_TO_NBAPP_SHIM_MSG(trait_name, extapp_name): return (
"'{trait_name}' has moved from {extapp_name} to "
"NotebookApp. Be sure to update your config before "
"our next release.".format(
trait_name=trait_name,
extapp_name=extapp_name
)
)
# A tuple of traits that shouldn't be shimmed or throw any
# warnings of any kind.
IGNORED_TRAITS = ("open_browser", "log_level", "log_format", "default_url", "show_banner")
class NotebookConfigShimMixin:
"""A Mixin class for shimming configuration from
NotebookApp to ServerApp. This class handles warnings, errors,
etc.
This class should be used during a transition period for apps
that are switching from depending on NotebookApp to ServerApp.
After one release cycle, this class can be safely removed
from the inheriting class.
TL;DR
The entry point to shimming is at the `update_config` method.
Once traits are loaded, before updating config across all
configurable objects, this class injects a method to reroute
traits to their *most logical* classes.
This class raises warnings when:
1. a trait has moved.
2. a trait is redundant across classes.
Redundant traits across multiple classes now must be
configured separately, *or* removed from their old
location to avoid this warning.
For a longer description on how individual traits are handled,
read the docstring under `shim_config_from_notebook_to_jupyter_server`.
"""
@wraps(JupyterApp.update_config)
def update_config(self, config):
# Shim traits to handle transition from NotebookApp to ServerApp
shimmed_config = self.shim_config_from_notebook_to_jupyter_server(
config)
super().update_config(shimmed_config)
def shim_config_from_notebook_to_jupyter_server(self, config):
"""Reorganizes a config object to reroute traits to their expected destinations
after the transition from NotebookApp to ServerApp.
A detailed explanation of how traits are handled:
1. If the argument is prefixed with `ServerApp`,
pass this trait to `ServerApp`.
2. If the argument is prefixed with `NotebookApp`,
* If the argument is a trait of `NotebookApp` *and* `ServerApp`:
1. Raise a warning—**for the extension developers**—that
there's redundant traits.
2. Pass trait to `NotebookApp`.
* If the argument is a trait of just `ServerApp` only
(i.e. the trait moved from `NotebookApp` to `ServerApp`):
1. Raise a "this trait has moved" **for the user**.
3. Pass trait to `ServerApp`.
* If the argument is a trait of `NotebookApp` only, pass trait
to `NotebookApp`.
* If the argument is not found in any object, raise a
`"Trait not found."` error.
3. If the argument is prefixed with `ExtensionApp`:
* If the argument is a trait of `ExtensionApp`,
`NotebookApp`, and `ServerApp`,
1. Raise a warning about redundancy.
2. Pass to the ExtensionApp
* If the argument is a trait of `ExtensionApp` and `NotebookApp`,
1. Raise a warning about redundancy.
2. Pass to ExtensionApp.
* If the argument is a trait of `ExtensionApp` and `ServerApp`,
1. Raise a warning about redundancy.
2. Pass to ExtensionApp.
* If the argument is a trait of `ExtensionApp`.
1. Pass to ExtensionApp.
* If the argument is a trait of `NotebookApp` but not `ExtensionApp`,
1. Raise a warning that trait has likely moved to NotebookApp.
2. Pass to NotebookApp
* If the arguent is a trait of `ServerApp` but not `ExtensionApp`,
1. Raise a warning that the trait has likely moved to ServerApp.
2. Pass to ServerApp.
* else
* Raise a TraitError: "trait not found."
"""
extapp_name = self.__class__.__name__
# Pop out the various configurable objects that we need to evaluate.
nbapp_config = config.pop('NotebookApp', {})
svapp_config = config.pop('ServerApp', {})
extapp_config = config.pop(extapp_name, {})
# Created shimmed configs.
# Leave the rest of the config alone.
config_shim = deepcopy(config)
svapp_config_shim = {}
nbapp_config_shim = {}
extapp_config_shim = {}
extapp_traits = (
self.__class__.class_trait_names() +
ExtensionApp.class_trait_names()
)
svapp_traits = ServerApp.class_trait_names()
nbapp_traits = (
NotebookAppTraits.class_trait_names() +
ExtensionApp.class_trait_names()
)
# 1. Handle ServerApp traits.
svapp_config_shim.update(svapp_config)
# 2. Handle NotebookApp traits.
warning_msg = None
for trait_name, trait_value in nbapp_config.items():
in_svapp = trait_name in svapp_traits
in_nbapp = trait_name in nbapp_traits
if trait_name in IGNORED_TRAITS:
# Pass trait through without any warning message.
nbapp_config_shim.update({trait_name: trait_value})
elif in_svapp and in_nbapp:
warning_msg = NBAPP_AND_SVAPP_SHIM_MSG(trait_name)
nbapp_config_shim.update({trait_name: trait_value})
elif in_svapp:
warning_msg = NBAPP_TO_SVAPP_SHIM_MSG(trait_name)
svapp_config_shim.update({trait_name: trait_value})
elif in_nbapp:
nbapp_config_shim.update({trait_name: trait_value})
else:
raise TraitError("Trait, {}, not found.".format(trait_name))
# Raise a warning if it's given.
if warning_msg:
self.log.warning(warning_msg)
# 3. Handle ExtensionApp traits.
warning_msg = None
for trait_name, trait_value in extapp_config.items():
in_extapp = trait_name in extapp_traits
in_svapp = trait_name in svapp_traits
in_nbapp = trait_name in nbapp_traits
if trait_name in IGNORED_TRAITS:
# Pass trait through without any warning message.
extapp_config_shim.update({trait_name: trait_value})
elif all([in_extapp, in_svapp, in_nbapp]):
warning_msg = EXTAPP_AND_NBAPP_AND_SVAPP_SHIM_MSG(
trait_name,
extapp_name
)
extapp_config_shim.update({trait_name: trait_value})
elif in_extapp and in_svapp:
warning_msg = EXTAPP_AND_SVAPP_SHIM_MSG(
trait_name,
extapp_name
)
extapp_config_shim.update({trait_name: trait_value})
elif in_extapp and in_nbapp:
warning_msg = EXTAPP_AND_NBAPP_SHIM_MSG(
trait_name,
extapp_name
)
extapp_config_shim.update({trait_name: trait_value})
elif in_extapp:
extapp_config_shim.update({trait_name: trait_value})
elif in_svapp and in_nbapp:
warning_msg = NOT_EXTAPP_NBAPP_AND_SVAPP_SHIM_MSG(
trait_name,
extapp_name
)
svapp_config_shim.update({trait_name: trait_value})
elif in_svapp:
warning_msg = EXTAPP_TO_SVAPP_SHIM_MSG(
trait_name,
extapp_name
)
svapp_config_shim.update({trait_name: trait_value})
elif in_nbapp:
warning_msg = EXTAPP_TO_NBAPP_SHIM_MSG(
trait_name,
extapp_name
)
nbapp_config_shim.update({trait_name: trait_value})
else:
raise TraitError("Trait, {}, not found.".format(trait_name))
# Raise warning if one is given
if warning_msg:
self.log.warning(warning_msg)
# Build config for shimmed traits.
new_config = Config({
'NotebookApp': nbapp_config_shim,
'ServerApp': svapp_config_shim,
})
if extapp_config_shim:
new_config.update(Config({
self.__class__.__name__: extapp_config_shim
}))
# Update the full config with new values
config_shim.update(new_config)
return config_shim