Browse Source

Merge pull request #235 from patacrep/zipimport_fix

Compatible module loading for zip modules
pull/239/head
oliverpool 8 years ago
committed by GitHub
parent
commit
fcc9493545
  1. 1
      NEWS.md
  2. 10
      patacrep/files.py
  3. 1
      test/test_content/custom.control
  4. 1
      test/test_content/custom.source
  5. 1
      test/test_content/customzipped.control
  6. 1
      test/test_content/customzipped.source
  7. 15
      test/test_content/datadir/python/content/customplugin.py
  8. BIN
      test/test_content/datadir_zippedcontent/python/content
  9. 4
      test/test_content/test_content.py

1
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

10
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

1
test/test_content/custom.control

@ -0,0 +1 @@
- "{'customname:': ''}"

1
test/test_content/custom.source

@ -0,0 +1 @@
- customname:

1
test/test_content/customzipped.control

@ -0,0 +1 @@
- "{'customzippedname:': ''}"

1
test/test_content/customzipped.source

@ -0,0 +1 @@
- customzippedname:

15
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}

BIN
test/test_content/datadir_zippedcontent/python/content

Binary file not shown.

4
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

Loading…
Cancel
Save