Skip to content

Commit

Permalink
fix: replace pkg_resoucres
Browse files Browse the repository at this point in the history
  • Loading branch information
irtazaakram committed Jan 7, 2025
1 parent 85ecad1 commit 12596b8
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 62 deletions.
4 changes: 2 additions & 2 deletions cms/djangoapps/contentstore/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from tempfile import NamedTemporaryFile, mkdtemp

import olxcleaner
import pkg_resources
from importlib.metadata import entry_points
from ccx_keys.locator import CCXLocator
from celery import shared_task
from celery.utils.log import get_task_logger
Expand Down Expand Up @@ -85,7 +85,7 @@
FILE_READ_CHUNK = 1024 # bytes
FULL_COURSE_REINDEX_THRESHOLD = 1
ALL_ALLOWED_XBLOCKS = frozenset(
[entry_point.name for entry_point in pkg_resources.iter_entry_points("xblock.v1")]
[entry_point.name for entry_point in entry_points(group="xblock.v1")]
)


Expand Down
4 changes: 0 additions & 4 deletions cms/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ filterwarnings =
ignore:.*You can remove default_app_config.*:PendingDeprecationWarning
# ABC deprecation Warning comes from libsass
ignore:Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated.*:DeprecationWarning:sass
# declare_namespace Warning comes from XBlock https://github.com/openedx/XBlock/issues/641
# and also due to dependency: https://github.com/PyFilesystem/pyfilesystem2
ignore:Deprecated call to `pkg_resources.declare_namespace.*:DeprecationWarning
ignore:.*pkg_resources is deprecated as an API.*:DeprecationWarning
ignore:'etree' is deprecated. Use 'xml.etree.ElementTree' instead.:DeprecationWarning:wiki

norecursedirs = envs
Expand Down
11 changes: 5 additions & 6 deletions common/djangoapps/edxmako/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import hashlib
import os

import pkg_resources
from importlib.resources import files
from django.conf import settings
from mako.exceptions import TopLevelLookupException
from mako.lookup import TemplateLookup
Expand Down Expand Up @@ -120,11 +120,10 @@ def clear_lookups(namespace):

def add_lookup(namespace, directory, package=None, prepend=False):
"""
Adds a new mako template lookup directory to the given namespace.
Adds a new Mako template lookup directory to the given namespace.
If `package` is specified, `pkg_resources` is used to look up the directory
inside the given package. Otherwise `directory` is assumed to be a path
in the filesystem.
If `package` is specified, `importlib.resources` is used to look up the directory
inside the given package. Otherwise, `directory` is assumed to be a path in the filesystem.
"""
templates = LOOKUP.get(namespace)
if not templates:
Expand All @@ -136,7 +135,7 @@ def add_lookup(namespace, directory, package=None, prepend=False):
encoding_errors='replace',
)
if package:
directory = pkg_resources.resource_filename(package, directory)
directory = str(files(package).joinpath(directory))
templates.add_directory(directory, prepend=prepend)


Expand Down
4 changes: 0 additions & 4 deletions common/test/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ filterwarnings =
ignore:.*You can remove default_app_config.*:PendingDeprecationWarning
# ABC deprecation Warning comes from libsass
ignore:Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated.*:DeprecationWarning:sass
# declare_namespace Warning comes from XBlock https://github.com/openedx/XBlock/issues/641
# and also due to dependency: https://github.com/PyFilesystem/pyfilesystem2
ignore:Deprecated call to `pkg_resources.declare_namespace.*:DeprecationWarning
ignore:.*pkg_resources is deprecated as an API.*:DeprecationWarning
ignore:'etree' is deprecated. Use 'xml.etree.ElementTree' instead.:DeprecationWarning:wiki

norecursedirs = .cache
2 changes: 1 addition & 1 deletion docs/decisions/0000-static-asset-plan.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ mechanism:

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

