mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-08-14 02:26:58 -07:00
Bump importlib-resources from 6.4.0 to 6.4.5 (#2394)
* Bump importlib-resources from 6.4.0 to 6.4.5 Bumps [importlib-resources](https://github.com/python/importlib_resources) from 6.4.0 to 6.4.5. - [Release notes](https://github.com/python/importlib_resources/releases) - [Changelog](https://github.com/python/importlib_resources/blob/main/NEWS.rst) - [Commits](https://github.com/python/importlib_resources/compare/v6.4.0...v6.4.5) --- updated-dependencies: - dependency-name: importlib-resources dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> * Update importlib-resources==6.4.5 --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> [skip ci]
This commit is contained in:
parent
f3a2c02e96
commit
01589cb8b0
41 changed files with 484 additions and 316 deletions
|
@ -2,15 +2,44 @@ import pathlib
|
|||
import functools
|
||||
|
||||
from typing import Dict, Union
|
||||
from typing import runtime_checkable
|
||||
from typing import Protocol
|
||||
|
||||
|
||||
####
|
||||
# from jaraco.path 3.4.1
|
||||
|
||||
FilesSpec = Dict[str, Union[str, bytes, 'FilesSpec']] # type: ignore
|
||||
# from jaraco.path 3.7.1
|
||||
|
||||
|
||||
def build(spec: FilesSpec, prefix=pathlib.Path()):
|
||||
class Symlink(str):
|
||||
"""
|
||||
A string indicating the target of a symlink.
|
||||
"""
|
||||
|
||||
|
||||
FilesSpec = Dict[str, Union[str, bytes, Symlink, 'FilesSpec']]
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class TreeMaker(Protocol):
|
||||
def __truediv__(self, *args, **kwargs): ... # pragma: no cover
|
||||
|
||||
def mkdir(self, **kwargs): ... # pragma: no cover
|
||||
|
||||
def write_text(self, content, **kwargs): ... # pragma: no cover
|
||||
|
||||
def write_bytes(self, content): ... # pragma: no cover
|
||||
|
||||
def symlink_to(self, target): ... # pragma: no cover
|
||||
|
||||
|
||||
def _ensure_tree_maker(obj: Union[str, TreeMaker]) -> TreeMaker:
|
||||
return obj if isinstance(obj, TreeMaker) else pathlib.Path(obj) # type: ignore[return-value]
|
||||
|
||||
|
||||
def build(
|
||||
spec: FilesSpec,
|
||||
prefix: Union[str, TreeMaker] = pathlib.Path(), # type: ignore[assignment]
|
||||
):
|
||||
"""
|
||||
Build a set of files/directories, as described by the spec.
|
||||
|
||||
|
@ -25,21 +54,25 @@ def build(spec: FilesSpec, prefix=pathlib.Path()):
|
|||
... "__init__.py": "",
|
||||
... },
|
||||
... "baz.py": "# Some code",
|
||||
... }
|
||||
... "bar.py": Symlink("baz.py"),
|
||||
... },
|
||||
... "bing": Symlink("foo"),
|
||||
... }
|
||||
>>> target = getfixture('tmp_path')
|
||||
>>> build(spec, target)
|
||||
>>> target.joinpath('foo/baz.py').read_text(encoding='utf-8')
|
||||
'# Some code'
|
||||
>>> target.joinpath('bing/bar.py').read_text(encoding='utf-8')
|
||||
'# Some code'
|
||||
"""
|
||||
for name, contents in spec.items():
|
||||
create(contents, pathlib.Path(prefix) / name)
|
||||
create(contents, _ensure_tree_maker(prefix) / name)
|
||||
|
||||
|
||||
@functools.singledispatch
|
||||
def create(content: Union[str, bytes, FilesSpec], path):
|
||||
path.mkdir(exist_ok=True)
|
||||
build(content, prefix=path) # type: ignore
|
||||
build(content, prefix=path) # type: ignore[arg-type]
|
||||
|
||||
|
||||
@create.register
|
||||
|
@ -52,5 +85,10 @@ def _(content: str, path):
|
|||
path.write_text(content, encoding='utf-8')
|
||||
|
||||
|
||||
@create.register
|
||||
def _(content: Symlink, path):
|
||||
path.symlink_to(content)
|
||||
|
||||
|
||||
# end from jaraco.path
|
||||
####
|
||||
|
|
|
@ -8,3 +8,6 @@ import_helper = try_import('import_helper') or from_test_support(
|
|||
'modules_setup', 'modules_cleanup', 'DirsOnSysPath'
|
||||
)
|
||||
os_helper = try_import('os_helper') or from_test_support('temp_dir')
|
||||
warnings_helper = try_import('warnings_helper') or from_test_support(
|
||||
'ignore_warnings', 'check_warnings'
|
||||
)
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -1 +0,0 @@
|
|||
Hello, UTF-8 world!
|
|
@ -1 +0,0 @@
|
|||
one resource
|
|
@ -1 +0,0 @@
|
|||
a resource
|
|
@ -1 +0,0 @@
|
|||
two resource
|
Binary file not shown.
Binary file not shown.
|
@ -1 +0,0 @@
|
|||
Hello, UTF-8 world!
|
|
@ -1,7 +1,6 @@
|
|||
import unittest
|
||||
import importlib_resources as resources
|
||||
|
||||
from . import data01
|
||||
from . import util
|
||||
|
||||
|
||||
|
@ -19,16 +18,17 @@ class ContentsTests:
|
|||
assert self.expected <= contents
|
||||
|
||||
|
||||
class ContentsDiskTests(ContentsTests, unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.data = data01
|
||||
class ContentsDiskTests(ContentsTests, util.DiskSetup, unittest.TestCase):
|
||||
pass
|
||||
|
||||
|
||||
class ContentsZipTests(ContentsTests, util.ZipSetup, unittest.TestCase):
|
||||
pass
|
||||
|
||||
|
||||
class ContentsNamespaceTests(ContentsTests, unittest.TestCase):
|
||||
class ContentsNamespaceTests(ContentsTests, util.DiskSetup, unittest.TestCase):
|
||||
MODULE = 'namespacedata01'
|
||||
|
||||
expected = {
|
||||
# no __init__ because of namespace design
|
||||
'binary.file',
|
||||
|
@ -36,8 +36,3 @@ class ContentsNamespaceTests(ContentsTests, unittest.TestCase):
|
|||
'utf-16.file',
|
||||
'utf-8.file',
|
||||
}
|
||||
|
||||
def setUp(self):
|
||||
from . import namespacedata01
|
||||
|
||||
self.data = namespacedata01
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
import os
|
||||
import pathlib
|
||||
import py_compile
|
||||
import shutil
|
||||
import textwrap
|
||||
import unittest
|
||||
import warnings
|
||||
|
@ -6,11 +10,8 @@ import contextlib
|
|||
|
||||
import importlib_resources as resources
|
||||
from ..abc import Traversable
|
||||
from . import data01
|
||||
from . import util
|
||||
from . import _path
|
||||
from .compat.py39 import os_helper
|
||||
from .compat.py312 import import_helper
|
||||
from .compat.py39 import os_helper, import_helper
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
|
@ -48,70 +49,146 @@ class FilesTests:
|
|||
resources.files(package=self.data)
|
||||
|
||||
|
||||
class OpenDiskTests(FilesTests, unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.data = data01
|
||||
class OpenDiskTests(FilesTests, util.DiskSetup, unittest.TestCase):
|
||||
pass
|
||||
|
||||
|
||||
class OpenZipTests(FilesTests, util.ZipSetup, unittest.TestCase):
|
||||
pass
|
||||
|
||||
|
||||
class OpenNamespaceTests(FilesTests, unittest.TestCase):
|
||||
def setUp(self):
|
||||
from . import namespacedata01
|
||||
class OpenNamespaceTests(FilesTests, util.DiskSetup, unittest.TestCase):
|
||||
MODULE = 'namespacedata01'
|
||||
|
||||
self.data = namespacedata01
|
||||
def test_non_paths_in_dunder_path(self):
|
||||
"""
|
||||
Non-path items in a namespace package's ``__path__`` are ignored.
|
||||
|
||||
As reported in python/importlib_resources#311, some tools
|
||||
like Setuptools, when creating editable packages, will inject
|
||||
non-paths into a namespace package's ``__path__``, a
|
||||
sentinel like
|
||||
``__editable__.sample_namespace-1.0.finder.__path_hook__``
|
||||
to cause the ``PathEntryFinder`` to be called when searching
|
||||
for packages. In that case, resources should still be loadable.
|
||||
"""
|
||||
import namespacedata01
|
||||
|
||||
namespacedata01.__path__.append(
|
||||
'__editable__.sample_namespace-1.0.finder.__path_hook__'
|
||||
)
|
||||
|
||||
resources.files(namespacedata01)
|
||||
|
||||
|
||||
class OpenNamespaceZipTests(FilesTests, util.ZipSetup, unittest.TestCase):
|
||||
ZIP_MODULE = 'namespacedata01'
|
||||
|
||||
|
||||
class SiteDir:
|
||||
def setUp(self):
|
||||
self.fixtures = contextlib.ExitStack()
|
||||
self.addCleanup(self.fixtures.close)
|
||||
self.site_dir = self.fixtures.enter_context(os_helper.temp_dir())
|
||||
self.fixtures.enter_context(import_helper.DirsOnSysPath(self.site_dir))
|
||||
self.fixtures.enter_context(import_helper.isolated_modules())
|
||||
class DirectSpec:
|
||||
"""
|
||||
Override behavior of ModuleSetup to write a full spec directly.
|
||||
"""
|
||||
|
||||
MODULE = 'unused'
|
||||
|
||||
def load_fixture(self, name):
|
||||
self.tree_on_path(self.spec)
|
||||
|
||||
|
||||
class ModulesFilesTests(SiteDir, unittest.TestCase):
|
||||
class ModulesFiles:
|
||||
spec = {
|
||||
'mod.py': '',
|
||||
'res.txt': 'resources are the best',
|
||||
}
|
||||
|
||||
def test_module_resources(self):
|
||||
"""
|
||||
A module can have resources found adjacent to the module.
|
||||
"""
|
||||
spec = {
|
||||
'mod.py': '',
|
||||
'res.txt': 'resources are the best',
|
||||
}
|
||||
_path.build(spec, self.site_dir)
|
||||
import mod
|
||||
import mod # type: ignore[import-not-found]
|
||||
|
||||
actual = resources.files(mod).joinpath('res.txt').read_text(encoding='utf-8')
|
||||
assert actual == spec['res.txt']
|
||||
assert actual == self.spec['res.txt']
|
||||
|
||||
|
||||
class ImplicitContextFilesTests(SiteDir, unittest.TestCase):
|
||||
def test_implicit_files(self):
|
||||
class ModuleFilesDiskTests(DirectSpec, util.DiskSetup, ModulesFiles, unittest.TestCase):
|
||||
pass
|
||||
|
||||
|
||||
class ModuleFilesZipTests(DirectSpec, util.ZipSetup, ModulesFiles, unittest.TestCase):
|
||||
pass
|
||||
|
||||
|
||||
class ImplicitContextFiles:
|
||||
set_val = textwrap.dedent(
|
||||
f"""
|
||||
import {resources.__name__} as res
|
||||
val = res.files().joinpath('res.txt').read_text(encoding='utf-8')
|
||||
"""
|
||||
)
|
||||
spec = {
|
||||
'somepkg': {
|
||||
'__init__.py': set_val,
|
||||
'submod.py': set_val,
|
||||
'res.txt': 'resources are the best',
|
||||
},
|
||||
'frozenpkg': {
|
||||
'__init__.py': set_val.replace(resources.__name__, 'c_resources'),
|
||||
'res.txt': 'resources are the best',
|
||||
},
|
||||
}
|
||||
|
||||
def test_implicit_files_package(self):
|
||||
"""
|
||||
Without any parameter, files() will infer the location as the caller.
|
||||
"""
|
||||
spec = {
|
||||
'somepkg': {
|
||||
'__init__.py': textwrap.dedent(
|
||||
"""
|
||||
import importlib_resources as res
|
||||
val = res.files().joinpath('res.txt').read_text(encoding='utf-8')
|
||||
"""
|
||||
),
|
||||
'res.txt': 'resources are the best',
|
||||
},
|
||||
}
|
||||
_path.build(spec, self.site_dir)
|
||||
assert importlib.import_module('somepkg').val == 'resources are the best'
|
||||
|
||||
def test_implicit_files_submodule(self):
|
||||
"""
|
||||
Without any parameter, files() will infer the location as the caller.
|
||||
"""
|
||||
assert importlib.import_module('somepkg.submod').val == 'resources are the best'
|
||||
|
||||
def _compile_importlib(self):
|
||||
"""
|
||||
Make a compiled-only copy of the importlib resources package.
|
||||
"""
|
||||
bin_site = self.fixtures.enter_context(os_helper.temp_dir())
|
||||
c_resources = pathlib.Path(bin_site, 'c_resources')
|
||||
sources = pathlib.Path(resources.__file__).parent
|
||||
shutil.copytree(sources, c_resources, ignore=lambda *_: ['__pycache__'])
|
||||
|
||||
for dirpath, _, filenames in os.walk(c_resources):
|
||||
for filename in filenames:
|
||||
source_path = pathlib.Path(dirpath) / filename
|
||||
cfile = source_path.with_suffix('.pyc')
|
||||
py_compile.compile(source_path, cfile)
|
||||
pathlib.Path.unlink(source_path)
|
||||
self.fixtures.enter_context(import_helper.DirsOnSysPath(bin_site))
|
||||
|
||||
def test_implicit_files_with_compiled_importlib(self):
|
||||
"""
|
||||
Caller detection works for compiled-only resources module.
|
||||
|
||||
python/cpython#123085
|
||||
"""
|
||||
self._compile_importlib()
|
||||
assert importlib.import_module('frozenpkg').val == 'resources are the best'
|
||||
|
||||
|
||||
class ImplicitContextFilesDiskTests(
|
||||
DirectSpec, util.DiskSetup, ImplicitContextFiles, unittest.TestCase
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
class ImplicitContextFilesZipTests(
|
||||
DirectSpec, util.ZipSetup, ImplicitContextFiles, unittest.TestCase
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
@ -1,31 +1,38 @@
|
|||
import unittest
|
||||
import os
|
||||
import contextlib
|
||||
import importlib
|
||||
|
||||
try:
|
||||
from test.support.warnings_helper import ignore_warnings, check_warnings
|
||||
except ImportError:
|
||||
# older Python versions
|
||||
from test.support import ignore_warnings, check_warnings
|
||||
from .compat.py39 import warnings_helper
|
||||
|
||||
import importlib_resources as resources
|
||||
|
||||
from . import util
|
||||
|
||||
# Since the functional API forwards to Traversable, we only test
|
||||
# filesystem resources here -- not zip files, namespace packages etc.
|
||||
# We do test for two kinds of Anchor, though.
|
||||
|
||||
|
||||
class StringAnchorMixin:
|
||||
anchor01 = 'importlib_resources.tests.data01'
|
||||
anchor02 = 'importlib_resources.tests.data02'
|
||||
anchor01 = 'data01'
|
||||
anchor02 = 'data02'
|
||||
|
||||
|
||||
class ModuleAnchorMixin:
|
||||
from . import data01 as anchor01
|
||||
from . import data02 as anchor02
|
||||
@property
|
||||
def anchor01(self):
|
||||
return importlib.import_module('data01')
|
||||
|
||||
@property
|
||||
def anchor02(self):
|
||||
return importlib.import_module('data02')
|
||||
|
||||
|
||||
class FunctionalAPIBase:
|
||||
class FunctionalAPIBase(util.DiskSetup):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.load_fixture('data02')
|
||||
|
||||
def _gen_resourcetxt_path_parts(self):
|
||||
"""Yield various names of a text file in anchor02, each in a subTest"""
|
||||
for path_parts in (
|
||||
|
@ -36,6 +43,12 @@ class FunctionalAPIBase:
|
|||
with self.subTest(path_parts=path_parts):
|
||||
yield path_parts
|
||||
|
||||
def assertEndsWith(self, string, suffix):
|
||||
"""Assert that `string` ends with `suffix`.
|
||||
|
||||
Used to ignore an architecture-specific UTF-16 byte-order mark."""
|
||||
self.assertEqual(string[-len(suffix) :], suffix)
|
||||
|
||||
def test_read_text(self):
|
||||
self.assertEqual(
|
||||
resources.read_text(self.anchor01, 'utf-8.file'),
|
||||
|
@ -76,13 +89,13 @@ class FunctionalAPIBase:
|
|||
),
|
||||
'\x00\x01\x02\x03',
|
||||
)
|
||||
self.assertEqual(
|
||||
self.assertEndsWith( # ignore the BOM
|
||||
resources.read_text(
|
||||
self.anchor01,
|
||||
'utf-16.file',
|
||||
errors='backslashreplace',
|
||||
),
|
||||
'Hello, UTF-16 world!\n'.encode('utf-16').decode(
|
||||
'Hello, UTF-16 world!\n'.encode('utf-16-le').decode(
|
||||
errors='backslashreplace',
|
||||
),
|
||||
)
|
||||
|
@ -128,9 +141,9 @@ class FunctionalAPIBase:
|
|||
'utf-16.file',
|
||||
errors='backslashreplace',
|
||||
) as f:
|
||||
self.assertEqual(
|
||||
self.assertEndsWith( # ignore the BOM
|
||||
f.read(),
|
||||
'Hello, UTF-16 world!\n'.encode('utf-16').decode(
|
||||
'Hello, UTF-16 world!\n'.encode('utf-16-le').decode(
|
||||
errors='backslashreplace',
|
||||
),
|
||||
)
|
||||
|
@ -163,32 +176,32 @@ class FunctionalAPIBase:
|
|||
self.assertTrue(is_resource(self.anchor02, *path_parts))
|
||||
|
||||
def test_contents(self):
|
||||
with check_warnings((".*contents.*", DeprecationWarning)):
|
||||
with warnings_helper.check_warnings((".*contents.*", DeprecationWarning)):
|
||||
c = resources.contents(self.anchor01)
|
||||
self.assertGreaterEqual(
|
||||
set(c),
|
||||
{'utf-8.file', 'utf-16.file', 'binary.file', 'subdirectory'},
|
||||
)
|
||||
with contextlib.ExitStack() as cm:
|
||||
cm.enter_context(self.assertRaises(OSError))
|
||||
cm.enter_context(check_warnings((".*contents.*", DeprecationWarning)))
|
||||
|
||||
with self.assertRaises(OSError), warnings_helper.check_warnings((
|
||||
".*contents.*",
|
||||
DeprecationWarning,
|
||||
)):
|
||||
list(resources.contents(self.anchor01, 'utf-8.file'))
|
||||
|
||||
for path_parts in self._gen_resourcetxt_path_parts():
|
||||
with contextlib.ExitStack() as cm:
|
||||
cm.enter_context(self.assertRaises(OSError))
|
||||
cm.enter_context(check_warnings((".*contents.*", DeprecationWarning)))
|
||||
|
||||
with self.assertRaises(OSError), warnings_helper.check_warnings((
|
||||
".*contents.*",
|
||||
DeprecationWarning,
|
||||
)):
|
||||
list(resources.contents(self.anchor01, *path_parts))
|
||||
with check_warnings((".*contents.*", DeprecationWarning)):
|
||||
with warnings_helper.check_warnings((".*contents.*", DeprecationWarning)):
|
||||
c = resources.contents(self.anchor01, 'subdirectory')
|
||||
self.assertGreaterEqual(
|
||||
set(c),
|
||||
{'binary.file'},
|
||||
)
|
||||
|
||||
@ignore_warnings(category=DeprecationWarning)
|
||||
@warnings_helper.ignore_warnings(category=DeprecationWarning)
|
||||
def test_common_errors(self):
|
||||
for func in (
|
||||
resources.read_text,
|
||||
|
@ -227,16 +240,16 @@ class FunctionalAPIBase:
|
|||
|
||||
|
||||
class FunctionalAPITest_StringAnchor(
|
||||
unittest.TestCase,
|
||||
FunctionalAPIBase,
|
||||
StringAnchorMixin,
|
||||
FunctionalAPIBase,
|
||||
unittest.TestCase,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
class FunctionalAPITest_ModuleAnchor(
|
||||
unittest.TestCase,
|
||||
FunctionalAPIBase,
|
||||
ModuleAnchorMixin,
|
||||
FunctionalAPIBase,
|
||||
unittest.TestCase,
|
||||
):
|
||||
pass
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import unittest
|
||||
|
||||
import importlib_resources as resources
|
||||
from . import data01
|
||||
from . import util
|
||||
|
||||
|
||||
|
@ -65,16 +64,12 @@ class OpenTests:
|
|||
target.open(encoding='utf-8')
|
||||
|
||||
|
||||
class OpenDiskTests(OpenTests, unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.data = data01
|
||||
class OpenDiskTests(OpenTests, util.DiskSetup, unittest.TestCase):
|
||||
pass
|
||||
|
||||
|
||||
class OpenDiskNamespaceTests(OpenTests, unittest.TestCase):
|
||||
def setUp(self):
|
||||
from . import namespacedata01
|
||||
|
||||
self.data = namespacedata01
|
||||
class OpenDiskNamespaceTests(OpenTests, util.DiskSetup, unittest.TestCase):
|
||||
MODULE = 'namespacedata01'
|
||||
|
||||
|
||||
class OpenZipTests(OpenTests, util.ZipSetup, unittest.TestCase):
|
||||
|
@ -82,7 +77,7 @@ class OpenZipTests(OpenTests, util.ZipSetup, unittest.TestCase):
|
|||
|
||||
|
||||
class OpenNamespaceZipTests(OpenTests, util.ZipSetup, unittest.TestCase):
|
||||
ZIP_MODULE = 'namespacedata01'
|
||||
MODULE = 'namespacedata01'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -3,7 +3,6 @@ import pathlib
|
|||
import unittest
|
||||
|
||||
import importlib_resources as resources
|
||||
from . import data01
|
||||
from . import util
|
||||
|
||||
|
||||
|
@ -25,9 +24,7 @@ class PathTests:
|
|||
self.assertEqual('Hello, UTF-8 world!\n', path.read_text(encoding='utf-8'))
|
||||
|
||||
|
||||
class PathDiskTests(PathTests, unittest.TestCase):
|
||||
data = data01
|
||||
|
||||
class PathDiskTests(PathTests, util.DiskSetup, unittest.TestCase):
|
||||
def test_natural_path(self):
|
||||
"""
|
||||
Guarantee the internal implementation detail that
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import unittest
|
||||
import importlib_resources as resources
|
||||
|
||||
from . import data01
|
||||
from . import util
|
||||
from importlib import import_module
|
||||
|
||||
|
@ -52,8 +51,8 @@ class ReadTests:
|
|||
)
|
||||
|
||||
|
||||
class ReadDiskTests(ReadTests, unittest.TestCase):
|
||||
data = data01
|
||||
class ReadDiskTests(ReadTests, util.DiskSetup, unittest.TestCase):
|
||||
pass
|
||||
|
||||
|
||||
class ReadZipTests(ReadTests, util.ZipSetup, unittest.TestCase):
|
||||
|
@ -69,15 +68,12 @@ class ReadZipTests(ReadTests, util.ZipSetup, unittest.TestCase):
|
|||
self.assertEqual(result, bytes(range(4, 8)))
|
||||
|
||||
|
||||
class ReadNamespaceTests(ReadTests, unittest.TestCase):
|
||||
def setUp(self):
|
||||
from . import namespacedata01
|
||||
|
||||
self.data = namespacedata01
|
||||
class ReadNamespaceTests(ReadTests, util.DiskSetup, unittest.TestCase):
|
||||
MODULE = 'namespacedata01'
|
||||
|
||||
|
||||
class ReadNamespaceZipTests(ReadTests, util.ZipSetup, unittest.TestCase):
|
||||
ZIP_MODULE = 'namespacedata01'
|
||||
MODULE = 'namespacedata01'
|
||||
|
||||
def test_read_submodule_resource(self):
|
||||
submodule = import_module('namespacedata01.subdirectory')
|
||||
|
|
|
@ -1,16 +1,21 @@
|
|||
import os.path
|
||||
import sys
|
||||
import pathlib
|
||||
import unittest
|
||||
|
||||
from importlib import import_module
|
||||
from importlib_resources.readers import MultiplexedPath, NamespaceReader
|
||||
|
||||
from . import util
|
||||
|
||||
class MultiplexedPathTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.folder = pathlib.Path(__file__).parent / 'namespacedata01'
|
||||
|
||||
class MultiplexedPathTest(util.DiskSetup, unittest.TestCase):
|
||||
MODULE = 'namespacedata01'
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.folder = pathlib.Path(self.data.__path__[0])
|
||||
self.data01 = pathlib.Path(self.load_fixture('data01').__file__).parent
|
||||
self.data02 = pathlib.Path(self.load_fixture('data02').__file__).parent
|
||||
|
||||
def test_init_no_paths(self):
|
||||
with self.assertRaises(FileNotFoundError):
|
||||
|
@ -31,9 +36,8 @@ class MultiplexedPathTest(unittest.TestCase):
|
|||
)
|
||||
|
||||
def test_iterdir_duplicate(self):
|
||||
data01 = pathlib.Path(__file__).parent.joinpath('data01')
|
||||
contents = {
|
||||
path.name for path in MultiplexedPath(self.folder, data01).iterdir()
|
||||
path.name for path in MultiplexedPath(self.folder, self.data01).iterdir()
|
||||
}
|
||||
for remove in ('__pycache__', '__init__.pyc'):
|
||||
try:
|
||||
|
@ -61,9 +65,8 @@ class MultiplexedPathTest(unittest.TestCase):
|
|||
path.open()
|
||||
|
||||
def test_join_path(self):
|
||||
data01 = pathlib.Path(__file__).parent.joinpath('data01')
|
||||
prefix = str(data01.parent)
|
||||
path = MultiplexedPath(self.folder, data01)
|
||||
prefix = str(self.folder.parent)
|
||||
path = MultiplexedPath(self.folder, self.data01)
|
||||
self.assertEqual(
|
||||
str(path.joinpath('binary.file'))[len(prefix) + 1 :],
|
||||
os.path.join('namespacedata01', 'binary.file'),
|
||||
|
@ -83,10 +86,8 @@ class MultiplexedPathTest(unittest.TestCase):
|
|||
assert not path.joinpath('imaginary/foo.py').exists()
|
||||
|
||||
def test_join_path_common_subdir(self):
|
||||
data01 = pathlib.Path(__file__).parent.joinpath('data01')
|
||||
data02 = pathlib.Path(__file__).parent.joinpath('data02')
|
||||
prefix = str(data01.parent)
|
||||
path = MultiplexedPath(data01, data02)
|
||||
prefix = str(self.data02.parent)
|
||||
path = MultiplexedPath(self.data01, self.data02)
|
||||
self.assertIsInstance(path.joinpath('subdirectory'), MultiplexedPath)
|
||||
self.assertEqual(
|
||||
str(path.joinpath('subdirectory', 'subsubdir'))[len(prefix) + 1 :],
|
||||
|
@ -106,16 +107,8 @@ class MultiplexedPathTest(unittest.TestCase):
|
|||
)
|
||||
|
||||
|
||||
class NamespaceReaderTest(unittest.TestCase):
|
||||
site_dir = str(pathlib.Path(__file__).parent)
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
sys.path.append(cls.site_dir)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
sys.path.remove(cls.site_dir)
|
||||
class NamespaceReaderTest(util.DiskSetup, unittest.TestCase):
|
||||
MODULE = 'namespacedata01'
|
||||
|
||||
def test_init_error(self):
|
||||
with self.assertRaises(ValueError):
|
||||
|
@ -125,7 +118,7 @@ class NamespaceReaderTest(unittest.TestCase):
|
|||
namespacedata01 = import_module('namespacedata01')
|
||||
reader = NamespaceReader(namespacedata01.__spec__.submodule_search_locations)
|
||||
|
||||
root = os.path.abspath(os.path.join(__file__, '..', 'namespacedata01'))
|
||||
root = self.data.__path__[0]
|
||||
self.assertEqual(
|
||||
reader.resource_path('binary.file'), os.path.join(root, 'binary.file')
|
||||
)
|
||||
|
@ -134,9 +127,8 @@ class NamespaceReaderTest(unittest.TestCase):
|
|||
)
|
||||
|
||||
def test_files(self):
|
||||
namespacedata01 = import_module('namespacedata01')
|
||||
reader = NamespaceReader(namespacedata01.__spec__.submodule_search_locations)
|
||||
root = os.path.abspath(os.path.join(__file__, '..', 'namespacedata01'))
|
||||
reader = NamespaceReader(self.data.__spec__.submodule_search_locations)
|
||||
root = self.data.__path__[0]
|
||||
self.assertIsInstance(reader.files(), MultiplexedPath)
|
||||
self.assertEqual(repr(reader.files()), f"MultiplexedPath('{root}')")
|
||||
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
import sys
|
||||
import unittest
|
||||
import importlib_resources as resources
|
||||
import pathlib
|
||||
|
||||
from . import data01
|
||||
from . import util
|
||||
from importlib import import_module
|
||||
|
||||
|
@ -25,9 +22,8 @@ class ResourceTests:
|
|||
self.assertTrue(target.is_dir())
|
||||
|
||||
|
||||
class ResourceDiskTests(ResourceTests, unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.data = data01
|
||||
class ResourceDiskTests(ResourceTests, util.DiskSetup, unittest.TestCase):
|
||||
pass
|
||||
|
||||
|
||||
class ResourceZipTests(ResourceTests, util.ZipSetup, unittest.TestCase):
|
||||
|
@ -38,33 +34,39 @@ def names(traversable):
|
|||
return {item.name for item in traversable.iterdir()}
|
||||
|
||||
|
||||
class ResourceLoaderTests(unittest.TestCase):
|
||||
class ResourceLoaderTests(util.DiskSetup, unittest.TestCase):
|
||||
def test_resource_contents(self):
|
||||
package = util.create_package(
|
||||
file=data01, path=data01.__file__, contents=['A', 'B', 'C']
|
||||
file=self.data, path=self.data.__file__, contents=['A', 'B', 'C']
|
||||
)
|
||||
self.assertEqual(names(resources.files(package)), {'A', 'B', 'C'})
|
||||
|
||||
def test_is_file(self):
|
||||
package = util.create_package(
|
||||
file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F']
|
||||
file=self.data,
|
||||
path=self.data.__file__,
|
||||
contents=['A', 'B', 'C', 'D/E', 'D/F'],
|
||||
)
|
||||
self.assertTrue(resources.files(package).joinpath('B').is_file())
|
||||
|
||||
def test_is_dir(self):
|
||||
package = util.create_package(
|
||||
file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F']
|
||||
file=self.data,
|
||||
path=self.data.__file__,
|
||||
contents=['A', 'B', 'C', 'D/E', 'D/F'],
|
||||
)
|
||||
self.assertTrue(resources.files(package).joinpath('D').is_dir())
|
||||
|
||||
def test_resource_missing(self):
|
||||
package = util.create_package(
|
||||
file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F']
|
||||
file=self.data,
|
||||
path=self.data.__file__,
|
||||
contents=['A', 'B', 'C', 'D/E', 'D/F'],
|
||||
)
|
||||
self.assertFalse(resources.files(package).joinpath('Z').is_file())
|
||||
|
||||
|
||||
class ResourceCornerCaseTests(unittest.TestCase):
|
||||
class ResourceCornerCaseTests(util.DiskSetup, unittest.TestCase):
|
||||
def test_package_has_no_reader_fallback(self):
|
||||
"""
|
||||
Test odd ball packages which:
|
||||
|
@ -73,7 +75,7 @@ class ResourceCornerCaseTests(unittest.TestCase):
|
|||
# 3. Are not in a zip file
|
||||
"""
|
||||
module = util.create_package(
|
||||
file=data01, path=data01.__file__, contents=['A', 'B', 'C']
|
||||
file=self.data, path=self.data.__file__, contents=['A', 'B', 'C']
|
||||
)
|
||||
# Give the module a dummy loader.
|
||||
module.__loader__ = object()
|
||||
|
@ -84,9 +86,7 @@ class ResourceCornerCaseTests(unittest.TestCase):
|
|||
self.assertFalse(resources.files(module).joinpath('A').is_file())
|
||||
|
||||
|
||||
class ResourceFromZipsTest01(util.ZipSetupBase, unittest.TestCase):
|
||||
ZIP_MODULE = 'data01'
|
||||
|
||||
class ResourceFromZipsTest01(util.ZipSetup, unittest.TestCase):
|
||||
def test_is_submodule_resource(self):
|
||||
submodule = import_module('data01.subdirectory')
|
||||
self.assertTrue(resources.files(submodule).joinpath('binary.file').is_file())
|
||||
|
@ -117,8 +117,8 @@ class ResourceFromZipsTest01(util.ZipSetupBase, unittest.TestCase):
|
|||
assert not data.parent.exists()
|
||||
|
||||
|
||||
class ResourceFromZipsTest02(util.ZipSetupBase, unittest.TestCase):
|
||||
ZIP_MODULE = 'data02'
|
||||
class ResourceFromZipsTest02(util.ZipSetup, unittest.TestCase):
|
||||
MODULE = 'data02'
|
||||
|
||||
def test_unrelated_contents(self):
|
||||
"""
|
||||
|
@ -135,7 +135,7 @@ class ResourceFromZipsTest02(util.ZipSetupBase, unittest.TestCase):
|
|||
)
|
||||
|
||||
|
||||
class DeletingZipsTest(util.ZipSetupBase, unittest.TestCase):
|
||||
class DeletingZipsTest(util.ZipSetup, unittest.TestCase):
|
||||
"""Having accessed resources in a zip file should not keep an open
|
||||
reference to the zip.
|
||||
"""
|
||||
|
@ -217,24 +217,20 @@ class ResourceFromNamespaceTests:
|
|||
self.assertEqual(contents, {'binary.file'})
|
||||
|
||||
|
||||
class ResourceFromNamespaceDiskTests(ResourceFromNamespaceTests, unittest.TestCase):
|
||||
site_dir = str(pathlib.Path(__file__).parent)
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
sys.path.append(cls.site_dir)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
sys.path.remove(cls.site_dir)
|
||||
|
||||
|
||||
class ResourceFromNamespaceZipTests(
|
||||
util.ZipSetupBase,
|
||||
class ResourceFromNamespaceDiskTests(
|
||||
util.DiskSetup,
|
||||
ResourceFromNamespaceTests,
|
||||
unittest.TestCase,
|
||||
):
|
||||
ZIP_MODULE = 'namespacedata01'
|
||||
MODULE = 'namespacedata01'
|
||||
|
||||
|
||||
class ResourceFromNamespaceZipTests(
|
||||
util.ZipSetup,
|
||||
ResourceFromNamespaceTests,
|
||||
unittest.TestCase,
|
||||
):
|
||||
MODULE = 'namespacedata01'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -6,10 +6,10 @@ import types
|
|||
import pathlib
|
||||
import contextlib
|
||||
|
||||
from . import data01
|
||||
from ..abc import ResourceReader
|
||||
from .compat.py39 import import_helper, os_helper
|
||||
from . import zip as zip_
|
||||
from . import _path
|
||||
|
||||
|
||||
from importlib.machinery import ModuleSpec
|
||||
|
@ -68,7 +68,7 @@ def create_package(file=None, path=None, is_package=True, contents=()):
|
|||
)
|
||||
|
||||
|
||||
class CommonTests(metaclass=abc.ABCMeta):
|
||||
class CommonTestsBase(metaclass=abc.ABCMeta):
|
||||
"""
|
||||
Tests shared by test_open, test_path, and test_read.
|
||||
"""
|
||||
|
@ -84,34 +84,34 @@ class CommonTests(metaclass=abc.ABCMeta):
|
|||
"""
|
||||
Passing in the package name should succeed.
|
||||
"""
|
||||
self.execute(data01.__name__, 'utf-8.file')
|
||||
self.execute(self.data.__name__, 'utf-8.file')
|
||||
|
||||
def test_package_object(self):
|
||||
"""
|
||||
Passing in the package itself should succeed.
|
||||
"""
|
||||
self.execute(data01, 'utf-8.file')
|
||||
self.execute(self.data, 'utf-8.file')
|
||||
|
||||
def test_string_path(self):
|
||||
"""
|
||||
Passing in a string for the path should succeed.
|
||||
"""
|
||||
path = 'utf-8.file'
|
||||
self.execute(data01, path)
|
||||
self.execute(self.data, path)
|
||||
|
||||
def test_pathlib_path(self):
|
||||
"""
|
||||
Passing in a pathlib.PurePath object for the path should succeed.
|
||||
"""
|
||||
path = pathlib.PurePath('utf-8.file')
|
||||
self.execute(data01, path)
|
||||
self.execute(self.data, path)
|
||||
|
||||
def test_importing_module_as_side_effect(self):
|
||||
"""
|
||||
The anchor package can already be imported.
|
||||
"""
|
||||
del sys.modules[data01.__name__]
|
||||
self.execute(data01.__name__, 'utf-8.file')
|
||||
del sys.modules[self.data.__name__]
|
||||
self.execute(self.data.__name__, 'utf-8.file')
|
||||
|
||||
def test_missing_path(self):
|
||||
"""
|
||||
|
@ -141,24 +141,66 @@ class CommonTests(metaclass=abc.ABCMeta):
|
|||
self.execute(package, 'utf-8.file')
|
||||
|
||||
|
||||
class ZipSetupBase:
|
||||
ZIP_MODULE = 'data01'
|
||||
fixtures = dict(
|
||||
data01={
|
||||
'__init__.py': '',
|
||||
'binary.file': bytes(range(4)),
|
||||
'utf-16.file': 'Hello, UTF-16 world!\n'.encode('utf-16'),
|
||||
'utf-8.file': 'Hello, UTF-8 world!\n'.encode('utf-8'),
|
||||
'subdirectory': {
|
||||
'__init__.py': '',
|
||||
'binary.file': bytes(range(4, 8)),
|
||||
},
|
||||
},
|
||||
data02={
|
||||
'__init__.py': '',
|
||||
'one': {'__init__.py': '', 'resource1.txt': 'one resource'},
|
||||
'two': {'__init__.py': '', 'resource2.txt': 'two resource'},
|
||||
'subdirectory': {'subsubdir': {'resource.txt': 'a resource'}},
|
||||
},
|
||||
namespacedata01={
|
||||
'binary.file': bytes(range(4)),
|
||||
'utf-16.file': 'Hello, UTF-16 world!\n'.encode('utf-16'),
|
||||
'utf-8.file': 'Hello, UTF-8 world!\n'.encode('utf-8'),
|
||||
'subdirectory': {
|
||||
'binary.file': bytes(range(12, 16)),
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class ModuleSetup:
|
||||
def setUp(self):
|
||||
self.fixtures = contextlib.ExitStack()
|
||||
self.addCleanup(self.fixtures.close)
|
||||
|
||||
self.fixtures.enter_context(import_helper.isolated_modules())
|
||||
self.data = self.load_fixture(self.MODULE)
|
||||
|
||||
def load_fixture(self, module):
|
||||
self.tree_on_path({module: fixtures[module]})
|
||||
return importlib.import_module(module)
|
||||
|
||||
|
||||
class ZipSetup(ModuleSetup):
|
||||
MODULE = 'data01'
|
||||
|
||||
def tree_on_path(self, spec):
|
||||
temp_dir = self.fixtures.enter_context(os_helper.temp_dir())
|
||||
modules = pathlib.Path(temp_dir) / 'zipped modules.zip'
|
||||
src_path = pathlib.Path(__file__).parent.joinpath(self.ZIP_MODULE)
|
||||
self.fixtures.enter_context(
|
||||
import_helper.DirsOnSysPath(str(zip_.make_zip_file(src_path, modules)))
|
||||
import_helper.DirsOnSysPath(str(zip_.make_zip_file(spec, modules)))
|
||||
)
|
||||
|
||||
self.data = importlib.import_module(self.ZIP_MODULE)
|
||||
|
||||
class DiskSetup(ModuleSetup):
|
||||
MODULE = 'data01'
|
||||
|
||||
def tree_on_path(self, spec):
|
||||
temp_dir = self.fixtures.enter_context(os_helper.temp_dir())
|
||||
_path.build(spec, pathlib.Path(temp_dir))
|
||||
self.fixtures.enter_context(import_helper.DirsOnSysPath(temp_dir))
|
||||
|
||||
|
||||
class ZipSetup(ZipSetupBase):
|
||||
class CommonTests(DiskSetup, CommonTestsBase):
|
||||
pass
|
||||
|
|
|
@ -2,31 +2,25 @@
|
|||
Generate zip test data files.
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
import os
|
||||
import pathlib
|
||||
import zipfile
|
||||
|
||||
import zipp
|
||||
|
||||
|
||||
def make_zip_file(src, dst):
|
||||
def make_zip_file(tree, dst):
|
||||
"""
|
||||
Zip the files in src into a new zipfile at dst.
|
||||
Zip the files in tree into a new zipfile at dst.
|
||||
"""
|
||||
with zipfile.ZipFile(dst, 'w') as zf:
|
||||
for src_path, rel in walk(src):
|
||||
dst_name = src.name / pathlib.PurePosixPath(rel.as_posix())
|
||||
zf.write(src_path, dst_name)
|
||||
for name, contents in walk(tree):
|
||||
zf.writestr(name, contents)
|
||||
zipp.CompleteDirs.inject(zf)
|
||||
return dst
|
||||
|
||||
|
||||
def walk(datapath):
|
||||
for dirpath, dirnames, filenames in os.walk(datapath):
|
||||
with contextlib.suppress(ValueError):
|
||||
dirnames.remove('__pycache__')
|
||||
for filename in filenames:
|
||||
res = pathlib.Path(dirpath) / filename
|
||||
rel = res.relative_to(datapath)
|
||||
yield res, rel
|
||||
def walk(tree, prefix=''):
|
||||
for name, contents in tree.items():
|
||||
if isinstance(contents, dict):
|
||||
yield from walk(contents, prefix=f'{prefix}{name}/')
|
||||
else:
|
||||
yield f'{prefix}{name}', contents
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue