From 1ed28ef2b4d1693485b1543c97f47c6910fb9efd Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sun, 12 Jun 2016 21:59:37 +0200 Subject: [PATCH] Refactor plugin loading --- patacrep/build.py | 8 ++------ patacrep/files.py | 17 +++++++++++++++++ patacrep/tools/convert/__main__.py | 7 +------ test/test_content/test_content.py | 8 ++------ test/test_song/test_parser.py | 4 +--- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/patacrep/build.py b/patacrep/build.py index 3998244d..a1ece8a5 100644 --- a/patacrep/build.py +++ b/patacrep/build.py @@ -80,15 +80,11 @@ class Songbook: ) # Loading custom plugins - self._config['_content_plugins'] = files.load_plugins( + self._config['_content_plugins'] = files.load_plugins_content( datadirs=self._config['_datadir'], - root_modules=['content'], - keyword='CONTENT_PLUGINS', ) - self._config['_song_plugins'] = files.load_plugins( + self._config['_song_plugins'] = files.load_plugins_songs( datadirs=self._config['_datadir'], - root_modules=['songs'], - keyword='SONG_RENDERERS', )['tsg'] # Configuration set diff --git a/patacrep/files.py b/patacrep/files.py index bc51557d..8a3963ec 100644 --- a/patacrep/files.py +++ b/patacrep/files.py @@ -93,6 +93,22 @@ def iter_modules(path, prefix): LOGGER.debug("[plugins] Could not load module {}: {}".format(name, str(error))) continue +def load_plugins_content(datadirs=()): + """Load the content plugins, and return a dictionary of those plugins.""" + return load_plugins( + datadirs=datadirs, + root_modules=('content',), + keyword='CONTENT_PLUGINS', + ) + +def load_plugins_songs(datadirs=()): + """Load the song renderer plugins, and return a dictionary of those plugins.""" + return load_plugins( + datadirs=datadirs, + root_modules=('songs',), + keyword='SONG_RENDERERS', + ) + def load_plugins(datadirs, root_modules, keyword): """Load all plugins, and return a dictionary of those plugins. @@ -102,6 +118,7 @@ def load_plugins(datadirs, root_modules, keyword): Arguments: - 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 list of modules (e.g. ["some", "deep", "module"] for "some.deep.module"). diff --git a/patacrep/tools/convert/__main__.py b/patacrep/tools/convert/__main__.py index 65b82d8b..ac6c0843 100644 --- a/patacrep/tools/convert/__main__.py +++ b/patacrep/tools/convert/__main__.py @@ -35,12 +35,7 @@ def main(args=None): dest = args[2] song_files = args[3:] - # todo : what is the datadir argument used for? - renderers = files.load_plugins( - datadirs=[], - root_modules=['songs'], - keyword='SONG_RENDERERS', - ) + renderers = files.load_plugins_songs() if dest not in renderers: LOGGER.error( diff --git a/test/test_content/test_content.py b/test/test_content/test_content.py index dbf6926a..3c675889 100644 --- a/test/test_content/test_content.py +++ b/test/test_content/test_content.py @@ -108,15 +108,11 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest): ) # Load the plugins - config['_content_plugins'] = files.load_plugins( + config['_content_plugins'] = files.load_plugins_content( datadirs=config['_datadir'], - root_modules=['content'], - keyword='CONTENT_PLUGINS', ) - config['_song_plugins'] = files.load_plugins( + config['_song_plugins'] = files.load_plugins_songs( datadirs=config['_datadir'], - root_modules=['songs'], - keyword='SONG_RENDERERS', )['tsg'] return config diff --git a/test/test_song/test_parser.py b/test/test_song/test_parser.py index 39f4a05c..e4c0da35 100644 --- a/test/test_song/test_parser.py +++ b/test/test_song/test_parser.py @@ -81,10 +81,8 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest): cls.config['_datadir'] = [] cls.config['_datadir'].append('datadir') - cls.song_plugins = files.load_plugins( + cls.song_plugins = files.load_plugins_songs( datadirs=cls.config['_datadir'], - root_modules=['songs'], - keyword='SONG_RENDERERS', ) with cls.chdir(): for source in sorted(glob.glob('*.*.source')):