Skip to content

Commit

Permalink
chore: Shift from pkg_resources to importlib.resources
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBhatti committed Sep 11, 2024
1 parent 40e7b84 commit 1086127
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
8 changes: 5 additions & 3 deletions sql_grader/mixins/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
Mixin workbench behavior into XBlocks
"""
from glob import glob
import pkg_resources
import importlib.resources
import importlib


def _read_file(file_path):
Expand Down Expand Up @@ -62,7 +63,8 @@ def workbench_scenarios(cls):
"""
module = cls.__module__
module = module.split('.', maxsplit=1)[0]
directory = pkg_resources.resource_filename(module, 'scenarios')
files = _find_files(directory)
module_ref = importlib.import_module(module)
files = importlib.resources.files(module_ref).joinpath('scenarios')
files = _find_files(files)
scenarios = _read_files(files)
return scenarios
17 changes: 10 additions & 7 deletions sql_grader/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
import os
import sqlite3

import pkg_resources
try:
from xblock.utils.resources import ResourceLoader
except ModuleNotFoundError:
# For backward compatibility with releases older than Quince.
from xblockutils.resources import ResourceLoader
import importlib.resources


loader = ResourceLoader(__name__)

class SqlProblem:
"""
Handle modeling and processing of SQL problems aside from XBlock logic
Expand Down Expand Up @@ -178,10 +185,7 @@ def all_datasets():
"""
Lookup the names of all avaiable datasets (.sql files)
"""
dataset_directory = pkg_resources.resource_filename(
'sql_grader',
'datasets'
)
dataset_directory = importlib.resources.files('sql_grader') / 'datasets'
for _, _, files in os.walk(dataset_directory):
for fname in files:
if fname.endswith('.sql'):
Expand All @@ -193,8 +197,7 @@ def resource_string(path):
"""
Handy helper for getting resources from our kit
"""
data = pkg_resources.resource_string(__name__, path)
return data.decode('utf8')
return loader.load_unicode(path)


def create_database(database_name):
Expand Down

0 comments on commit 1086127

Please sign in to comment.