From 385a49c5f44ec62536036a7bb8927e05cc2e0d81 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sun, 3 Jul 2016 19:50:11 +0200 Subject: [PATCH 1/8] Compatible module loading for zip modules #234 --- patacrep/files.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/patacrep/files.py b/patacrep/files.py index 1ad296d5..7a9b29ef 100644 --- a/patacrep/files.py +++ b/patacrep/files.py @@ -8,6 +8,7 @@ import pkgutil import posixpath import re import sys +from zipimport import ZipImportError from patacrep import utils from patacrep import __DATADIR__ @@ -90,6 +91,13 @@ def iter_modules(path, prefix): else: try: yield module_finder.find_spec(name).loader.load_module() + except AttributeError: + # It is a zipimport.zipimporter object + try: + yield module_finder.load_module(name) + except ZipImportError as error: + LOGGER.debug("[plugins] Could not load module {}: {}".format(name, str(error))) + continue except ImportError as error: LOGGER.debug("[plugins] Could not load module {}: {}".format(name, str(error))) continue From 5fb242f421d42e41945212cfceaeafa1860e7655 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sun, 3 Jul 2016 22:35:02 +0200 Subject: [PATCH 2/8] add custom plugins tests --- test/test_content/custom.control | 1 + test/test_content/custom.source | 1 + .../datadir/python/content/customplugin.py | 21 +++++++++++++++++++ test/test_content/test_content.py | 2 +- 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/test_content/custom.control create mode 100644 test/test_content/custom.source create mode 100755 test/test_content/datadir/python/content/customplugin.py 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/datadir/python/content/customplugin.py b/test/test_content/datadir/python/content/customplugin.py new file mode 100755 index 00000000..2137198c --- /dev/null +++ b/test/test_content/datadir/python/content/customplugin.py @@ -0,0 +1,21 @@ +"""Fake plugin for test purposes.""" + +from patacrep.content import ContentItem, ContentList, validate_parser_argument + +class FakeContent(ContentItem): + """Fake content.""" + + def render(self, __context): + return 'fakecontent' + + def file_entry(self): + return {'customname:':''} + +#pylint: disable=unused-argument +def parse(keyword, argument, config): + """ + """ + return ContentList([FakeContent()]) + +CONTENT_PLUGINS = {'customname': parse} + diff --git a/test/test_content/test_content.py b/test/test_content/test_content.py index 770d4e1a..bd429bf8 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): From a655f1584b354b15da54c10f6fbacb6af09067dd Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sun, 3 Jul 2016 22:38:06 +0200 Subject: [PATCH 3/8] simplify custom plugin --- test/test_content/datadir/python/content/customplugin.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/test_content/datadir/python/content/customplugin.py b/test/test_content/datadir/python/content/customplugin.py index 2137198c..e7333b30 100755 --- a/test/test_content/datadir/python/content/customplugin.py +++ b/test/test_content/datadir/python/content/customplugin.py @@ -5,16 +5,10 @@ from patacrep.content import ContentItem, ContentList, validate_parser_argument class FakeContent(ContentItem): """Fake content.""" - def render(self, __context): - return 'fakecontent' - def file_entry(self): return {'customname:':''} -#pylint: disable=unused-argument def parse(keyword, argument, config): - """ - """ return ContentList([FakeContent()]) CONTENT_PLUGINS = {'customname': parse} From d45753b5bc75c372267ab93b6cd7f42663cd1758 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sun, 3 Jul 2016 22:53:58 +0200 Subject: [PATCH 4/8] add test for custom contents --- test/test_content/customzipped.control | 1 + test/test_content/customzipped.source | 4 ++++ .../datadir/python/content/customzipedplugin.py | 15 +++++++++++++++ .../datadir_zippedcontent/python/content | Bin 0 -> 420 bytes test/test_content/test_content.py | 2 +- 5 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/test_content/customzipped.control create mode 100644 test/test_content/customzipped.source create mode 100755 test/test_content/datadir/python/content/customzipedplugin.py create mode 100644 test/test_content/datadir_zippedcontent/python/content 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..f94689e6 --- /dev/null +++ b/test/test_content/customzipped.source @@ -0,0 +1,4 @@ +- addsongdir: + path: "datadir_sort" + content: + - customzippedname: diff --git a/test/test_content/datadir/python/content/customzipedplugin.py b/test/test_content/datadir/python/content/customzipedplugin.py new file mode 100755 index 00000000..1d20b777 --- /dev/null +++ b/test/test_content/datadir/python/content/customzipedplugin.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 {'customzippedname:':''} + +def parse(keyword, argument, config): + return ContentList([FakeContent()]) + +CONTENT_PLUGINS = {'customzipedname': parse} + diff --git a/test/test_content/datadir_zippedcontent/python/content b/test/test_content/datadir_zippedcontent/python/content new file mode 100644 index 0000000000000000000000000000000000000000..a333d318a650a18aa11e723f9930df3f4a98577d GIT binary patch literal 420 zcmWIWW@Zs#U|`^2__F1(M{;Ia(GwuAkdc8wltG3exwN<>KesBgpddA+Ag44vGf%Ie zGBkvfftjbGGMo>HODnh;7+GF0GcbUO+r@==*=~-Y*@JZ!Ze2hZqM+g z5{dmi9jTocHB@!?R)VJDw@x%Y>NkMiixtr!c%1FD=*I;Py!{Z(+kG1Affmw4{ z*GGJ9sk^>=W?`DaHS44s>t8osy!QQR2GjL|(8-b_$FEEKQ{4CLWO+^R9DT)dIqgj8&aBVNKh?gDsSR;@6MyS-a`n5o{`~Xp-&pwnFX&EF z)%y}3;LXS+$BZk`B!I!kzyJ(ChPRC%7LFifg#;m5@CA6YvVk-)0%0_eHU^o)004f8 Boo)aC literal 0 HcmV?d00001 diff --git a/test/test_content/test_content.py b/test/test_content/test_content.py index bd429bf8..5edb9f96 100644 --- a/test/test_content/test_content.py +++ b/test/test_content/test_content.py @@ -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 From 9fdb617d4ce8b65ebeb5f180b6b7061344727b79 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sun, 3 Jul 2016 22:54:59 +0200 Subject: [PATCH 5/8] unused file --- .../datadir/python/content/customzipedplugin.py | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100755 test/test_content/datadir/python/content/customzipedplugin.py diff --git a/test/test_content/datadir/python/content/customzipedplugin.py b/test/test_content/datadir/python/content/customzipedplugin.py deleted file mode 100755 index 1d20b777..00000000 --- a/test/test_content/datadir/python/content/customzipedplugin.py +++ /dev/null @@ -1,15 +0,0 @@ -"""Fake plugin for test purposes.""" - -from patacrep.content import ContentItem, ContentList, validate_parser_argument - -class FakeContent(ContentItem): - """Fake content.""" - - def file_entry(self): - return {'customzippedname:':''} - -def parse(keyword, argument, config): - return ContentList([FakeContent()]) - -CONTENT_PLUGINS = {'customzipedname': parse} - From dfdd9ce10bf02df8dd2959b419fefeff816de899 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sun, 3 Jul 2016 22:58:41 +0200 Subject: [PATCH 6/8] factorize module loading --- patacrep/files.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/patacrep/files.py b/patacrep/files.py index 7a9b29ef..967844d4 100644 --- a/patacrep/files.py +++ b/patacrep/files.py @@ -8,7 +8,7 @@ import pkgutil import posixpath import re import sys -from zipimport import ZipImportError +from zipimport import zipimporter from patacrep import utils from patacrep import __DATADIR__ @@ -80,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`. @@ -90,14 +97,7 @@ def iter_modules(path, prefix): yield sys.modules[name] else: try: - yield module_finder.find_spec(name).loader.load_module() - except AttributeError: - # It is a zipimport.zipimporter object - try: - yield module_finder.load_module(name) - except ZipImportError as error: - LOGGER.debug("[plugins] Could not load module {}: {}".format(name, str(error))) - continue + yield load_module(module_finder, name) except ImportError as error: LOGGER.debug("[plugins] Could not load module {}: {}".format(name, str(error))) continue From 02768656e9a3ac4a5be5e52a30b025faf81dd22b Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sun, 3 Jul 2016 23:14:57 +0200 Subject: [PATCH 7/8] update news --- NEWS.md | 1 + 1 file changed, 1 insertion(+) 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 From 4c0fc7d926b37c35cb61a2021b78f59c3f08040c Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Mon, 4 Jul 2016 09:04:20 +0200 Subject: [PATCH 8/8] simplify test --- test/test_content/customzipped.source | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/test_content/customzipped.source b/test/test_content/customzipped.source index f94689e6..7c67530d 100644 --- a/test/test_content/customzipped.source +++ b/test/test_content/customzipped.source @@ -1,4 +1 @@ -- addsongdir: - path: "datadir_sort" - content: - - customzippedname: +- customzippedname: