Browse Source

Merge pull request #53 from patacrep/include

Plugin include
pull/55/head
Luthaf 11 years ago
parent
commit
5d18d08ffa
  1. 60
      patacrep/content/include.py
  2. 4
      songbook

60
patacrep/content/include.py

@ -0,0 +1,60 @@
# -*- coding: utf-8 -*-
"""Include an external list of songs
This plugin provides keyword 'include', used to include an external list of
songs in JSON format.
"""
import json
import os
import sys
import logging
from patacrep.content import process_content, ContentError
from patacrep import encoding
LOGGER = logging.getLogger(__name__)
def load_from_datadirs(path, config=None):
for datadir in config.get("datadir", []):
filepath = os.path.join(datadir, path)
if os.path.exists(filepath):
return filepath
# File not found
raise ContentError("include", "The file '{0}' was not found in the "
"datadirs.".format(path))
#pylint: disable=unused-argument
def parse(keyword, config, argument, contentlist):
"""Include an external file content.
Arguments:
- keyword: the string 'include';
- config: the current songbook configuration dictionary;
- argument: None;
- contentlist: a list of file paths to be included.
"""
new_contentlist = []
for path in contentlist:
filepath = load_from_datadirs(path, config)
content_file = None
try:
content_file = encoding.open_read(filepath, 'r')
new_content = json.load(content_file)
except Exception as error: # pylint: disable=broad-except
LOGGER.error(error)
LOGGER.error("Error while loading file '{}'.".format(filepath))
sys.exit(1)
finally:
if content_file:
content_file.close()
config["datadir"].append(os.path.abspath(os.path.dirname(filepath)))
new_contentlist += process_content(new_content, config)
config["datadir"].pop()
return new_contentlist
CONTENT_PLUGINS = {'include': parse}

4
songbook

@ -127,9 +127,9 @@ def main():
) )
for path in songbook['datadir'] for path in songbook['datadir']
] ]
if not datadirs:
# Default value # Default value
datadirs = [os.path.dirname(os.path.abspath(songbook_path))] datadirs.append(os.path.dirname(os.path.abspath(songbook_path)))
songbook['datadir'] = datadirs songbook['datadir'] = datadirs
try: try:

Loading…
Cancel
Save