Browse Source

Merge pull request #227 from patacrep/plugins_refactor

Content & Song plugins refactor
pull/230/head
oliverpool 9 years ago
committed by GitHub
parent
commit
d49b66d8f5
  1. 14
      patacrep/build.py
  2. 2
      patacrep/content/__init__.py
  3. 2
      patacrep/content/song.py
  4. 19
      patacrep/files.py
  5. 7
      patacrep/tools/convert/__main__.py
  6. 12
      test/test_content/test_content.py
  7. 14
      test/test_song/test_parser.py

14
patacrep/build.py

@ -10,7 +10,7 @@ from subprocess import Popen, PIPE, call, check_call
import yaml import yaml
from patacrep import authors, content, encoding, errors, files, pkg_datapath, utils from patacrep import authors, content, encoding, errors, pkg_datapath, utils
from patacrep.index import process_sxd from patacrep.index import process_sxd
from patacrep.templates import TexBookRenderer, iter_bookoptions from patacrep.templates import TexBookRenderer, iter_bookoptions
@ -79,18 +79,6 @@ class Songbook:
copy.deepcopy(self._config['authors']) copy.deepcopy(self._config['authors'])
) )
# Loading custom plugins
self._config['_content_plugins'] = files.load_plugins(
datadirs=self._config['_datadir'],
root_modules=['content'],
keyword='CONTENT_PLUGINS',
)
self._config['_song_plugins'] = files.load_plugins(
datadirs=self._config['_datadir'],
root_modules=['songs'],
keyword='SONG_RENDERERS',
)['tsg']
# Configuration set # Configuration set
self._config['render'] = content.render self._config['render'] = content.render
self._config['content'] = content.process_content( self._config['content'] = content.process_content(

2
patacrep/content/__init__.py

@ -265,7 +265,7 @@ def process_content(content, config=None):
included in the .tex file. included in the .tex file.
""" """
contentlist = ContentList() contentlist = ContentList()
plugins = config.get('_content_plugins', {}) plugins = files.load_content_plugins(config['_datadir'])
if not content: if not content:
content = [{'song': None}] content = [{'song': None}]
elif isinstance(content, dict): elif isinstance(content, dict):

2
patacrep/content/song.py

@ -87,7 +87,7 @@ def parse(keyword, argument, config):
contentlist = argument contentlist = argument
if isinstance(contentlist, str): if isinstance(contentlist, str):
contentlist = [contentlist] contentlist = [contentlist]
plugins = config['_song_plugins'] plugins = files.load_renderer_plugins(config['_datadir'])['tsg']
if '_langs' not in config: if '_langs' not in config:
config['_langs'] = set() config['_langs'] = set()
songlist = ContentList() songlist = ContentList()

19
patacrep/files.py

@ -1,6 +1,7 @@
"""File system utilities.""" """File system utilities."""
from contextlib import contextmanager from contextlib import contextmanager
from functools import lru_cache
import logging import logging
import os import os
import pkgutil import pkgutil
@ -93,6 +94,23 @@ def iter_modules(path, prefix):
LOGGER.debug("[plugins] Could not load module {}: {}".format(name, str(error))) LOGGER.debug("[plugins] Could not load module {}: {}".format(name, str(error)))
continue continue
def load_content_plugins(datadirs=()):
"""Load the content plugins, and return a dictionary of those plugins."""
return load_plugins(
datadirs=tuple(datadirs),
root_modules=('content',),
keyword='CONTENT_PLUGINS',
)
def load_renderer_plugins(datadirs=()):
"""Load the song renderer plugins, and return a dictionary of those plugins."""
return load_plugins(
datadirs=tuple(datadirs),
root_modules=('songs',),
keyword='SONG_RENDERERS',
)
@lru_cache()
def load_plugins(datadirs, root_modules, keyword): def load_plugins(datadirs, root_modules, keyword):
"""Load all plugins, and return a dictionary of those plugins. """Load all plugins, and return a dictionary of those plugins.
@ -102,6 +120,7 @@ def load_plugins(datadirs, root_modules, keyword):
Arguments: Arguments:
- datadirs: List of directories in which plugins are to be searched. - datadirs: List of directories in which plugins are to be searched.
The plugins will also be searched in the patacrep modules.
- root_modules: the submodule in which plugins are to be searched, as a - root_modules: the submodule in which plugins are to be searched, as a
list of modules (e.g. ["some", "deep", "module"] for list of modules (e.g. ["some", "deep", "module"] for
"some.deep.module"). "some.deep.module").

7
patacrep/tools/convert/__main__.py

@ -35,12 +35,7 @@ def main(args=None):
dest = args[2] dest = args[2]
song_files = args[3:] song_files = args[3:]
# todo : what is the datadir argument used for? renderers = files.load_renderer_plugins()
renderers = files.load_plugins(
datadirs=[],
root_modules=['songs'],
keyword='SONG_RENDERERS',
)
if dest not in renderers: if dest not in renderers:
LOGGER.error( LOGGER.error(

12
test/test_content/test_content.py

@ -107,16 +107,4 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
outputdir outputdir
) )
# Load the plugins
config['_content_plugins'] = files.load_plugins(
datadirs=config['_datadir'],
root_modules=['content'],
keyword='CONTENT_PLUGINS',
)
config['_song_plugins'] = files.load_plugins(
datadirs=config['_datadir'],
root_modules=['songs'],
keyword='SONG_RENDERERS',
)['tsg']
return config return config

14
test/test_song/test_parser.py

@ -60,7 +60,7 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
with self.chdir(): with self.chdir():
with open_read(destname) as expectfile: with open_read(destname) as expectfile:
with logging_reduced(): with logging_reduced():
song = self.song_plugins[out_format][in_format](sourcename, self.config) song = self.song_renderer[out_format][in_format](sourcename, self.config)
expected = expectfile.read().strip().replace( expected = expectfile.read().strip().replace(
"@TEST_FOLDER@", "@TEST_FOLDER@",
files.path2posix(resource_filename(__name__, "")), files.path2posix(resource_filename(__name__, "")),
@ -76,16 +76,10 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
# Setting datadir # Setting datadir
# Load the default songbook config # Load the default songbook config
cls.config = config_model('default')['en'] cls.config = config_model('default')['en']
cls.config['_datadir'] = ['datadir']
if '_datadir' not in cls.config: cls.song_renderer = files.load_renderer_plugins()
cls.config['_datadir'] = []
cls.config['_datadir'].append('datadir')
cls.song_plugins = files.load_plugins(
datadirs=cls.config['_datadir'],
root_modules=['songs'],
keyword='SONG_RENDERERS',
)
with cls.chdir(): with cls.chdir():
for source in sorted(glob.glob('*.*.source')): for source in sorted(glob.glob('*.*.source')):
[*base, in_format, _] = source.split('.') [*base, in_format, _] = source.split('.')
@ -128,7 +122,7 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
"""Test that `base` parsing fails.""" """Test that `base` parsing fails."""
sourcename = "{}.{}.source".format(base, in_format) sourcename = "{}.{}.source".format(base, in_format)
with self.chdir('errors'): with self.chdir('errors'):
parser = self.song_plugins[out_format][in_format] parser = self.song_renderer[out_format][in_format]
self.assertRaises(errors.SongSyntaxError, parser, sourcename, self.config) self.assertRaises(errors.SongSyntaxError, parser, sourcename, self.config)
test_parse_failure.__doc__ = ( test_parse_failure.__doc__ = (

Loading…
Cancel
Save