12
12
import traceback
13
13
14
14
import babel
15
- import pkg_resources
15
+ import entrypoints
16
16
from packaging .version import parse as parse_version
17
17
18
18
# Entry points
@@ -45,7 +45,7 @@ def _get_installed_language_pack_locales():
45
45
"""
46
46
data = {}
47
47
messages = []
48
- for entry_point in pkg_resources . iter_entry_points (JUPYTERLAB_LANGUAGEPACK_ENTRY ):
48
+ for entry_point in entrypoints . get_group_all (JUPYTERLAB_LANGUAGEPACK_ENTRY ):
49
49
try :
50
50
data [entry_point .name ] = os .path .dirname (entry_point .load ().__file__ )
51
51
except Exception :
@@ -74,7 +74,7 @@ def _get_installed_package_locales():
74
74
"""
75
75
data = {}
76
76
messages = []
77
- for entry_point in pkg_resources . iter_entry_points (JUPYTERLAB_LOCALE_ENTRY ):
77
+ for entry_point in entrypoints . get_group_all (JUPYTERLAB_LOCALE_ENTRY ):
78
78
try :
79
79
data [entry_point .name ] = os .path .dirname (entry_point .load ().__file__ )
80
80
except Exception :
@@ -107,31 +107,6 @@ def _main():
107
107
108
108
# --- Helpers
109
109
# ----------------------------------------------------------------------------
110
- def run_process_and_parse (cmd : list ):
111
- """
112
- Run a list of commands and return the result parsed form stdout.
113
-
114
- Parameters
115
- ----------
116
- cmd: list
117
- List of commands
118
-
119
- Returns
120
- -------
121
- tuple
122
- A tuple in the form `(result_dict, message)`.
123
- """
124
- result = {"data" : {}, "message" : "" }
125
- try :
126
- p = subprocess .Popen (cmd , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
127
- stdout , stderr = p .communicate ()
128
- result = json .loads (stdout .decode ('utf-8' ))
129
- except Exception :
130
- result ["message" ] = traceback .format_exc () + "\n " + repr (stderr .decode ('utf-8' ))
131
-
132
- return result ["data" ], result ["message" ]
133
-
134
-
135
110
def is_valid_locale (locale : str ) -> bool :
136
111
"""
137
112
Check if a `locale` value is valid.
@@ -168,7 +143,7 @@ def is_valid_locale(locale: str) -> bool:
168
143
return valid
169
144
170
145
171
- def get_display_name (locale : str , display_locale : str = DEFAULT_LOCALE ) -> str :
146
+ def get_display_name (locale : str , display_locale : str = DEFAULT_LOCALE ) -> str :
172
147
"""
173
148
Return the language name to use with a `display_locale` for a given language locale.
174
149
@@ -185,7 +160,9 @@ def get_display_name(locale: str, display_locale: str=DEFAULT_LOCALE) -> str:
185
160
Localized `locale` and capitalized language name using `display_locale` as language.
186
161
"""
187
162
locale = locale if is_valid_locale (locale ) else DEFAULT_LOCALE
188
- display_locale = display_locale if is_valid_locale (display_locale ) else DEFAULT_LOCALE
163
+ display_locale = (
164
+ display_locale if is_valid_locale (display_locale ) else DEFAULT_LOCALE
165
+ )
189
166
loc = babel .Locale .parse (locale )
190
167
return loc .get_display_name (display_locale ).capitalize ()
191
168
@@ -242,8 +219,7 @@ def get_installed_packages_locale(locale: str) -> dict:
242
219
- `entry_points={"jupyterlab.locale": "package-name = package_module"}`
243
220
- `entry_points={"jupyterlab.locale": "jupyterlab-git = jupyterlab_git"}`
244
221
"""
245
- cmd = [sys .executable , __file__ , "_get_installed_package_locales" ]
246
- found_package_locales , message = run_process_and_parse (cmd )
222
+ found_package_locales , message = _get_installed_package_locales ()
247
223
packages_locale_data = {}
248
224
messages = message .split ("\n " )
249
225
if not message :
@@ -269,7 +245,7 @@ def get_installed_packages_locale(locale: str) -> dict:
269
245
)
270
246
if os .path .isfile (locale_json_path ):
271
247
try :
272
- with open (locale_json_path , "r" , encoding = ' utf-8' ) as fh :
248
+ with open (locale_json_path , "r" , encoding = " utf-8" ) as fh :
273
249
packages_locale_data [package_name ] = json .load (fh )
274
250
except Exception :
275
251
messages .append (traceback .format_exc ())
@@ -279,7 +255,7 @@ def get_installed_packages_locale(locale: str) -> dict:
279
255
280
256
# --- API
281
257
# ----------------------------------------------------------------------------
282
- def get_language_packs (display_locale : str = DEFAULT_LOCALE ) -> tuple :
258
+ def get_language_packs (display_locale : str = DEFAULT_LOCALE ) -> tuple :
283
259
"""
284
260
Return the available language packs installed in the system.
285
261
@@ -295,15 +271,8 @@ def get_language_packs(display_locale: str=DEFAULT_LOCALE) -> tuple:
295
271
-------
296
272
tuple
297
273
A tuple in the form `(locale_data_dict, message)`.
298
-
299
- Notes
300
- -----
301
- We call `_get_installed_language_pack_locales` via a subprocess to
302
- guarantee the results represent the most up-to-date entry point
303
- information, which seems to be defined on interpreter startup.
304
274
"""
305
- cmd = [sys .executable , __file__ , "_get_installed_language_pack_locales" ]
306
- found_locales , message = run_process_and_parse (cmd )
275
+ found_locales , message = _get_installed_language_pack_locales ()
307
276
locales = {}
308
277
messages = message .split ("\n " )
309
278
if not message :
@@ -316,7 +285,9 @@ def get_language_packs(display_locale: str=DEFAULT_LOCALE) -> tuple:
316
285
else :
317
286
invalid_locales .append (locale )
318
287
319
- display_locale = display_locale if display_locale in valid_locales else DEFAULT_LOCALE
288
+ display_locale = (
289
+ display_locale if display_locale in valid_locales else DEFAULT_LOCALE
290
+ )
320
291
locales = {
321
292
DEFAULT_LOCALE : {
322
293
"displayName" : get_display_name (DEFAULT_LOCALE , display_locale ),
@@ -330,7 +301,9 @@ def get_language_packs(display_locale: str=DEFAULT_LOCALE) -> tuple:
330
301
}
331
302
332
303
if invalid_locales :
333
- messages .append ("The following locales are invalid: {0}!" .format (invalid_locales ))
304
+ messages .append (
305
+ "The following locales are invalid: {0}!" .format (invalid_locales )
306
+ )
334
307
335
308
return locales , "\n " .join (messages )
336
309
@@ -351,8 +324,7 @@ def get_language_pack(locale: str) -> tuple:
351
324
guarantee the results represent the most up-to-date entry point
352
325
information, which seems to be defined on interpreter startup.
353
326
"""
354
- cmd = [sys .executable , __file__ , "_get_installed_language_pack_locales" ]
355
- found_locales , message = run_process_and_parse (cmd )
327
+ found_locales , message = _get_installed_language_pack_locales ()
356
328
found_packages_locales , message = get_installed_packages_locale (locale )
357
329
locale_data = {}
358
330
messages = message .split ("\n " )
@@ -365,7 +337,7 @@ def get_language_pack(locale: str) -> tuple:
365
337
pkg_name = name .replace (".json" , "" )
366
338
json_path = os .path .join (root , name )
367
339
try :
368
- with open (json_path , "r" , encoding = ' utf-8' ) as fh :
340
+ with open (json_path , "r" , encoding = " utf-8" ) as fh :
369
341
merged_data = json .load (fh )
370
342
except Exception :
371
343
messages .append (traceback .format_exc ())
0 commit comments