diff --git a/NEWS.md b/NEWS.md index bbc8f7af..4e374f6c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,7 @@ * The files don't need to end with a trailing line [#223](https://github.com/patacrep/patacrep/pull/223) * Content files * The `tex` keyword correctly includes all files of the list [#228](https://github.com/patacrep/patacrep/pull/228) + * Fix the import when the content folder is zipped [#235](https://github.com/patacrep/patacrep/pull/235) * PDF generation * The bookmarks correctly use the unicode encoding [#225](https://github.com/patacrep/patacrep/pull/225) * Enhancements diff --git a/patacrep/files.py b/patacrep/files.py index 1ad296d5..967844d4 100644 --- a/patacrep/files.py +++ b/patacrep/files.py @@ -8,6 +8,7 @@ import pkgutil import posixpath import re import sys +from zipimport import zipimporter from patacrep import utils from patacrep import __DATADIR__ @@ -79,6 +80,13 @@ def chdir(*path): else: yield +def load_module(module_finder, name): + """Load a custom module, be it a zipimporter or a FileFinder""" + if isinstance(module_finder, zipimporter): + return module_finder.load_module(name) + else: + return module_finder.find_spec(name).loader.load_module() + def iter_modules(path, prefix): """Iterate over modules located in list of `path`. @@ -89,7 +97,7 @@ def iter_modules(path, prefix): yield sys.modules[name] else: try: - yield module_finder.find_spec(name).loader.load_module() + yield load_module(module_finder, name) except ImportError as error: LOGGER.debug("[plugins] Could not load module {}: {}".format(name, str(error))) continue diff --git a/test/test_content/custom.control b/test/test_content/custom.control new file mode 100644 index 00000000..b549c6a3 --- /dev/null +++ b/test/test_content/custom.control @@ -0,0 +1 @@ +- "{'customname:': ''}" \ No newline at end of file diff --git a/test/test_content/custom.source b/test/test_content/custom.source new file mode 100644 index 00000000..ded39db8 --- /dev/null +++ b/test/test_content/custom.source @@ -0,0 +1 @@ +- customname: \ No newline at end of file diff --git a/test/test_content/customzipped.control b/test/test_content/customzipped.control new file mode 100644 index 00000000..6d3fd533 --- /dev/null +++ b/test/test_content/customzipped.control @@ -0,0 +1 @@ +- "{'customzippedname:': ''}" \ No newline at end of file diff --git a/test/test_content/customzipped.source b/test/test_content/customzipped.source new file mode 100644 index 00000000..7c67530d --- /dev/null +++ b/test/test_content/customzipped.source @@ -0,0 +1 @@ +- customzippedname: diff --git a/test/test_content/datadir/python/content/customplugin.py b/test/test_content/datadir/python/content/customplugin.py new file mode 100755 index 00000000..e7333b30 --- /dev/null +++ b/test/test_content/datadir/python/content/customplugin.py @@ -0,0 +1,15 @@ +"""Fake plugin for test purposes.""" + +from patacrep.content import ContentItem, ContentList, validate_parser_argument + +class FakeContent(ContentItem): + """Fake content.""" + + def file_entry(self): + return {'customname:':''} + +def parse(keyword, argument, config): + return ContentList([FakeContent()]) + +CONTENT_PLUGINS = {'customname': parse} + diff --git a/test/test_content/datadir_zippedcontent/python/content b/test/test_content/datadir_zippedcontent/python/content new file mode 100644 index 00000000..a333d318 Binary files /dev/null and b/test/test_content/datadir_zippedcontent/python/content differ diff --git a/test/test_content/test_content.py b/test/test_content/test_content.py index 770d4e1a..5edb9f96 100644 --- a/test/test_content/test_content.py +++ b/test/test_content/test_content.py @@ -93,7 +93,7 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest): return files.path2posix(elem.filename) else: - raise Exception(elem) + return str(elem.file_entry()) @classmethod def _generate_config(cls, sbcontent, outputdir, base): @@ -101,7 +101,7 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest): # Load the default songbook config config = prepare_songbook( - {'book':{'datadir':'datadir'}, 'content': sbcontent}, + {'book':{'datadir':['datadir', 'datadir_zippedcontent']}, 'content': sbcontent}, outputdir, base, outputdir