Skip to content

Commit

Permalink
Changed from deprecated pkg_resources code to importlib.resources for…
Browse files Browse the repository at this point in the history
… handling package files.
  • Loading branch information
ajenhl committed Jun 12, 2023
1 parent 3ea935a commit f1921a0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 40 deletions.
11 changes: 4 additions & 7 deletions tacl/report.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import importlib.resources
import os.path
import shutil

from jinja2 import Environment, PackageLoader

from pkg_resources import resource_filename, resource_listdir


class Report:

Expand Down Expand Up @@ -33,11 +32,9 @@ def _copy_static_assets(self, output_dir):
:type output_dir: `str`
"""
base_directory = 'assets/{}'.format(self._report_name)
for asset in resource_listdir(self._package_name, base_directory):
filename = resource_filename(
self._package_name, '{}/{}'.format(base_directory, asset))
shutil.copy2(filename, output_dir)
base_directory = 'tacl.assets.{}'.format(self._report_name)
for asset in importlib.resources.files(base_directory).iterdir():
shutil.copy2(asset, output_dir)

def generate(self, output_dir):
"""Generate the report, writing it to `output_dir`."""
Expand Down
11 changes: 6 additions & 5 deletions tacl/stripper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Module containing the Stripper class."""

import importlib.resources
import logging
import os

from lxml import etree
from pkg_resources import resource_filename

from . import constants

Expand All @@ -24,9 +24,10 @@ def __init__(self, input_dir, output_dir):
self._logger = logging.getLogger(__name__)
self._input_dir = os.path.abspath(input_dir)
self._output_dir = os.path.abspath(output_dir)
xslt_filename = resource_filename(
__name__, 'assets/xslt/strip_tei.xsl')
self.transform = etree.XSLT(etree.parse(xslt_filename))
xslt_ref = importlib.resources.files('tacl') / \
'assets/xslt/strip_tei.xsl'
with importlib.resources.as_file(xslt_ref) as xslt_path:
self.transform = etree.XSLT(etree.parse(xslt_path))

def get_witnesses(self, source_tree):
"""Returns a list of all witnesses of variant readings in
Expand Down Expand Up @@ -74,7 +75,7 @@ def strip_files(self):
'Could not create output directory: {}'.format(err))
raise
for dirpath, dirnames, filenames in os.walk(self._input_dir):
for filename in filenames:
for filename in sorted(filenames):
if os.path.splitext(filename)[1] == '.xml':
work, witnesses = self.strip_file(
os.path.join(dirpath, filename))
Expand Down
46 changes: 18 additions & 28 deletions tacl/tei_corpus.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Module containing the TEICorpus class."""

import importlib.resources
import logging
import os
import re

from lxml import etree
from pkg_resources import resource_filename

from . import constants
from .exceptions import TACLError
Expand Down Expand Up @@ -69,9 +69,7 @@ def __init__(self, input_dir, output_dir):
self._logger = logging.getLogger(__name__)
self._input_dir = os.path.abspath(input_dir)
self._output_dir = os.path.abspath(output_dir)
xslt_filename = resource_filename(__name__, 'assets/xslt/{}'.format(
self.xslt))
self.transform = etree.XSLT(etree.parse(xslt_filename))
self.transform = self._get_xslt('assets/xslt/{}'.format(self.xslt))

def _assemble_parts(self, work, paths):
parts = list(paths.keys())
Expand Down Expand Up @@ -116,6 +114,11 @@ def _assemble_part_list(self):
def _extract_work(self, filename):
raise NotImplementedError

def _get_xslt(self, path):
ref = importlib.resources.files('tacl') / path
with importlib.resources.as_file(ref) as xslt_path:
return etree.XSLT(etree.parse(xslt_path))

