mirror of https://github.com/patacrep/patacrep.git
Louis
11 years ago
19 changed files with 242 additions and 86 deletions
@ -1,3 +1,4 @@ |
|||||
Jinja2==2.7.3 |
Jinja2==2.7.3 |
||||
argparse==1.2.1 |
argparse==1.2.1 |
||||
-e git+https://github.com/tiarno/plastex#egg=plasTeX |
chardet==2.2.1 |
||||
|
https://github.com/tiarno/plastex/archive/master.zip |
@ -0,0 +1,64 @@ |
|||||
|
# -*- 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): |
||||
|
"""Load 'path' from one of the datadirs. |
||||
|
|
||||
|
Raise an exception if it was found if none of the datadirs of 'config'. |
||||
|
""" |
||||
|
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} |
@ -0,0 +1,24 @@ |
|||||
|
{ |
||||
|
"bookoptions" : [ |
||||
|
"importantdiagramonly", |
||||
|
"repeatchords", |
||||
|
"lilypond", |
||||
|
"pictures" |
||||
|
], |
||||
|
"booktype" : "chorded", |
||||
|
"lang" : "french", |
||||
|
"authwords" : { |
||||
|
"sep" : ["and", "et", "À"], |
||||
|
"ignore" : ["À"], |
||||
|
"after" : ["À"] |
||||
|
}, |
||||
|
"titleprefixwords": ["À"], |
||||
|
"datadir" : ".", |
||||
|
"content" : [["section", "Traditional"], |
||||
|
"chevaliers_de_la_table_ronde.sg", |
||||
|
"greensleeves.sg", |
||||
|
"vent_frais.sg", |
||||
|
["section", "Example"], |
||||
|
"example-fr.sg", |
||||
|
"example-en.sg"] |
||||
|
} |
@ -0,0 +1,49 @@ |
|||||
|
#!/usr/bin/python |
||||
|
# -*- coding: utf-8 -*- |
||||
|
|
||||
|
"""Dealing with encoding problems.""" |
||||
|
|
||||
|
import codecs |
||||
|
import chardet |
||||
|
import logging |
||||
|
from unidecode import unidecode as unidecode_orig |
||||
|
|
||||
|
LOGGER = logging.getLogger(__name__) |
||||
|
|
||||
|
def open_read(filename, mode='r'): |
||||
|
"""Open a file for reading, guessing the right encoding. |
||||
|
|
||||
|
Return a fileobject, reading unicode strings. |
||||
|
""" |
||||
|
return codecs.open( |
||||
|
filename, |
||||
|
mode=mode, |
||||
|
encoding=chardet.detect(open(filename, "r").read())['encoding'], |
||||
|
errors='replace', |
||||
|
) |
||||
|
|
||||
|
def basestring2unicode(arg): |
||||
|
"""Return the unicode version of the argument, guessing original encoding. |
||||
|
""" |
||||
|
if isinstance(arg, unicode): |
||||
|
return arg |
||||
|
elif isinstance(arg, basestring): |
||||
|
return arg.decode( |
||||
|
encoding=chardet.detect(arg)['encoding'], |
||||
|
errors='replace', |
||||
|
) |
||||
|
else: |
||||
|
LOGGER.warning("Cannot decode string {}. Ignored.".format(str(arg))) |
||||
|
return "" |
||||
|
|
||||
|
def list2unicode(arg): |
||||
|
"""Return the unicode version of the argument, guessing original encoding. |
||||
|
|
||||
|
Argument is a list of strings. If an item is of another type, it is |
||||
|
silently ignored (an empty string is returned). |
||||
|
""" |
||||
|
return [basestring2unicode(item) for item in arg] |
||||
|
|
||||
|
def unidecode(arg): |
||||
|
"""Return a unicode version of a unidecoded string.""" |
||||
|
return unicode(unidecode_orig(arg)) |
Loading…
Reference in new issue