Browse Source

Added option 'encoding' to songbook fiels

pull/69/head
Louis 10 years ago
parent
commit
88ba800fb3
  1. 2
      patacrep/build.py
  2. 5
      patacrep/content/include.py
  3. 10
      patacrep/encoding.py
  4. 2
      patacrep/index.py
  5. 4
      patacrep/latex/__init__.py
  6. 10
      patacrep/songbook.py
  7. 2
      patacrep/songs/__init__.py
  8. 2
      patacrep/songs/tex.py
  9. 12
      patacrep/templates.py

2
patacrep/build.py

@ -33,6 +33,7 @@ DEFAULT_CONFIG = {
'lang': 'english', 'lang': 'english',
'content': [], 'content': [],
'titleprefixwords': [], 'titleprefixwords': [],
'encoding': None,
} }
@ -91,6 +92,7 @@ class Songbook(object):
config['template'], config['template'],
config['datadir'], config['datadir'],
config['lang'], config['lang'],
config['encoding'],
) )
config.update(renderer.get_variables()) config.update(renderer.get_variables())
config.update(self.config) config.update(self.config)

5
patacrep/content/include.py

@ -48,7 +48,10 @@ def parse(keyword, config, argument, contentlist):
filepath = load_from_datadirs(path, config) filepath = load_from_datadirs(path, config)
content_file = None content_file = None
try: try:
with encoding.open_read(filepath, 'r') as content_file: with encoding.open_read(
filepath,
encoding=config['encoding']
) as content_file:
new_content = json.load(content_file) new_content = json.load(content_file)
except Exception as error: # pylint: disable=broad-except except Exception as error: # pylint: disable=broad-except
LOGGER.error(error) LOGGER.error(error)

10
patacrep/encoding.py

@ -11,15 +11,21 @@ LOGGER = logging.getLogger(__name__)
@contextlib.contextmanager @contextlib.contextmanager
def open_read(filename, mode='r'): def open_read(filename, mode='r', encoding=None):
"""Open a file for reading, guessing the right encoding. """Open a file for reading, guessing the right encoding.
Return a fileobject, reading unicode strings. Return a fileobject, reading unicode strings.
If `encoding` is set, use it as the encoding (do not guess).
""" """
if encoding is None:
fileencoding = chardet.detect(open(filename, 'rb').read())['encoding']
else:
fileencoding = encoding
with codecs.open( with codecs.open(
filename, filename,
mode=mode, mode=mode,
encoding=chardet.detect(open(filename, 'rb').read())['encoding'], encoding=fileencoding,
errors='replace', errors='replace',
) as fileobject: ) as fileobject:
yield fileobject yield fileobject

2
patacrep/index.py

@ -29,7 +29,7 @@ def process_sxd(filename):
""" """
data = [] data = []
index_file = None index_file = None
with encoding.open_read(filename, 'r') as index_file: with encoding.open_read(filename) as index_file:
for line in index_file: for line in index_file:
data.append(line.strip()) data.append(line.strip())

4
patacrep/latex/__init__.py

@ -11,11 +11,11 @@ from patacrep.latex.syntax import tex2plain
from patacrep.latex.syntax import parsesong as syntax_parsesong from patacrep.latex.syntax import parsesong as syntax_parsesong
from patacrep import encoding from patacrep import encoding
def parsesong(path): def parsesong(path, fileencoding=None):
"""Return a dictonary of data read from the latex file `path`. """Return a dictonary of data read from the latex file `path`.
""" """
with encoding.open_read(path) as songfile: with encoding.open_read(path, encoding=fileencoding) as songfile:
data = syntax_parsesong(songfile.read(), path) data = syntax_parsesong(songfile.read(), path)
data['@path'] = path data['@path'] = path
return data return data

10
patacrep/songbook.py

