From 038028d224c0d862e1bb2c1fef9d000735676350 Mon Sep 17 00:00:00 2001 From: Luthaf Date: Thu, 3 Jul 2014 16:58:25 +0100 Subject: [PATCH 01/10] 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: From 75932768e264f755da9255c2df85bbb9ecb8e588 Mon Sep 17 00:00:00 2001 From: Luthaf Date: Thu, 3 Jul 2014 22:11:17 +0100 Subject: [PATCH 02/10] Adds a warning message if _songbook_dir is not set --- patacrep/content/include.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/patacrep/content/include.py b/patacrep/content/include.py index 777aa3bd..a7c14735 100644 --- a/patacrep/content/include.py +++ b/patacrep/content/include.py @@ -26,6 +26,11 @@ def parse(keyword, config, argument, contentlist): - contentlist: a list of file paths to be included. """ new_contentlist = [] + + if not os.path.isdir(config.get("_songbook_dir", "")): + LOGGER.warning("No songbook directory in configuration. 'include' " + "keyword may fail.") + for path in contentlist: filepath = os.path.join(config["_songbook_dir"], path) try: From 18b5c9c22c16e61feef6508165c228cf714db026 Mon Sep 17 00:00:00 2001 From: Luthaf Date: Thu, 3 Jul 2014 22:16:16 +0100 Subject: [PATCH 03/10] =?UTF-8?q?Gestion=20du=20cas=20o=C3=B9=20la=20clef?= =?UTF-8?q?=20=5Fsongbook=5Fdir=20n'existe=20pas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- patacrep/content/include.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/patacrep/content/include.py b/patacrep/content/include.py index a7c14735..e01239fa 100644 --- a/patacrep/content/include.py +++ b/patacrep/content/include.py @@ -26,16 +26,15 @@ def parse(keyword, config, argument, contentlist): - contentlist: a list of file paths to be included. """ new_contentlist = [] - - if not os.path.isdir(config.get("_songbook_dir", "")): + songbook_dir = config.get("_songbook_dir", "") + if not os.path.isdir(songbook_dir): LOGGER.warning("No songbook directory in configuration. 'include' " "keyword may fail.") for path in contentlist: - filepath = os.path.join(config["_songbook_dir"], path) + filepath = os.path.join(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) @@ -47,7 +46,7 @@ def parse(keyword, config, argument, contentlist): ) new_contentlist += process_content(new_content, config) - config["_songbook_dir"] = old_songbook_dir + config["_songbook_dir"] = songbook_dir return new_contentlist From 4563c04c9df935e7c4dcb2249b00eaaf3faf7c0d Mon Sep 17 00:00:00 2001 From: Luthaf Date: Thu, 3 Jul 2014 22:16:40 +0100 Subject: [PATCH 04/10] Typo --- patacrep/content/include.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/patacrep/content/include.py b/patacrep/content/include.py index e01239fa..ac81c940 100644 --- a/patacrep/content/include.py +++ b/patacrep/content/include.py @@ -20,7 +20,7 @@ def parse(keyword, config, argument, contentlist): """Include an external file content. Arguments: - - keyword: the string 'sorted'; + - keyword: the string 'include'; - config: the current songbook configuration dictionary; - argument: None; - contentlist: a list of file paths to be included. From 813e5d9bdcc18cc7c18160379f8eedd45eee3454 Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 4 Jul 2014 10:50:46 +0200 Subject: [PATCH 05/10] Trying to solve encoding problems #50 --- patacrep/authors.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/patacrep/authors.py b/patacrep/authors.py index 8cb66962..6728029d 100644 --- a/patacrep/authors.py +++ b/patacrep/authors.py @@ -21,8 +21,11 @@ def to_utf8(string): elif type(string) is str: return string.decode('iso-8859-1').encode('utf-8') else: - LOGGER.warning("Ignoring a word I can not decode...") - return None + try: + return string.encode('utf-8') + except: + LOGGER.warning("Ignoring a word I can not decode...") + return "" def compile_authwords(authwords): """Convert strings of authwords to compiled regular expressions. From ec48864f761bc1c6de7685295dc9d531f299f97b Mon Sep 17 00:00:00 2001 From: Luthaf Date: Fri, 4 Jul 2014 18:38:49 +0100 Subject: [PATCH 06/10] Use datadirs as base search path for included content --- patacrep/content/include.py | 27 ++++++++++++++++----------- songbook | 6 ++---- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/patacrep/content/include.py b/patacrep/content/include.py index ac81c940..b1e1f1cd 100644 --- a/patacrep/content/include.py +++ b/patacrep/content/include.py @@ -11,10 +11,22 @@ import os import sys import logging -from patacrep.content import process_content +from patacrep.content import process_content, ContentError LOGGER = logging.getLogger(__name__) +def load_from_datadirs(path, config=None): + if not config or not config["datadir"]: + LOGGER.error("No datadir in the configuration.") + sys.exit(1) + for datadir in config["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. @@ -26,13 +38,9 @@ def parse(keyword, config, argument, contentlist): - contentlist: a list of file paths to be included. """ new_contentlist = [] - songbook_dir = config.get("_songbook_dir", "") - if not os.path.isdir(songbook_dir): - LOGGER.warning("No songbook directory in configuration. 'include' " - "keyword may fail.") for path in contentlist: - filepath = os.path.join(songbook_dir, path) + filepath = load_from_datadirs(path, config) try: with open(filepath, "r") as content_file: new_content = json.load(content_file) @@ -41,12 +49,9 @@ def parse(keyword, config, argument, contentlist): LOGGER.error("Error while loading file '{}'.".format(filepath)) sys.exit(1) - config["_songbook_dir"] = os.path.abspath( - os.path.dirname(filepath) - ) - + config["datadir"].append(os.path.abspath(os.path.dirname(filepath))) new_contentlist += process_content(new_content, config) - config["_songbook_dir"] = songbook_dir + config["datadir"].pop() return new_contentlist diff --git a/songbook b/songbook index 1919d12a..626385b4 100755 --- a/songbook +++ b/songbook @@ -108,8 +108,6 @@ 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: @@ -126,9 +124,9 @@ def main(): ) for path in songbook['datadir'] ] - if not datadirs: # Default value - datadirs = [os.path.dirname(os.path.abspath(songbook_path))] + datadirs.append(os.path.dirname(os.path.abspath(songbook_path))) + songbook['datadir'] = datadirs try: From d19675189b6984d7cc714d36b7c63abafc82012b Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 4 Jul 2014 20:36:20 +0200 Subject: [PATCH 07/10] Corrected indentation --- songbook | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/songbook b/songbook index 6d9f107a..2507ecf3 100755 --- a/songbook +++ b/songbook @@ -127,8 +127,8 @@ def main(): ) for path in songbook['datadir'] ] - # Default value - datadirs.append(os.path.dirname(os.path.abspath(songbook_path))) + # Default value + datadirs.append(os.path.dirname(os.path.abspath(songbook_path))) songbook['datadir'] = datadirs From e6fbc51ebf1789d86444f90d059666f03a0f3ad6 Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 4 Jul 2014 20:55:05 +0200 Subject: [PATCH 08/10] Simplify case where datadir is empty --- patacrep/content/include.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/patacrep/content/include.py b/patacrep/content/include.py index b1e1f1cd..5c73d8e2 100644 --- a/patacrep/content/include.py +++ b/patacrep/content/include.py @@ -16,10 +16,7 @@ from patacrep.content import process_content, ContentError LOGGER = logging.getLogger(__name__) def load_from_datadirs(path, config=None): - if not config or not config["datadir"]: - LOGGER.error("No datadir in the configuration.") - sys.exit(1) - for datadir in config["datadir"]: + for datadir in config.get("datadir", []): filepath = os.path.join(datadir, path) if os.path.exists(filepath): return filepath From fc05a7cb17966d8090e8796ca96dbe5564923138 Mon Sep 17 00:00:00 2001 From: Luthaf Date: Fri, 4 Jul 2014 19:58:09 +0100 Subject: [PATCH 09/10] Suppression de la liste des accords si les diagrammes sont absents La liste des accords ne comprenait que des lignes vides sinon --- patacrep/data/templates/default.tex | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/patacrep/data/templates/default.tex b/patacrep/data/templates/default.tex index 1a2bf26a..5c105a72 100644 --- a/patacrep/data/templates/default.tex +++ b/patacrep/data/templates/default.tex @@ -117,9 +117,11 @@ (* block chords *) % list of chords \ifchorded - \phantomsection - \addcontentsline{toc}{section}{\chordlistname} - \chords + \ifdiagram + \phantomsection + \addcontentsline{toc}{section}{\chordlistname} + \chords + \fi \fi (* endblock *) From f8ee770610878478018f85fddae972b8049d1544 Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 4 Jul 2014 20:57:32 +0200 Subject: [PATCH 10/10] Guess encoding while encoding file --- patacrep/content/include.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/patacrep/content/include.py b/patacrep/content/include.py index 5c73d8e2..0b4a8967 100644 --- a/patacrep/content/include.py +++ b/patacrep/content/include.py @@ -12,6 +12,7 @@ import sys import logging from patacrep.content import process_content, ContentError +from patacrep import encoding LOGGER = logging.getLogger(__name__) @@ -38,13 +39,17 @@ def parse(keyword, config, argument, contentlist): for path in contentlist: filepath = load_from_datadirs(path, config) + content_file = None try: - with open(filepath, "r") as content_file: - new_content = json.load(content_file) + 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)