From 414716cdba6db42a7122ed01dea6e82cc8609538 Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 14 Jun 2014 16:47:35 +0200 Subject: [PATCH] Sorted out configuration defaults (between hard-coded defaults, templates, and configuration files) --- songbook_core/authors.py | 28 +++++++++++++++++ songbook_core/build.py | 67 +++++++++++++--------------------------- songbook_core/index.py | 26 ++-------------- 3 files changed, 52 insertions(+), 69 deletions(-) diff --git a/songbook_core/authors.py b/songbook_core/authors.py index cea20439..3f58b5af 100644 --- a/songbook_core/authors.py +++ b/songbook_core/authors.py @@ -3,6 +3,34 @@ """Authors string management.""" +import re + +DEFAULT_AUTHWORDS = { + "after": ["by"], + "ignore": ["unknown"], + "sep": ["and"], + } + +def compile_authwords(authwords): + # Convert strings to regular expressions + # Fill holes + for (key, value) in DEFAULT_AUTHWORDS.items(): + if key not in authwords: + authwords[key] = value + + # Compilation + authwords['after'] = [ + re.compile(r"^.*%s\b(.*)" % word) + for word in authwords['after'] + ] + authwords['sep'] = [ + re.compile(r"^(.*)%s (.*)$" % word) + for word in ([" %s" % word for word in authwords['sep']] + [',']) + ] + + return authwords + + def split_author_names(string): r"""Split author between first and last name. diff --git a/songbook_core/build.py b/songbook_core/build.py index bc257aa7..deeb5d55 100755 --- a/songbook_core/build.py +++ b/songbook_core/build.py @@ -11,6 +11,7 @@ import re from subprocess import Popen, PIPE, call from songbook_core import __DATADIR__ +from songbook_core import authors from songbook_core import content from songbook_core import errors from songbook_core.index import process_sxd @@ -19,11 +20,6 @@ from songbook_core.templates import TexRenderer LOGGER = logging.getLogger(__name__) EOL = "\n" -DEFAULT_AUTHWORDS = { - "after": ["by"], - "ignore": ["unknown"], - "sep": ["and"], - } DEFAULT_STEPS = ['tex', 'pdf', 'sbx', 'pdf', 'clean'] GENERATED_EXTENSIONS = [ "_auth.sbx", @@ -36,6 +32,12 @@ GENERATED_EXTENSIONS = [ "_title.sbx", "_title.sxd", ] +DEFAULT_CONFIG = { + 'template': "default.tex", + 'lang': 'english', + 'content': [], + 'titleprefixwords': [], + } @@ -50,46 +52,10 @@ class Songbook(object): def __init__(self, raw_songbook, basename): super(Songbook, self).__init__() + self.config = raw_songbook self.basename = basename - # Default values: will be updated while parsing raw_songbook - self.config = { - 'template': "default.tex", - 'lang': 'english', - 'sort': [u"by", u"album", u"@title"], - 'content': None, - 'titleprefixwords': [], - } - self._parse_raw(raw_songbook) - - - def _parse_raw(self, raw_songbook): - """Parse raw_songbook. - - The principle is: some special keys have their value processed; others - are stored verbatim in self.config. - """ - self.config.update(raw_songbook) + # Some special keys have their value processed. self._set_datadir() - self._set_authwords() - - - def _set_authwords(self): - # Ensure self.config['authwords'] contains all entries - for (key, value) in DEFAULT_AUTHWORDS.items(): - if key not in self.config['authwords']: - self.config['authwords'][key] = value - # Convert strings to regular expressions - self.config["authwords"]['after'] = [ - re.compile(r"^.*%s\b(.*)" % after) - for after - in self.config['authwords']["after"] - ] - self.config["authwords"]['sep'] = [ - re.compile(r"^(.*)%s (.*)$" % sep) - for sep in ([ - " %s" % sep for sep in self.config['authwords']["sep"] - ] + [',']) - ] def _set_datadir(self): """Set the default values for datadir""" @@ -110,21 +76,30 @@ class Songbook(object): self.config['datadir'] = abs_datadir + def build_config(self, from_templates): + config = DEFAULT_CONFIG + config.update(from_templates) + config.update(self.config) + + # Post-processing + config['authwords'] = authors.compile_authwords(config['authwords']) + + return config + def write_tex(self, output): """Build the '.tex' file corresponding to self. Arguments: - output: a file object, in which the file will be written. """ - self.contentlist = content.process_content(self.config['content'], self.config) renderer = TexRenderer( self.config['template'], self.config['datadir'], self.config['lang'], ) - context = renderer.get_variables() - context.update(self.config) + context = self.build_config(renderer.get_variables()) + self.contentlist = content.process_content(self.config['content'], context) context['render_content'] = content.render_content context['titleprefixkeys'] = ["after", "sep", "ignore"] context['content'] = self.contentlist diff --git a/songbook_core/index.py b/songbook_core/index.py index 78b64165..734e4ec3 100755 --- a/songbook_core/index.py +++ b/songbook_core/index.py @@ -13,7 +13,7 @@ import locale import re import codecs -from songbook_core.authors import processauthors +from songbook_core import authors from songbook_core.plastex import simpleparse EOL = u"\n" @@ -66,7 +66,6 @@ class Index(object): self.data = dict() self.keywords = dict() self.prefix_patterns = [] - self.authwords = {"after": [], "ignore": [], "sep": []} if indextype == "TITLE INDEX DATA FILE": self.indextype = "TITLE" elif indextype == "SCRIPTURE INDEX DATA FILE": @@ -100,26 +99,7 @@ class Index(object): )) if self.indextype == "AUTHOR": - for key in self.keywords: - if key in self.authwords: - self.authwords[key] = self.keywords[key] - for word in self.authwords.keys(): - if word in self.keywords: - if word == "after": - self.authwords[word] = [ - re.compile(r"^.*{after}\b(.*)".format(after=after)) - for after in self.keywords[word] - ] - elif word == "sep": - self.authwords[word] = [" {sep}".format(sep=sep) - for sep in self.authwords[word] - ] + [","] - self.authwords[word] = [ - re.compile(r"^(.*){sep} (.*)$".format(sep=sep)) - for sep in self.authwords[word] - ] - else: - self.authwords[word] = self.keywords[word] + self.authwords = authors.compile_authwords(self.keywords) def _raw_add(self, key, number, link): """Add a song to the list. @@ -157,7 +137,7 @@ class Index(object): if self.indextype == "AUTHOR": # Processing authors - for author in processauthors( + for author in authors.processauthors( key, **self.authwords): self._raw_add(author, number, link)