def get_witnesses(self, source_tree):
"""Returns a sorted list of all witnesses of readings in
`source_tree`, and the elements that bear @wit attributes.
Expand Down Expand Up @@ -275,12 +278,9 @@ class TEICorpusCBETAGitHub (TEICorpus):

def __init__(self, *args):
super().__init__(*args)
div_xslt = resource_filename(
__name__, 'assets/xslt/CBETA_extract_div.xsl')
self._transform_div = etree.XSLT(etree.parse(div_xslt))
remove_divs_xslt = resource_filename(
__name__, 'assets/xslt/CBETA_remove_divs.xsl')
self._remove_divs = etree.XSLT(etree.parse(remove_divs_xslt))
self._transform_div = self._get_xslt(
'assets/xslt/CBETA_extract_div.xsl')
self._remove_divs = self._get_xslt('assets/xslt/CBETA_remove_divs.xsl')

def _extract_divs(self, work, tree, div_types, exclude=None):
"""Writes out to files the individual parts of a work corresponding to
Expand Down Expand Up @@ -532,9 +532,7 @@ def _postprocess_T0186(self, work, tree):
material, and one the rest.
"""
xslt_filename = resource_filename(
__name__, 'assets/xslt/CBETA_extract_verse.xsl')
extract_verse = etree.XSLT(etree.parse(xslt_filename))
extract_verse = self._get_xslt('assets/xslt/CBETA_extract_verse.xsl')
verse_tree = extract_verse(tree)
verse_filename = '{}-verses.xml'.format(work)
self._output_tree(verse_filename, verse_tree)
Expand Down Expand Up @@ -630,9 +628,8 @@ def _postprocess_T0220(self, work, tree):
Also divide into one file for each cb:div[@type='dharani'].
"""
xslt_filename = resource_filename(
__name__, 'assets/xslt/CBETA_T0220_reparent_divs.xsl')
move_non_hui_divs = etree.XSLT(etree.parse(xslt_filename))
move_non_hui_divs = self._get_xslt(
'assets/xslt/CBETA_T0220_reparent_divs.xsl')
tree = move_non_hui_divs(tree)
divs = tree.xpath('//cb:div[@type="hui"]',
namespaces=constants.NAMESPACES)
Expand All @@ -650,9 +647,8 @@ def _postprocess_T0225(self, work, tree):
tei:note[@place='inline'] material, and one the rest.
"""
xslt_filename = resource_filename(
__name__, 'assets/xslt/CBETA_extract_commentary.xsl')
extract_commentary = etree.XSLT(etree.parse(xslt_filename))
extract_commentary = self._get_xslt(
'assets/xslt/CBETA_extract_commentary.xsl')
commentary_tree = extract_commentary(tree)
commentary_filename = '{}-commentary.xml'.format(work)
self._output_tree(commentary_filename, commentary_tree)
Expand Down Expand Up @@ -682,9 +678,7 @@ def _postprocess_T0328(self, work, tree):
material, and one the rest.
"""
xslt_filename = resource_filename(
__name__, 'assets/xslt/CBETA_extract_verse.xsl')
extract_verse = etree.XSLT(etree.parse(xslt_filename))
extract_verse = self._get_xslt('assets/xslt/CBETA_extract_verse.xsl')
verse_tree = extract_verse(tree)
verse_filename = '{}-verses.xml'.format(work)
self._output_tree(verse_filename, verse_tree)
Expand Down Expand Up @@ -712,13 +706,9 @@ def _postprocess_T0418(self, work, tree):
# There are verse elements within tei:app/tei:rdg that occur
# within a prose context and have no markup specifying that
# they are verse, so add that in.
xslt_filename = resource_filename(
__name__, 'assets/xslt/CBETA_T0418_fix_verse.xsl')
fix_verse = etree.XSLT(etree.parse(xslt_filename))
fix_verse = self._get_xslt('assets/xslt/CBETA_T0418_fix_verse.xsl')
tree = fix_verse(tree)
xslt_filename = resource_filename(
__name__, 'assets/xslt/CBETA_extract_verse.xsl')
extract_verse = etree.XSLT(etree.parse(xslt_filename))
extract_verse = self._get_xslt('assets/xslt/CBETA_extract_verse.xsl')
verse_tree = extract_verse(tree)
verse_filename = '{}-revised-verses.xml'.format(work)
self._output_tree(verse_filename, verse_tree)
Expand Down

0 comments on commit f1921a0

Please sign in to comment.