From 038028d224c0d862e1bb2c1fef9d000735676350 Mon Sep 17 00:00:00 2001 From: Luthaf Date: Thu, 3 Jul 2014 16:58:25 +0100 Subject: [PATCH] Plugin include MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Inclusion de fichier JSON contenant une liste de contenu. Permet d'avoir le même contenu avec plusieurs mises en page. --- patacrep/content/include.py | 49 +++++++++++++++++++++++++++++++++++++ songbook | 2 ++ 2 files changed, 51 insertions(+) create mode 100644 patacrep/content/include.py diff --git a/patacrep/content/include.py b/patacrep/content/include.py new file mode 100644 index 00000000..777aa3bd --- /dev/null +++ b/patacrep/content/include.py @@ -0,0 +1,49 @@ +# -*- 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 + +LOGGER = logging.getLogger(__name__) + +#pylint: disable=unused-argument +def parse(keyword, config, argument, contentlist): + """Include an external file content. + + Arguments: + - keyword: the string 'sorted'; + - config: the current songbook configuration dictionary; + - argument: None; + - contentlist: a list of file paths to be included. + """ + new_contentlist = [] + for path in contentlist: + filepath = os.path.join(config["_songbook_dir"], path) + try: + with open(filepath, "r") as content_file: + old_songbook_dir = config["_songbook_dir"] + 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) + + config["_songbook_dir"] = os.path.abspath( + os.path.dirname(filepath) + ) + + new_contentlist += process_content(new_content, config) + config["_songbook_dir"] = old_songbook_dir + + return new_contentlist + +CONTENT_PLUGINS = {'include': parse} diff --git a/songbook b/songbook index 53d707a1..1919d12a 100755 --- a/songbook +++ b/songbook @@ -108,6 +108,8 @@ def main(): LOGGER.error("Error while loading file '{}'.".format(songbook_path)) sys.exit(1) + songbook["_songbook_dir"] = os.path.abspath(os.path.dirname(songbook_path)) + # Gathering datadirs datadirs = [] if options.datadir: