From a42ca249d83ee59498859681120635062b909f8d Mon Sep 17 00:00:00 2001 From: Luthaf Date: Sun, 1 Jun 2014 15:26:16 +0100 Subject: [PATCH] Support de plusieurs datadirs --- songbook | 22 ++++++++++------ songbook_core/build.py | 34 ++++++++++++++----------- songbook_core/data/templates/layout.tex | 6 +++++ songbook_core/data/templates/songs.tex | 8 ++++-- songbook_core/songs.py | 16 +++++++----- songbook_core/templates.py | 22 ++++++++-------- 6 files changed, 65 insertions(+), 43 deletions(-) diff --git a/songbook b/songbook index 678e243e..366f8afd 100755 --- a/songbook +++ b/songbook @@ -108,16 +108,22 @@ def main(): LOGGER.error("Error while loading file '{}'.".format(songbook_path)) sys.exit(1) + datadirs = [] + try: + for path in songbook['datadir']: + if not os.path.isabs(path): + datadirs.append(os.path.join(os.path.dirname(songbook_path), + path + ) + ) + except KeyError: + datadirs = [os.path.dirname(songbook_path)] if options.datadir is not None: - songbook['datadir'] = options.datadir[0] - elif 'datadir' in songbook.keys(): - if not os.path.isabs(songbook['datadir']): - songbook['datadir'] = os.path.join(os.path.dirname(songbook_path), - songbook['datadir'] - ) - else: - songbook['datadir'] = os.path.dirname(songbook_path) + datadirs = [options.datadir[0]].extend(datadirs) + + songbook['datadir'] = datadirs + try: sb_builder = SongbookBuilder(songbook, basename) diff --git a/songbook_core/build.py b/songbook_core/build.py index 4bc399b2..a19ccda2 100755 --- a/songbook_core/build.py +++ b/songbook_core/build.py @@ -57,9 +57,7 @@ class Songbook(object): 'lang': 'english', 'sort': [u"by", u"album", u"@title"], 'content': None, - 'datadir': os.path.abspath('.'), } - self.songslist = None self._parse_raw(raw_songbook) @staticmethod @@ -94,7 +92,7 @@ class Songbook(object): are stored verbatim in self.config. """ self.config.update(raw_songbook) - self.config['datadir'] = os.path.abspath(self.config['datadir']) + self._set_datadir() # Compute song list if self.config['content'] is None: @@ -129,6 +127,23 @@ class Songbook(object): if key not in self.config['authwords']: self.config['authwords'][key] = value + def _set_datadir(self): + """Set the default values for datadir""" + try: + if isinstance(self.config['datadir'], str) or \ + isinstance(self.config['datadir'], unicode): + self.config['datadir'] = [self.config['datadir']] + except KeyError: # No datadir in the raw_songbook + self.config['datadir'] = [os.path.abspath('.')] + + abs_datadir = [] + for path in self.config['datadir']: + abs_datadir.append(os.path.abspath(path)) + + abs_datadir.append(__DATADIR__) + + self.config['datadir'] = abs_datadir + def _parse_songs(self): """Parse content included in songbook.""" self.contentlist = SongbookContent(self.config['datadir']) @@ -186,18 +201,7 @@ class SongbookBuilder(object): return self._called_functions[function] def _set_latex(self): - """Set TEXINPUTS and LaTeX options.""" - if not 'TEXINPUTS' in os.environ.keys(): - os.environ['TEXINPUTS'] = '' - os.environ['TEXINPUTS'] += os.pathsep + os.path.join( - __DATADIR__, - 'latex', - ) - os.environ['TEXINPUTS'] += os.pathsep + os.path.join( - self.songbook.config['datadir'], - 'latex', - ) - + """Set LaTeX options.""" if self.unsafe: self._pdflatex_options.append("--shell-escape") if not self.interactive: diff --git a/songbook_core/data/templates/layout.tex b/songbook_core/data/templates/layout.tex index 9a17feab..58fcbcb9 100644 --- a/songbook_core/data/templates/layout.tex +++ b/songbook_core/data/templates/layout.tex @@ -37,6 +37,12 @@ (* block documentclass *) \documentclass[((mainfontsize))pt]{article} +\makeatletter +\def\input@path{(* for dir in datadir *) + {((dir))/latex/} % + (* endfor *) + } +\makeatother (* endblock *) (* block songbookpackages *) diff --git a/songbook_core/data/templates/songs.tex b/songbook_core/data/templates/songs.tex index 968d3600..2b208f08 100644 --- a/songbook_core/data/templates/songs.tex +++ b/songbook_core/data/templates/songs.tex @@ -48,7 +48,7 @@ "values": {"chorded": {"english": "With guitar chords", "french": "Avec accords de guitare" }, "lyric": {"english": "Lyrics only", "french": "Paroles uniquement"} }, - "default": {"default":"chorded"}, + "default": {"default":"chorded"}, "mandatory": true }, "lang": {"description": {"english": "Language", "french": "Langue"}, @@ -77,7 +77,11 @@ \lang{((lang))} \usepackage{graphicx} - \graphicspath{{((datadir))/img/}} + \graphicspath{(* for dir in datadir *) + {((dir))/img/} % + (* endfor *) + } + \makeatletter \@ifpackageloaded{hyperref}{}{\newcommand{\phantomsection}{}} diff --git a/songbook_core/songs.py b/songbook_core/songs.py index 5446903d..cc4ebbed 100644 --- a/songbook_core/songs.py +++ b/songbook_core/songs.py @@ -90,11 +90,10 @@ def unprefixed_title(title, prefixes): class SongbookContent(object): """Manipulation et traitement de liste de chansons""" - def __init__(self, library): - self._songdir = os.path.join(library, 'songs') - - # Sorted list of the content - self.content = [] + def __init__(self, datadirs): + self.songdirs = [os.path.join(d, 'songs') + for d in datadirs] + self.content = [] # Sorted list of the content def append_song(self, filename): """Ajout d'une chanson à la liste @@ -124,8 +123,11 @@ class SongbookContent(object): if type == "song": # Add all the songs matching the regex before = len(self.content) - for filename in glob.iglob(os.path.join(self._songdir, elem)): - self.append_song(filename) + for songdir in self.songdirs: + for filename in glob.iglob(os.path.join(songdir, elem)): + self.append_song(filename) + if len(self.content) > before: + break if len(self.content) == before: # No songs were added LOGGER.warning( diff --git a/songbook_core/templates.py b/songbook_core/templates.py index f71b2b8a..cc778ded 100644 --- a/songbook_core/templates.py +++ b/songbook_core/templates.py @@ -68,25 +68,25 @@ def _escape_tex(value): class TexRenderer(object): """Render a template to a LaTeX file.""" - def __init__(self, template, datadir, lang): + def __init__(self, template, datadirs, lang): '''Start a new jinja2 environment for .tex creation. Arguments: - template: name of the template to use. - - datadir: location of the data directory (which max contain - file /templates/