Skip to content

Commit 8bdedda

Browse files
jesperhodgeschenedx
authored andcommitted
Revert "Replace pkg_resources with importlib.resources"
1 parent 2c7eefd commit 8bdedda

File tree

14 files changed

+62
-48
lines changed

14 files changed

+62
-48
lines changed

cms/djangoapps/contentstore/tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from tempfile import NamedTemporaryFile, mkdtemp
1212

1313
import olxcleaner
14-
from importlib.metadata import entry_points
14+
import pkg_resources
1515
from ccx_keys.locator import CCXLocator
1616
from celery import shared_task
1717
from celery.utils.log import get_task_logger
@@ -85,7 +85,7 @@
8585
FILE_READ_CHUNK = 1024 # bytes
8686
FULL_COURSE_REINDEX_THRESHOLD = 1
8787
ALL_ALLOWED_XBLOCKS = frozenset(
88-
[entry_point.name for entry_point in entry_points(group="xblock.v1")]
88+
[entry_point.name for entry_point in pkg_resources.iter_entry_points("xblock.v1")]
8989
)
9090

9191

cms/pytest.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ filterwarnings =
1616
ignore:.*You can remove default_app_config.*:PendingDeprecationWarning
1717
# ABC deprecation Warning comes from libsass
1818
ignore:Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated.*:DeprecationWarning:sass
19+
# declare_namespace Warning comes from XBlock https://github.com/openedx/XBlock/issues/641
20+
# and also due to dependency: https://github.com/PyFilesystem/pyfilesystem2
21+
ignore:Deprecated call to `pkg_resources.declare_namespace.*:DeprecationWarning
22+
ignore:.*pkg_resources is deprecated as an API.*:DeprecationWarning
1923
ignore:'etree' is deprecated. Use 'xml.etree.ElementTree' instead.:DeprecationWarning:wiki
2024

2125
norecursedirs = envs

common/djangoapps/edxmako/paths.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import hashlib
66
import os
77

8-
from importlib.resources import files
9-
from pathlib import Path
8+
import pkg_resources
109
from django.conf import settings
1110
from mako.exceptions import TopLevelLookupException
1211
from mako.lookup import TemplateLookup
@@ -123,7 +122,7 @@ def add_lookup(namespace, directory, package=None, prepend=False):
123122
"""
124123
Adds a new mako template lookup directory to the given namespace.
125124
126-
If `package` is specified, `importlib.resources` is used to look up the directory
125+
If `package` is specified, `pkg_resources` is used to look up the directory
127126
inside the given package. Otherwise `directory` is assumed to be a path
128127
in the filesystem.
129128
"""
@@ -137,11 +136,8 @@ def add_lookup(namespace, directory, package=None, prepend=False):
137136
encoding_errors='replace',
138137
)
139138
if package:
140-
package, module_path = package.split('.', 1)
141-
module_dir = str(Path(module_path).parent)
142-
directory = files(package).joinpath(module_dir, directory)
143-
144-
templates.add_directory(str(directory), prepend=prepend)
139+
directory = pkg_resources.resource_filename(package, directory)
140+
templates.add_directory(directory, prepend=prepend)
145141

146142

147143
@request_cached()

common/test/pytest.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ filterwarnings =
1515
ignore:.*You can remove default_app_config.*:PendingDeprecationWarning
1616
# ABC deprecation Warning comes from libsass
1717
ignore:Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated.*:DeprecationWarning:sass
18+
# declare_namespace Warning comes from XBlock https://github.com/openedx/XBlock/issues/641
19+
# and also due to dependency: https://github.com/PyFilesystem/pyfilesystem2
20+
ignore:Deprecated call to `pkg_resources.declare_namespace.*:DeprecationWarning
21+
ignore:.*pkg_resources is deprecated as an API.*:DeprecationWarning
1822
ignore:'etree' is deprecated. Use 'xml.etree.ElementTree' instead.:DeprecationWarning:wiki
1923

2024
norecursedirs = .cache

docs/decisions/0000-static-asset-plan.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ mechanism:
8484

8585
``openedx.core.lib.xblock_pipeline.finder.XBlockPipelineFinder``
8686
Custom finder that accesses and extracts assets from pip-installed XBlocks via
87-
``importlib.resources``.
87+
``pkg_resources``.
8888

8989
``openedx.core.storage.DevelopmentStorage/ProductionStorage``
9090
Custom ``FileStorage`` classes that mostly exist for theme-awareness.

