fix: replace pkg_resources with importlib.resources (#36213)

This commit is contained in:
Irtaza Akram
2025-02-13 17:43:07 +05:00
committed by GitHub
parent 5623b36e55
commit a8a8ae3286
12 changed files with 45 additions and 61 deletions

View File

@@ -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'

View File

@@ -4,13 +4,13 @@ Django pipeline finder for handling static assets required by XBlocks.
import os
from datetime import datetime
import importlib.resources as resources
from django.contrib.staticfiles import utils
from django.contrib.staticfiles.finders import BaseFinder
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 xblock.core import XBlock
from openedx.core.lib.xblock_utils import xblock_resource_pkg
@@ -38,7 +38,8 @@ class XBlockPackageStorage(Storage):
"""
Returns a file system filename for the specified file name.
"""
return resource_filename(self.module, os.path.join(self.base_dir, name))
with resources.as_file(resources.files(self.module.rsplit('.', 1)[0]) / self.base_dir / name) as file_path:
return str(file_path)
def exists(self, path): # lint-amnesty, pylint: disable=arguments-differ
"""
@@ -46,8 +47,7 @@ class XBlockPackageStorage(Storage):
"""
if self.base_dir is None:
return False
return resource_exists(self.module, os.path.join(self.base_dir, path))
return (resources.files(self.module.rsplit('.', 1)[0]) / self.base_dir / path).exists()
def listdir(self, path):
"""
@@ -55,13 +55,14 @@ class XBlockPackageStorage(Storage):
"""
directories = []
files = []
for item in resource_listdir(self.module, os.path.join(self.base_dir, path)):
__, 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)):
directories.append(item)
else:
files.append(item)
base_path = resources.files(self.module.rsplit('.', 1)[0]) / self.base_dir / path
if base_path.is_dir():
for item in base_path.iterdir():
if item.suffix not in [".py", ".pyc", ".scss"]:
if item.is_dir():
directories.append(item.name)
else:
files.append(item.name)
return directories, files
def open(self, name, mode='rb'):