|
1 |
| -import pathlib |
2 | 1 | import importlib
|
3 | 2 |
|
4 |
| -from jupyter_core.paths import jupyter_config_path |
5 |
| -from traitlets.config.loader import ( |
6 |
| - JSONFileConfigLoader |
7 |
| -) |
8 |
| - |
9 |
| - |
10 |
| -def configd_path( |
11 |
| - config_dir=None, |
12 |
| - configd_prefix="jupyter_server", |
13 |
| -): |
14 |
| - # Build directory name for `config.d` directory. |
15 |
| - configd = "_".join([configd_prefix, "config.d"]) |
16 |
| - if not config_dir: |
17 |
| - config_dir = jupyter_config_path() |
18 |
| - # Leverage pathlib for path management. |
19 |
| - return [pathlib.Path(path).joinpath(configd) for path in config_dir] |
20 |
| - |
21 |
| - |
22 |
| -def configd_files( |
23 |
| - config_dir=None, |
24 |
| - configd_prefix="jupyter_server", |
25 |
| -): |
26 |
| - """Lists (only) JSON files found in a Jupyter config.d folder. |
27 |
| - """ |
28 |
| - paths = configd_path( |
29 |
| - config_dir=config_dir, |
30 |
| - configd_prefix=configd_prefix |
31 |
| - ) |
32 |
| - files = [] |
33 |
| - for path in paths: |
34 |
| - json_files = path.glob("*.json") |
35 |
| - files.extend(json_files) |
36 |
| - return files |
37 |
| - |
38 |
| - |
39 |
| -def enabled(name, server_config): |
40 |
| - """Given a server config object, return True if the extension |
41 |
| - is explicitly enabled in the config. |
42 |
| - """ |
43 |
| - enabled = ( |
44 |
| - server_config |
45 |
| - .get("ServerApp", {}) |
46 |
| - .get("jpserver_extensions", {}) |
47 |
| - .get(name, False) |
48 |
| - ) |
49 |
| - return enabled |
50 |
| - |
51 |
| - |
52 |
| -def find_extension_in_configd( |
53 |
| - name, |
54 |
| - config_dir=None, |
55 |
| - configd_prefix="jupyter_server", |
56 |
| -): |
57 |
| - """Search through all config.d files and return the |
58 |
| - JSON Path for this named extension. If the extension |
59 |
| - is not found, return None |
60 |
| - """ |
61 |
| - files = configd_files( |
62 |
| - config_dir=config_dir, |
63 |
| - configd_prefix=configd_prefix, |
64 |
| - ) |
65 |
| - for f in files: |
66 |
| - if name == f.stem: |
67 |
| - return f |
68 |
| - |
69 |
| - |
70 |
| -def configd_enabled( |
71 |
| - name, |
72 |
| - config_dir=None, |
73 |
| - configd_prefix="jupyter_server", |
74 |
| -): |
75 |
| - """Check if the named extension is enabled somewhere in |
76 |
| - a config.d folder. |
77 |
| - """ |
78 |
| - config_file = find_extension_in_configd( |
79 |
| - name, |
80 |
| - config_dir=config_dir, |
81 |
| - configd_prefix=configd_prefix, |
82 |
| - ) |
83 |
| - if config_file: |
84 |
| - c = JSONFileConfigLoader( |
85 |
| - filename=str(config_file.name), |
86 |
| - path=str(config_file.parent) |
87 |
| - ) |
88 |
| - config = c.load_config() |
89 |
| - return enabled(name, config) |
90 |
| - else: |
91 |
| - return False |
92 |
| - |
93 |
| - |
94 |
| -def list_extensions_in_configd( |
95 |
| - configd_prefix="jupyter_server", |
96 |
| - config_paths=None |
97 |
| -): |
98 |
| - """Get a dictionary of all jpserver_extensions found in the |
99 |
| - config directories list. |
100 |
| -
|
101 |
| - Parameters |
102 |
| - ---------- |
103 |
| - config_paths : list |
104 |
| - List of config directories to search for the |
105 |
| - `jupyter_server_config.d` directory. |
106 |
| - """ |
107 |
| - |
108 |
| - # Build directory name for `config.d` directory. |
109 |
| - configd = "_".join([configd_prefix, "config.d"]) |
110 |
| - |
111 |
| - if not config_paths: |
112 |
| - config_paths = jupyter_config_path() |
113 |
| - |
114 |
| - # Leverage pathlib for path management. |
115 |
| - config_paths = [pathlib.Path(p) for p in config_paths] |
116 |
| - |
117 |
| - extensions = [] |
118 |
| - for path in config_paths: |
119 |
| - json_files = path.joinpath(configd).glob("*.json") |
120 |
| - for file in json_files: |
121 |
| - # The extension name is the file name (minus file suffix) |
122 |
| - extension_name = file.stem |
123 |
| - extensions.append(extension_name) |
124 |
| - |
125 |
| - return extensions |
126 |
| - |
127 | 3 |
|
128 | 4 | class ExtensionLoadingError(Exception):
|
129 | 5 | pass
|
|
0 commit comments