docs/decisions/0019-oep-58-atlas-translations-design.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ be updated to support the new ``XBlockI18nService``
305305
def _get_statici18n_js_url():
306306
"""
307307
Returns the Javascript translation file for the currently selected language, if any found by
308-
`importlib.resources.files`
308+
`pkg_resources`
309309
"""
310310
lang_code = translation.get_language()
311311
if not lang_code:
@@ -320,8 +320,8 @@ be updated to support the new ``XBlockI18nService``
320320
text_js = 'public/js/translations/{lang_code}/text.js'
321321
country_code = lang_code.split('-')[0]
322322
for code in (translation.to_locale(lang_code), lang_code, country_code):
323-
if files(loader.module_name).joinpath(text_js.format(lang_code=code)).is_file():
324-
return text_js.format(lang_code=code)
323+
if pkg_resources.resource_exists(loader.module_name, text_js.format(lang_code=code)):
324+
return text_js.format(lang_code=code)
325325
return None
326326
327327

openedx/core/lib/logsettings.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ def log_python_warnings():
144144
category=DeprecationWarning,
145145
module="sass",
146146
)
147+
warnings.filterwarnings(
148+
'ignore',
149+
'Deprecated call to `pkg_resources.declare_namespace.*',
150+
category=DeprecationWarning,
151+
)
152+
warnings.filterwarnings(
153+
'ignore',
154+
'.*pkg_resources is deprecated as an API.*',
155+
category=DeprecationWarning,
156+
)
147157
warnings.filterwarnings(
148158
'ignore', "'etree' is deprecated. Use 'xml.etree.ElementTree' instead.",
149159
category=DeprecationWarning, module='wiki'

openedx/core/lib/xblock_pipeline/finder.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from django.contrib.staticfiles.storage import FileSystemStorage
1111
from django.core.files.storage import Storage
1212
from django.utils import timezone
13-
from importlib.resources import files
13+
from pkg_resources import resource_exists, resource_filename, resource_isdir, resource_listdir
1414
from xblock.core import XBlock
1515

1616
from openedx.core.lib.xblock_utils import xblock_resource_pkg
@@ -38,7 +38,7 @@ def path(self, name):
3838
"""
3939
Returns a file system filename for the specified file name.
4040
"""
41-
return str(files(self.module).joinpath(self.base_dir, name))
41+
return resource_filename(self.module, os.path.join(self.base_dir, name))
4242

4343
def exists(self, path): # lint-amnesty, pylint: disable=arguments-differ
4444
"""
@@ -47,22 +47,22 @@ def exists(self, path): # lint-amnesty, pylint: disable=arguments-differ
4747
if self.base_dir is None:
4848
return False
4949

50-
return os.path.exists(os.path.join(self.base_dir, path))
50+
return resource_exists(self.module, os.path.join(self.base_dir, path))
5151

5252
def listdir(self, path):
5353
"""
5454
Lists the directories beneath the specified path.
5555
"""
5656
directories = []
57-
files_p = []
58-
for item in files(self.module).joinpath(self.base_dir, path).iterdir():
57+
files = []
58+
for item in resource_listdir(self.module, os.path.join(self.base_dir, path)):
5959
__, file_extension = os.path.splitext(item)
6060
if file_extension not in [".py", ".pyc", ".scss"]:
61-
if files(self.module).joinpath(self.base_dir, path, item).is_dir():
61+
if resource_isdir(self.module, os.path.join(self.base_dir, path, item)):
6262
directories.append(item)
6363
else:
64-
files_p.append(item)
65-
return directories, files_p
64+
files.append(item)
65+
return directories, files
6666

6767
def open(self, name, mode='rb'):
6868
"""

openedx/tests/xblock_integration/test_external_xblocks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"""
1010

1111

12-
from importlib.metadata import entry_points
12+
import pkg_resources
1313

1414

1515
class DuplicateXBlockTest(Exception):
@@ -37,7 +37,7 @@ class InvalidTestName(Exception):
3737

3838
xblock_loaded = False # pylint: disable=invalid-name
3939

40-
for entrypoint in entry_points(group="xblock.test.v0"):
40+
for entrypoint in pkg_resources.iter_entry_points(group="xblock.test.v0"):
4141
plugin = entrypoint.load()
4242
classname = plugin.__name__
4343
if classname in globals():

scripts/xblock/list-installed.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Lookup list of installed XBlocks, to aid XBlock developers
33
"""
4-
from importlib.metadata import entry_points
4+
import pkg_resources
55

66

77
def get_without_builtins():
@@ -12,10 +12,11 @@ def get_without_builtins():
1212
"""
1313
xblocks = [
1414
entry_point.name
15-
for entry_point in entry_points(group='xblock.v1')
16-
if not entry_point.module.startswith('xmodule')
15+
for entry_point in pkg_resources.iter_entry_points('xblock.v1')
16+
if not entry_point.module_name.startswith('xmodule')
1717
]
18-
return sorted(xblocks)
18+
xblocks = sorted(xblocks)
19+
return xblocks
1920

2021

2122
def main():

setup.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ filterwarnings =
1717
ignore:Instead access HTTPResponse.headers directly.*:DeprecationWarning:elasticsearch
1818
# ABC deprecation Warning comes from libsass
1919
ignore:Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated.*:DeprecationWarning:sass
20+
# declare_namespace Warning comes from XBlock https://github.com/openedx/XBlock/issues/641
21+
# and also due to dependency: https://github.com/PyFilesystem/pyfilesystem2
22+
ignore:Deprecated call to `pkg_resources.declare_namespace.*:DeprecationWarning
23+
ignore:.*pkg_resources is deprecated as an API.*:DeprecationWarning
2024
ignore:'etree' is deprecated. Use 'xml.etree.ElementTree' instead.:DeprecationWarning:wiki
2125

2226
junit_family = xunit2

xmodule/modulestore/django.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import gettext
1010
import logging
1111

12-
from importlib.resources import path as resources_path
12+
from pkg_resources import resource_filename
1313
import re # lint-amnesty, pylint: disable=wrong-import-order
1414

1515
from django.conf import settings
@@ -422,8 +422,7 @@ def get_python_locale(self, block):
422422
return 'django', xblock_locale_path
423423

424424
# Pre-OEP-58 translations within the XBlock pip packages are deprecated but supported.
425-
deprecated_xblock_locale_path = str(resources_path(xblock_module_name, 'translations'))
426-
425+
deprecated_xblock_locale_path = resource_filename(xblock_module_name, 'translations')
427426
# The `text` domain was used for XBlocks pre-OEP-58.
428427
return 'text', deprecated_xblock_locale_path
429428

xmodule/modulestore/tests/test_django_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ def test_get_python_locale_with_atlas_oep58_translations(mock_modern_xblock):
2323
assert domain == 'django', 'Uses django domain when atlas locale is found.'
2424

2525

26-
@patch('xmodule.modulestore.django.resources_path', return_value='/lib/my_legacy_xblock/translations')
26+
@patch('xmodule.modulestore.django.resource_filename', return_value='/lib/my_legacy_xblock/translations')
2727
def test_get_python_locale_with_bundled_translations(mock_modern_xblock):
2828
"""
2929
Ensure that get_python_locale() falls back to XBlock internal translations if atlas translations weren't pulled.
3030
3131
Pre-OEP-58 translations were stored in the `translations` directory of the XBlock which is
32-
accessible via the `importlib.resources.path` function.
32+
accessible via the `pkg_resources.resource_filename` function.
3333
"""
3434
i18n_service = XBlockI18nService()
3535
block = mock_modern_xblock['legacy_xblock']

xmodule/x_module.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
from lxml import etree
1414
from opaque_keys.edx.asides import AsideDefinitionKeyV2, AsideUsageKeyV2
1515
from opaque_keys.edx.keys import UsageKey
16-
from importlib.resources import files, as_file
17-
from pathlib import Path as P
16+
from pkg_resources import resource_isdir, resource_filename
1817
from web_fragments.fragment import Fragment
1918
from webob import Response
2019
from webob.multidict import MultiDict
@@ -856,35 +855,32 @@ def templates(cls):
856855
@classmethod
857856
def get_template_dir(cls): # lint-amnesty, pylint: disable=missing-function-docstring
858857
if getattr(cls, 'template_dir_name', None):
859-
dirname = os.path.join('templates', cls.template_dir_name)
860-
if not os.path.isdir(os.path.join(os.path.dirname(__file__), dirname)):
858+
dirname = os.path.join('templates', cls.template_dir_name) # lint-amnesty, pylint: disable=no-member
859+
if not resource_isdir(__name__, dirname):
861860
log.warning("No resource directory {dir} found when loading {cls_name} templates".format(
862861
dir=dirname,
863862
cls_name=cls.__name__,
864863
))
865864
return None
866-
return dirname
867-
return None
865+
else:
866+
return dirname
867+
else:
868+
return None
868869

869870
@classmethod
870871
def get_template_dirpaths(cls):
871872
"""
872-
Returns a list of directories containing resource templates.
873+
Returns of list of directories containing resource templates.
873874
"""
874875
template_dirpaths = []
875876
template_dirname = cls.get_template_dir()
876-
package, module_path = __name__.split('.', 1)
877-
module_dir = str(P(module_path).parent)
878-
module_dir = "" if module_dir == "." else module_dir
879-
file_dirs = files(package).joinpath(module_dir, template_dirname or "")
880-
if template_dirname and file_dirs.is_dir():
881-
with as_file(file_dirs) as path:
882-
template_dirpaths.append(path)
877+
if template_dirname and resource_isdir(__name__, template_dirname):
878+
template_dirpaths.append(resource_filename(__name__, template_dirname))
883879

884880
custom_template_dir = cls.get_custom_template_dir()
885881
if custom_template_dir:
886882
template_dirpaths.append(custom_template_dir)
887-
return [str(td) for td in template_dirpaths]
883+
return template_dirpaths
888884

889885
@classmethod
890886
def get_custom_template_dir(cls):

0 commit comments

Comments
 (0)