Skip to content

Commit 919186b

Browse files
authored
Switch to entrypoints package (#187)
* Switch to entrypoints package * Fix tests * Add entrypoints as dependency * Remove white space
1 parent a67e80c commit 919186b

File tree

4 files changed

+25
-68
lines changed

4 files changed

+25
-68
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ docs/source/changelog.md
2323

2424
# generated cli docs
2525
docs/source/api/app-config.rst
26+
27+
.vscode/

jupyterlab_server/tests/test_translation_api.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
get_installed_packages_locale,
1515
get_language_pack, get_language_packs,
1616
is_valid_locale, merge_locale_data,
17-
run_process_and_parse, translator)
17+
translator)
1818

1919
from .utils import maybe_patch_ioloop
2020
from .utils import validate_request
@@ -130,31 +130,13 @@ async def test_backend_locale_extension(jp_fetch):
130130

131131
# --- Utils testing
132132
# ------------------------------------------------------------------------
133-
def test_get_installed_language_pack_locales_fails():
134-
# This should not be able to find entry points, since it needs to be
135-
# ran in a subprocess
136-
data, message = _get_installed_language_pack_locales()
137-
assert "es_CO" not in data
138-
assert message == ""
139-
140133
def test_get_installed_language_pack_locales_passes():
141-
utils_file = os.path.join(os.path.dirname(HERE), "translation_utils.py")
142-
cmd = [sys.executable, utils_file, "_get_installed_language_pack_locales"]
143-
data, message = run_process_and_parse(cmd)
134+
data, message = _get_installed_language_pack_locales()
144135
assert "es_CO" in data
145136
assert message == ""
146137

147-
def test_get_installed_package_locales_fails():
148-
# This should not be able to find entry points, since it needs to be
149-
# ran in a subprocess
150-
data, message = _get_installed_package_locales()
151-
assert "jupyterlab_some_package" not in data
152-
assert message == ""
153-
154138
def test_get_installed_package_locales():
155-
utils_file = os.path.join(os.path.dirname(HERE), "translation_utils.py")
156-
cmd = [sys.executable, utils_file, "_get_installed_package_locales"]
157-
data, message = run_process_and_parse(cmd)
139+
data, message = _get_installed_package_locales()
158140
assert "jupyterlab_some_package" in data
159141
assert os.path.isdir(data["jupyterlab_some_package"])
160142
assert message == ""

jupyterlab_server/translation_utils.py

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import traceback
1313

1414
import babel
15-
import pkg_resources
15+
import entrypoints
1616
from packaging.version import parse as parse_version
1717

1818
# Entry points
@@ -45,7 +45,7 @@ def _get_installed_language_pack_locales():
4545
"""
4646
data = {}
4747
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):
4949
try:
5050
data[entry_point.name] = os.path.dirname(entry_point.load().__file__)
5151
except Exception:
@@ -74,7 +74,7 @@ def _get_installed_package_locales():
7474
"""
7575
data = {}
7676
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):
7878
try:
7979
data[entry_point.name] = os.path.dirname(entry_point.load().__file__)
8080
except Exception:
@@ -107,31 +107,6 @@ def _main():
107107

108108
# --- Helpers
109109
# ----------------------------------------------------------------------------
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-
135110
def is_valid_locale(locale: str) -> bool:
136111
"""
137112
Check if a `locale` value is valid.
@@ -168,7 +143,7 @@ def is_valid_locale(locale: str) -> bool:
168143
return valid
169144

170145

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:
172147
"""
173148
Return the language name to use with a `display_locale` for a given language locale.
174149
@@ -185,7 +160,9 @@ def get_display_name(locale: str, display_locale: str=DEFAULT_LOCALE) -> str:
185160
Localized `locale` and capitalized language name using `display_locale` as language.
186161
"""
187162
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+
)
189166
loc = babel.Locale.parse(locale)
190167
return loc.get_display_name(display_locale).capitalize()
191168

@@ -242,8 +219,7 @@ def get_installed_packages_locale(locale: str) -> dict:
242219
- `entry_points={"jupyterlab.locale": "package-name = package_module"}`
243220
- `entry_points={"jupyterlab.locale": "jupyterlab-git = jupyterlab_git"}`
244221
"""
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()
247223
packages_locale_data = {}
248224
messages = message.split("\n")
249225
if not message:
@@ -269,7 +245,7 @@ def get_installed_packages_locale(locale: str) -> dict:
269245
)
270246
if os.path.isfile(locale_json_path):
271247
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:
273249
packages_locale_data[package_name] = json.load(fh)
274250
except Exception:
275251
messages.append(traceback.format_exc())
@@ -279,7 +255,7 @@ def get_installed_packages_locale(locale: str) -> dict:
279255

280256
# --- API
281257
# ----------------------------------------------------------------------------
282-
def get_language_packs(display_locale: str=DEFAULT_LOCALE) -> tuple:
258+
def get_language_packs(display_locale: str = DEFAULT_LOCALE) -> tuple:
283259
"""
284260
Return the available language packs installed in the system.
285261
@@ -295,15 +271,8 @@ def get_language_packs(display_locale: str=DEFAULT_LOCALE) -> tuple:
295271
-------
296272
tuple
297273
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.
304274
"""
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()
307276
locales = {}
308277
messages = message.split("\n")
309278
if not message:
@@ -316,7 +285,9 @@ def get_language_packs(display_locale: str=DEFAULT_LOCALE) -> tuple:
316285
else:
317286
invalid_locales.append(locale)
318287

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+
)
320291
locales = {
321292
DEFAULT_LOCALE: {
322293
"displayName": get_display_name(DEFAULT_LOCALE, display_locale),
@@ -330,7 +301,9 @@ def get_language_packs(display_locale: str=DEFAULT_LOCALE) -> tuple:
330301
}
331302

332303
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+
)
334307

335308
return locales, "\n".join(messages)
336309

@@ -351,8 +324,7 @@ def get_language_pack(locale: str) -> tuple:
351324
guarantee the results represent the most up-to-date entry point
352325
information, which seems to be defined on interpreter startup.
353326
"""
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()
356328
found_packages_locales, message = get_installed_packages_locale(locale)
357329
locale_data = {}
358330
messages = message.split("\n")
@@ -365,7 +337,7 @@ def get_language_pack(locale: str) -> tuple:
365337
pkg_name = name.replace(".json", "")
366338
json_path = os.path.join(root, name)
367339
try:
368-
with open(json_path, "r", encoding='utf-8') as fh:
340+
with open(json_path, "r", encoding="utf-8") as fh:
369341
merged_data = json.load(fh)
370342
except Exception:
371343
messages.append(traceback.format_exc())

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ packages = find:
2828
python_requires = >=3.6
2929
install_requires =
3030
babel
31+
entrypoints>=0.2.2
3132
jinja2>=2.10
3233
json5
3334
jsonschema>=3.0.1

0 commit comments

Comments
 (0)