@ -14,7 +14,7 @@ import sys
from patacrep.build import SongbookBuilder, DEFAULT_STEPS from patacrep.build import SongbookBuilder, DEFAULT_STEPS
from patacrep import __version__ from patacrep import __version__
from patacrep import errors from patacrep import errors
from patacrep import encoding import patacrep.encoding
# Logging configuration # Logging configuration
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
@ -101,8 +101,14 @@ def main():
basename = os.path.basename(songbook_path)[:-3] basename = os.path.basename(songbook_path)[:-3]
try: try:
with encoding.open_read(songbook_path) as songbook_file: with patacrep.encoding.open_read(songbook_path) as songbook_file:
songbook = json.load(songbook_file) songbook = json.load(songbook_file)
if 'encoding' in songbook:
with patacrep.encoding.open_read(
songbook_path,
encoding=songbook['encoding']
) as songbook_file:
songbook = json.load(songbook_file)
except Exception as error: # pylint: disable=broad-except except Exception as error: # pylint: disable=broad-except
LOGGER.error(error) LOGGER.error(error)
LOGGER.error("Error while loading file '{}'.".format(songbook_path)) LOGGER.error("Error while loading file '{}'.".format(songbook_path))

2
patacrep/songs/__init__.py

@ -100,6 +100,8 @@ class Song(Content):
def __init__(self, datadir, subpath, config): def __init__(self, datadir, subpath, config):
self.fullpath = os.path.join(datadir, subpath) self.fullpath = os.path.join(datadir, subpath)
self.datadir = datadir self.datadir = datadir
self.encoding = config["encoding"]
if datadir: if datadir:
# Only songs in datadirs are cached # Only songs in datadirs are cached
self._filehash = hashlib.md5( self._filehash = hashlib.md5(

2
patacrep/songs/tex.py

@ -13,7 +13,7 @@ class TexRenderer(Song):
def parse(self): def parse(self):
"""Parse song and set metadata.""" """Parse song and set metadata."""
self.data = parsesong(self.fullpath) self.data = parsesong(self.fullpath, self.encoding)
self.titles = self.data['@titles'] self.titles = self.data['@titles']
self.languages = self.data['@languages'] self.languages = self.data['@languages']
self.authors = self.data['by'] self.authors = self.data['by']

12
patacrep/templates.py

@ -9,7 +9,8 @@ import os
import re import re
import json import json
from patacrep import encoding, errors, files from patacrep import errors, files
import patacrep.encoding
_LATEX_SUBS = ( _LATEX_SUBS = (
(re.compile(r'\\'), r'\\textbackslash'), (re.compile(r'\\'), r'\\textbackslash'),
@ -67,7 +68,7 @@ def _escape_tex(value):
class TexRenderer(object): class TexRenderer(object):
"""Render a template to a LaTeX file.""" """Render a template to a LaTeX file."""
def __init__(self, template, datadirs, lang): def __init__(self, template, datadirs, lang, encoding=None):
'''Start a new jinja2 environment for .tex creation. '''Start a new jinja2 environment for .tex creation.
Arguments: Arguments:
@ -75,8 +76,10 @@ class TexRenderer(object):
- datadirs: list of locations of the data directory - datadirs: list of locations of the data directory
(which may contain file <datadir>/templates/<template>). (which may contain file <datadir>/templates/<template>).
- lang: main language of songbook. - lang: main language of songbook.
- encoding: if set, encoding of the template.
''' '''
self.lang = lang self.lang = lang
self.encoding = encoding
# Load templates in filesystem ... # Load templates in filesystem ...
loaders = [FileSystemLoader(os.path.join(datadir, 'templates')) loaders = [FileSystemLoader(os.path.join(datadir, 'templates'))
for datadir in datadirs] for datadir in datadirs]
@ -187,7 +190,10 @@ class TexRenderer(object):
subvariables = {} subvariables = {}
templatename = self.texenv.get_template(template).filename templatename = self.texenv.get_template(template).filename
with encoding.open_read(templatename, 'r') as template_file: with patacrep.encoding.open_read(
templatename,
encoding=self.encoding
) as template_file:
content = template_file.read() content = template_file.read()
subtemplates = list(find_templates(self.texenv.parse(content))) subtemplates = list(find_templates(self.texenv.parse(content)))
match = re.findall(_VARIABLE_REGEXP, content) match = re.findall(_VARIABLE_REGEXP, content)

Loading…
Cancel
Save