Skip to content

Commit

Permalink
chore: Transition from pkg_resources API to importlib-resources API
Browse files Browse the repository at this point in the history
  • Loading branch information
farhan committed May 30, 2024
1 parent e31150a commit fc0bcd6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
13 changes: 4 additions & 9 deletions sample_xblocks/filethumbs/filethumbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
"""



import json
import logging

import pkg_resources
import importlib.resources
import png
from web_fragments.fragment import Fragment
from xblock.core import XBlock
Expand Down Expand Up @@ -72,8 +70,7 @@ def student_view(self, context=None): # pylint: disable=W0613
"""

# Load the HTML fragment from within the package and fill in the template
html_str = pkg_resources.resource_string(__name__,
"static/html/thumbs.html").decode('utf-8')
html_str = importlib.resources.files(__package__).joinpath("static/html/thumbs.html").read_text()
frag = Fragment(str(html_str))

if not self.fs.exists("thumbsvotes.json"):
Expand All @@ -86,12 +83,10 @@ def student_view(self, context=None): # pylint: disable=W0613
self.downvotes = votes['down']

# Load the CSS and JavaScript fragments from within the package
css_str = pkg_resources.resource_string(__name__,
"static/css/thumbs.css").decode('utf-8')
css_str = importlib.resources.files(__package__).joinpath("static/css/thumbs.css").read_text()
frag.add_css(str(css_str))

js_str = pkg_resources.resource_string(__name__,
"static/js/src/thumbs.js").decode('utf-8')
js_str = importlib.resources.files(__package__).joinpath("static/js/src/thumbs.js").read_text()
frag.add_javascript(str(js_str))

with self.fs.open('uparrow.png', 'wb') as file_output:
Expand Down
13 changes: 4 additions & 9 deletions sample_xblocks/thumbs/thumbs.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"""An XBlock providing thumbs-up/thumbs-down voting."""



import logging

import pkg_resources
import importlib.resources
from web_fragments.fragment import Fragment
from xblock.core import XBlock, XBlockAside
from xblock.fields import Boolean, Integer, Scope
Expand Down Expand Up @@ -36,17 +34,14 @@ def student_view(self, context=None): # pylint: disable=W0613
"""

# Load the HTML fragment from within the package and fill in the template
html_str = pkg_resources.resource_string(__name__,
"static/html/thumbs.html").decode('utf-8')
html_str = importlib.resources.files(__package__).joinpath("static/html/thumbs.html").read_text()
frag = Fragment(str(html_str).format(block=self))

# Load the CSS and JavaScript fragments from within the package
css_str = pkg_resources.resource_string(__name__,
"static/css/thumbs.css").decode('utf-8')
css_str = importlib.resources.files(__package__).joinpath("static/css/thumbs.css").read_text()
frag.add_css(str(css_str))

js_str = pkg_resources.resource_string(__name__,
"static/js/src/thumbs.js").decode('utf-8')
js_str = importlib.resources.files(__package__).joinpath("static/js/src/thumbs.js").read_text()
frag.add_javascript(str(js_str))

frag.initialize_js('ThumbsBlock')
Expand Down

0 comments on commit fc0bcd6

Please sign in to comment.