Browse Source

Look for content plugins in <datadir>/python/content

pull/54/head
Louis 10 years ago
parent
commit
255476ba00
  1. 33
      patacrep/content/__init__.py

33
patacrep/content/__init__.py

@ -11,8 +11,9 @@ Content class.
# Plugin definition # Plugin definition
A plugin is a submodule of this module, which have a variable A plugin is a submodule of this module, (or a python file in directory
CONTENT_PLUGINS, which is a dictionary where: <datadir>/python/content), which have a variable CONTENT_PLUGINS, which is a
dictionary where:
- keys are keywords, - keys are keywords,
- values are parsers (see below). - values are parsers (see below).
@ -73,6 +74,7 @@ import jinja2
import logging import logging
import os import os
import re import re
import sys
from patacrep import files from patacrep import files
from patacrep.errors import SongbookError from patacrep.errors import SongbookError
@ -132,7 +134,7 @@ class ContentError(SongbookError):
def __str__(self): def __str__(self):
return "Content: {}: {}".format(self.keyword, self.message) return "Content: {}: {}".format(self.keyword, self.message)
def load_plugins(): def load_plugins(config):
"""Load all content plugins, and return a dictionary of those plugins. """Load all content plugins, and return a dictionary of those plugins.
Return value: a dictionary where: Return value: a dictionary where:
@ -140,13 +142,33 @@ def load_plugins():
- values are functions triggered when this keyword is met. - values are functions triggered when this keyword is met.
""" """
plugins = {} plugins = {}
for name in glob.glob(os.path.join(os.path.dirname(__file__), "*.py")): directory_list = (
[
os.path.join(datadir, "python", "content")
for datadir in config.get('datadir', [])
]
+ [os.path.dirname(__file__)]
)
for directory in directory_list:
if not os.path.exists(directory):
LOGGER.debug(
"Ignoring non-existent directory '%s'.",
directory
)
continue
sys.path.append(directory)
for name in glob.glob(os.path.join(directory, '*.py')):
if name.endswith(".py") and os.path.basename(name) != "__init__.py": if name.endswith(".py") and os.path.basename(name) != "__init__.py":
if directory == os.path.dirname(__file__):
plugin = importlib.import_module( plugin = importlib.import_module(
'patacrep.content.{}'.format( 'patacrep.content.{}'.format(
os.path.basename(name[:-len('.py')]) os.path.basename(name[:-len('.py')])
) )
) )
else:
plugin = importlib.import_module(
os.path.basename(name[:-len('.py')])
)
for (key, value) in plugin.CONTENT_PLUGINS.items(): for (key, value) in plugin.CONTENT_PLUGINS.items():
if key in plugins: if key in plugins:
LOGGER.warning( LOGGER.warning(
@ -156,6 +178,7 @@ def load_plugins():
) )
continue continue
plugins[key] = value plugins[key] = value
del sys.path[-1]
return plugins return plugins
@jinja2.contextfunction @jinja2.contextfunction
@ -201,7 +224,7 @@ def process_content(content, config=None):
included in the .tex file. included in the .tex file.
""" """
contentlist = [] contentlist = []
plugins = load_plugins() plugins = load_plugins(config)
keyword_re = re.compile(r'^ *(?P<keyword>\w*) *(\((?P<argument>.*)\))? *$') keyword_re = re.compile(r'^ *(?P<keyword>\w*) *(\((?P<argument>.*)\))? *$')
if not content: if not content:
content = [["song"]] content = [["song"]]

Loading…
Cancel
Save