Skip to content

Commit

Permalink
Merge branch 'issue51' into 'master'
Browse files Browse the repository at this point in the history
Back port latest changes to importlib.resources

Closes python#51

See merge request python-devs/importlib_resources!55
  • Loading branch information
warsaw committed Mar 27, 2018
2 parents e421311 + 1af55e3 commit bd5afac
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
5 changes: 3 additions & 2 deletions importlib_resources/docs/using.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ directories, the model still holds. So zip files can contain packages and
resources, as could databases or other storage medium. In fact, while
``importlib_resources`` supports physical file systems and zip files by
default, anything that can be loaded with a Python import system `loader`_ can
provide resources, as long as the loader implements the :ref:`ResourceReader
<abc>` abstract base class.
provide resources, as long as the loader implements the `ResourceReader`_
abstract base class.


Example
Expand Down Expand Up @@ -172,3 +172,4 @@ manager.
.. _`pkg_resources API`: http://setuptools.readthedocs.io/en/latest/pkg_resources.html#basic-resource-access
.. _`loader`: https://docs.python.org/3/reference/import.html#finders-and-loaders
.. _`ResourceReader`: https://docs.python.org/3.7/library/importlib.html#importlib.abc.ResourceReader
14 changes: 12 additions & 2 deletions importlib_resources/tests/test_read.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import unittest

import importlib_resources as resources

from . import data01
from . import util
from importlib import import_module


class CommonBinaryTests(util.CommonTests, unittest.TestCase):
Expand Down Expand Up @@ -46,7 +47,16 @@ class ReadDiskTests(ReadTests, unittest.TestCase):


class ReadZipTests(ReadTests, util.ZipSetup, unittest.TestCase):
pass
def test_read_submodule_resource(self):
submodule = import_module('ziptestdata.subdirectory')
result = resources.read_binary(
submodule, 'binary.file')
self.assertEqual(result, b'\0\1\2\3')

def test_read_submodule_resource_by_name(self):
result = resources.read_binary(
'ziptestdata.subdirectory', 'binary.file')
self.assertEqual(result, b'\0\1\2\3')


if __name__ == '__main__':
Expand Down
31 changes: 28 additions & 3 deletions importlib_resources/tests/test_resource.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import sys
import unittest

import importlib_resources as resources

from . import data01
from . import zipdata02
from . import zipdata01, zipdata02
from . import util
from importlib import import_module


class ResourceTests:
Expand Down Expand Up @@ -94,7 +95,31 @@ def test_package_has_no_reader_fallback(self):
self.assertFalse(resources.is_resource(module, 'A'))


class ResourceFromZipsTest(util.ZipSetupBase, unittest.TestCase):
class ResourceFromZipsTest01(util.ZipSetupBase, unittest.TestCase):
ZIP_MODULE = zipdata01 # type: ignore

def test_is_submodule_resource(self):
submodule = import_module('ziptestdata.subdirectory')
self.assertTrue(
resources.is_resource(submodule, 'binary.file'))

def test_read_submodule_resource_by_name(self):
self.assertTrue(
resources.is_resource('ziptestdata.subdirectory', 'binary.file'))

def test_submodule_contents(self):
submodule = import_module('ziptestdata.subdirectory')
self.assertEqual(
set(resources.contents(submodule)),
{'__init__.py', 'binary.file'})

def test_submodule_contents_by_name(self):
self.assertEqual(
set(resources.contents('ziptestdata.subdirectory')),
{'__init__.py', 'binary.file'})


class ResourceFromZipsTest02(util.ZipSetupBase, unittest.TestCase):
ZIP_MODULE = zipdata02 # type: ignore

def test_unrelated_contents(self):
Expand Down
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ passenv =
PYTHON*
LANG*
LC_*
OMIT
deps =
cov,diffcov: coverage>=4.5
diffcov: diff_cover
setenv =
cov: COVERAGE_PROCESS_START={[coverage]rcfile}
cov: COVERAGE_OPTIONS="-p"
cov: COVERAGE_FILE={toxinidir}/.coverage
py27: OMIT=3
py34,py35,py36,py37: OMIT=2


[testenv:qa]
Expand Down

0 comments on commit bd5afac

Please sign in to comment.