``openedx.core.storage.DevelopmentStorage/ProductionStorage``
Custom ``FileStorage`` classes that mostly exist for theme-awareness.
Expand Down
6 changes: 3 additions & 3 deletions docs/decisions/0019-oep-58-atlas-translations-design.rst
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ be updated to support the new ``XBlockI18nService``
def _get_statici18n_js_url():
"""
Returns the Javascript translation file for the currently selected language, if any found by
`pkg_resources`
`importlib.resources.files`
"""
lang_code = translation.get_language()
if not lang_code:
Expand All @@ -320,8 +320,8 @@ be updated to support the new ``XBlockI18nService``
text_js = 'public/js/translations/{lang_code}/text.js'
country_code = lang_code.split('-')[0]
for code in (translation.to_locale(lang_code), lang_code, country_code):
if pkg_resources.resource_exists(loader.module_name, text_js.format(lang_code=code)):
return text_js.format(lang_code=code)
if files(loader.module_name).joinpath(text_js.format(lang_code=code)).is_file():
return text_js.format(lang_code=code)
return None
Expand Down
10 changes: 0 additions & 10 deletions openedx/core/lib/logsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,6 @@ def log_python_warnings():
category=DeprecationWarning,
module="sass",
)
warnings.filterwarnings(
'ignore',
'Deprecated call to `pkg_resources.declare_namespace.*',
category=DeprecationWarning,
)
warnings.filterwarnings(
'ignore',
'.*pkg_resources is deprecated as an API.*',
category=DeprecationWarning,
)
warnings.filterwarnings(
'ignore', "'etree' is deprecated. Use 'xml.etree.ElementTree' instead.",
category=DeprecationWarning, module='wiki'
Expand Down
16 changes: 8 additions & 8 deletions openedx/core/lib/xblock_pipeline/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.contrib.staticfiles.storage import FileSystemStorage
from django.core.files.storage import Storage
from django.utils import timezone
from pkg_resources import resource_exists, resource_filename, resource_isdir, resource_listdir
from importlib.resources import files
from xblock.core import XBlock

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

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

return resource_exists(self.module, os.path.join(self.base_dir, path))
return os.path.exists(os.path.join(self.base_dir, path))

def listdir(self, path):
"""
Lists the directories beneath the specified path.
"""
directories = []
files = []
for item in resource_listdir(self.module, os.path.join(self.base_dir, path)):
files_p = []
for item in files(self.module).joinpath(self.base_dir, path).iterdir():
__, file_extension = os.path.splitext(item)
if file_extension not in [".py", ".pyc", ".scss"]:
if resource_isdir(self.module, os.path.join(self.base_dir, path, item)):
if files(self.module).joinpath(self.base_dir, path, item).is_dir():
directories.append(item)
else:
files.append(item)
return directories, files
files_p.append(item)
return directories, files_p

def open(self, name, mode='rb'):
"""
Expand Down
4 changes: 2 additions & 2 deletions openedx/tests/xblock_integration/test_external_xblocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""


import pkg_resources
from importlib.metadata import entry_points


class DuplicateXBlockTest(Exception):
Expand Down Expand Up @@ -37,7 +37,7 @@ class InvalidTestName(Exception):

xblock_loaded = False # pylint: disable=invalid-name

for entrypoint in pkg_resources.iter_entry_points(group="xblock.test.v0"):
for entrypoint in entry_points(group="xblock.test.v0"):
plugin = entrypoint.load()
classname = plugin.__name__
if classname in globals():
Expand Down
9 changes: 4 additions & 5 deletions scripts/xblock/list-installed.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Lookup list of installed XBlocks, to aid XBlock developers
"""
import pkg_resources
from importlib.metadata import entry_points


def get_without_builtins():
Expand All @@ -12,11 +12,10 @@ def get_without_builtins():
"""
xblocks = [
entry_point.name
for entry_point in pkg_resources.iter_entry_points('xblock.v1')
if not entry_point.module_name.startswith('xmodule')
for entry_point in entry_points(group='xblock.v1')
if not entry_point.module.startswith('xmodule')
]
xblocks = sorted(xblocks)
return xblocks
return sorted(xblocks)


def main():
Expand Down
4 changes: 0 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ filterwarnings =
ignore:Instead access HTTPResponse.headers directly.*:DeprecationWarning:elasticsearch
# ABC deprecation Warning comes from libsass
ignore:Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated.*:DeprecationWarning:sass
# declare_namespace Warning comes from XBlock https://github.com/openedx/XBlock/issues/641
# and also due to dependency: https://github.com/PyFilesystem/pyfilesystem2
ignore:Deprecated call to `pkg_resources.declare_namespace.*:DeprecationWarning
ignore:.*pkg_resources is deprecated as an API.*:DeprecationWarning
ignore:'etree' is deprecated. Use 'xml.etree.ElementTree' instead.:DeprecationWarning:wiki

junit_family = xunit2
Expand Down
4 changes: 2 additions & 2 deletions xmodule/modulestore/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import gettext
import logging

from pkg_resources import resource_filename
from importlib.resources import files
import re # lint-amnesty, pylint: disable=wrong-import-order

from django.conf import settings
Expand Down Expand Up @@ -422,7 +422,7 @@ def get_python_locale(self, block):
return 'django', xblock_locale_path

# Pre-OEP-58 translations within the XBlock pip packages are deprecated but supported.
deprecated_xblock_locale_path = resource_filename(xblock_module_name, 'translations')
deprecated_xblock_locale_path = str(files(xblock_module_name) / 'translations')
# The `text` domain was used for XBlocks pre-OEP-58.
return 'text', deprecated_xblock_locale_path

Expand Down
2 changes: 1 addition & 1 deletion xmodule/modulestore/tests/test_django_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_get_python_locale_with_bundled_translations(mock_modern_xblock):
Ensure that get_python_locale() falls back to XBlock internal translations if atlas translations weren't pulled.
Pre-OEP-58 translations were stored in the `translations` directory of the XBlock which is
accessible via the `pkg_resources.resource_filename` function.
accessible via the `importlib.resources.files` function.
"""
i18n_service = XBlockI18nService()
block = mock_modern_xblock['legacy_xblock']
Expand Down
24 changes: 14 additions & 10 deletions xmodule/x_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from lxml import etree
from opaque_keys.edx.asides import AsideDefinitionKeyV2, AsideUsageKeyV2
from opaque_keys.edx.keys import UsageKey
from pkg_resources import resource_isdir, resource_filename
from importlib.resources import files, as_file
from pathlib import Path
from web_fragments.fragment import Fragment
from webob import Response
from webob.multidict import MultiDict
Expand Down Expand Up @@ -855,27 +856,30 @@ def templates(cls):
@classmethod
def get_template_dir(cls): # lint-amnesty, pylint: disable=missing-function-docstring
if getattr(cls, 'template_dir_name', None):
dirname = os.path.join('templates', cls.template_dir_name) # lint-amnesty, pylint: disable=no-member
if not resource_isdir(__name__, dirname):
dirname = os.path.join('templates', cls.template_dir_name)
if not os.path.isdir(os.path.join(os.path.dirname(__file__), dirname)):
log.warning("No resource directory {dir} found when loading {cls_name} templates".format(
dir=dirname,
cls_name=cls.__name__,
))
return None
else:
return dirname
else:
return None
return dirname
return None

@classmethod
def get_template_dirpaths(cls):
"""
Returns of list of directories containing resource templates.
Returns a list of directories containing resource templates.
"""
template_dirpaths = []
template_dirname = cls.get_template_dir()
if template_dirname and resource_isdir(__name__, template_dirname):
template_dirpaths.append(resource_filename(__name__, template_dirname))
package, module_path = __name__.split('.', 1)
module_dir = str(Path(module_path).parent)
file_dirs = files(package).joinpath(module_dir, template_dirname)

if template_dirname and file_dirs.is_dir():
with as_file(file_dirs) as path:
template_dirpaths.append(path)

custom_template_dir = cls.get_custom_template_dir()
if custom_template_dir:
Expand Down

0 comments on commit 12596b8

Please sign in to comment.