Replace pkg_resources with importlib.resources for Python 3.14 compatibility

This commit is contained in:
wave1martian
2026-03-08 20:24:24 +01:00
parent c89fc132b8
commit 17c304066a
11 changed files with 32 additions and 44 deletions
+2 -3
View File
@@ -1,6 +1,5 @@
import mimetypes
import pkg_resources
from importlib.resources import files
mimetypes.init([pkg_resources.
resource_filename('ebook_converter', 'data/mime.types')])
mimetypes.init([str(files('ebook_converter').joinpath('data/mime.types'))])
+2 -3
View File
@@ -7,7 +7,7 @@ import mimetypes
import numbers
import optparse
import os
import pkg_resources
from importlib.resources import files
import re
import sys
@@ -351,8 +351,7 @@ def read_sr_patterns(path, log=None):
def main(args=sys.argv):
log = logging.default_log
mimetypes.init([pkg_resources.resource_filename('ebook_converter',
'data/mime.types')])
mimetypes.init([str(files('ebook_converter').joinpath('data/mime.types'))])
parser, plumber = create_option_parser(args, log)
opts, leftover_args = parser.parse_args(args)
if len(leftover_args) > 3:
@@ -3,7 +3,7 @@ Convert .fb2 files to .lrf
"""
import mimetypes
import os
import pkg_resources
from importlib.resources import files
import re
from lxml import etree
@@ -85,8 +85,8 @@ class FB2Input(InputFormatPlugin):
css = re.sub(r'name\s*=\s*', 'class=', css)
self.extract_embedded_content(doc)
log.debug('Converting XML to HTML...')
with open(pkg_resources.resource_filename('ebook_converter',
'data/fb2.xsl')) as f:
with open(str(files('ebook_converter')
.joinpath('data/fb2.xsl'))) as f:
ss = f.read()
ss = ss.replace("__FB_NS__", fb_ns)
if options.no_inline_fb2_toc:
@@ -1,5 +1,5 @@
import os
import pkg_resources
from importlib.resources import files
import re
import shutil
@@ -92,9 +92,8 @@ class HTMLOutput(OutputFormatPlugin):
with open(opts.template_html_index, 'rb') as f:
template_html_index_data = f.read()
else:
with open(pkg_resources.
resource_filename('ebook_converter',
'data/html_export_default_index.tmpl')
with open(str(files('ebook_converter')
.joinpath('data/html_export_default_index.tmpl'))
) as fobj:
template_html_index_data = fobj.read().decode()
@@ -102,9 +101,8 @@ class HTMLOutput(OutputFormatPlugin):
with open(opts.template_html, 'rb') as f:
template_html_data = f.read()
else:
with open(pkg_resources.
resource_filename('ebook_converter',
'data/html_export_default.tmpl')
with open(str(files('ebook_converter')
.joinpath('data/html_export_default.tmpl'))
) as fobj:
template_html_data = fobj.read().decode()
@@ -112,9 +110,8 @@ class HTMLOutput(OutputFormatPlugin):
with open(opts.template_css, 'rb') as f:
template_css_data = f.read()
else:
with open(pkg_resources.
resource_filename('ebook_converter',
'data/html_export_default.css')
with open(str(files('ebook_converter')
.joinpath('data/html_export_default.css'))
) as fobj:
template_css_data = fobj.read().decode()
@@ -1,6 +1,6 @@
import os
import sys
import pkg_resources
from importlib.resources import files
from lxml import etree
@@ -52,9 +52,8 @@ class LRFInput(InputFormatPlugin):
self.log.info('Converting XML to HTML...')
with open(pkg_resources.
resource_filename('ebook_converter',
'data/lrf.xsl')) as fobj:
with open(str(files('ebook_converter')
.joinpath('data/lrf.xsl'))) as fobj:
# TODO(gryf): change this nonsense to etree.parse() instead.
styledoc = etree.fromstring(fobj.read())
media_type = MediaType()
@@ -1,6 +1,6 @@
import glob
import os
import pkg_resources
from importlib.resources import files
import re
import textwrap
@@ -288,8 +288,8 @@ class RTFInput(InputFormatPlugin):
self.log.info('Converting XML to HTML...')
inline_class = InlineClass(self.log)
with open(pkg_resources.resource_filename('ebook_converter',
'data/rtf.xsl')) as fobj:
with open(str(files('ebook_converter')
.joinpath('data/rtf.xsl'))) as fobj:
styledoc = etree.fromstring(fobj.read())
extensions = {('calibre', 'inline-class'): inline_class}
transform = etree.XSLT(styledoc, extensions=extensions)
+4 -6
View File
@@ -1,7 +1,7 @@
import collections
import functools
import operator
import pkg_resources
from importlib.resources import files
import re
import urllib.parse
@@ -719,9 +719,8 @@ def commit_nav_toc(container, toc, lang=None, landmarks=None,
if previous_nav is not None:
root = previous_nav[1]
else:
with open(pkg_resources.
resource_filename('ebook_converter',
'data/new_nav.html')) as fobj:
with open(str(files('ebook_converter')
.joinpath('data/new_nav.html'))) as fobj:
root = container.parse_xhtml(fobj.read())
container.replace(tocname, root)
else:
@@ -874,8 +873,7 @@ def toc_to_html(toc, container, toc_name, title, lang=None):
E = ElementMaker(namespace=const.XHTML_NS, nsmap={None: const.XHTML_NS})
# TODO(gryf): revisit lack of css.
css_f = pkg_resources.resource_filename('ebook_converter',
'data/inline_toc_styles.css')
css_f = str(files('ebook_converter').joinpath('data/inline_toc_styles.css'))
html = E.html(E.head(E.title(title),
E.style(css_f, type='text/css')),
E.body(E.h2(title), E.ul(),
+2 -3
View File
@@ -2,7 +2,7 @@
CSS property propagation class.
"""
import os, re, logging, copy, unicodedata, numbers
import pkg_resources
from importlib.resources import files
from operator import itemgetter
from weakref import WeakKeyDictionary
from xml.dom import SyntaxErr as CSSSyntaxError
@@ -28,8 +28,7 @@ _html_css_stylesheet = None
def html_css_stylesheet():
global _html_css_stylesheet
if _html_css_stylesheet is None:
with open(pkg_resources.resource_filename('ebook_converter',
'data/html.css'), 'rb') as f:
with open(str(files('ebook_converter').joinpath('data/html.css')), 'rb') as f:
html_css = f.read().decode('utf-8')
_html_css_stylesheet = parseString(html_css, validate=False)
return _html_css_stylesheet
+2 -3
View File
@@ -1,6 +1,6 @@
from collections import namedtuple
import json
import pkg_resources
from importlib.resources import files
from ebook_converter.utils.localization import canonicalize_lang
@@ -16,8 +16,7 @@ ccodes, ccodemap, country_names = None, None, None
def get_codes():
global ccodes, ccodemap, country_names
if ccodes is None:
src = pkg_resources.resource_filename('ebook_converter',
'data/iso_3166-1.json')
src = str(files('ebook_converter').joinpath('data/iso_3166-1.json'))
with open(src, 'rb') as f:
db = json.load(f)
codes = set()
+2 -3
View File
@@ -10,7 +10,7 @@ import pickle
import re
import traceback
import pkg_resources
from importlib.resources import files as _resource_files
from ebook_converter.constants_old import config_dir
from ebook_converter.constants_old import filesystem_encoding
@@ -591,8 +591,7 @@ def exec_tweaks(path):
def default_tweaks_raw():
return pkg_resources.resource_filename('ebook_converter',
'data/default_tweaks.py')
return str(_resource_files('ebook_converter').joinpath('data/default_tweaks.py'))
def read_tweaks():
+2 -3
View File
@@ -1,5 +1,5 @@
import json
import pkg_resources
from importlib.resources import files
def get_lang():
@@ -39,8 +39,7 @@ def _load_iso639():
# excerpt form Calibre transform code which is executed during Calibre
# build).
if _iso639 is None:
src = pkg_resources.resource_filename('ebook_converter',
'data/iso_639-3.json')
src = str(files('ebook_converter').joinpath('data/iso_639-3.json'))
with open(src, 'rb') as f:
root = json.load(f)