From 54084a56b4a27ad2792de732bdf4d277794a6066 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sat, 7 Nov 2015 15:20:59 +0100 Subject: [PATCH 01/16] Deprecate output argument of the Song.render method --- patacrep/content/song.py | 3 ++- patacrep/songs/__init__.py | 2 +- patacrep/songs/chordpro/__init__.py | 2 +- patacrep/songs/latex/__init__.py | 8 ++++---- test/test_chordpro/test_parser.py | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/patacrep/content/song.py b/patacrep/content/song.py index 4f9ab809..45bbfeac 100755 --- a/patacrep/content/song.py +++ b/patacrep/content/song.py @@ -33,6 +33,7 @@ class SongRenderer(Content): """Return the string to end a block.""" return r'\end{songs}' + #pylint: disable=unused-argument def render(self, context): """Return the string that will render the song.""" return textwrap.dedent("""\ @@ -43,7 +44,7 @@ class SongRenderer(Content): """).format( separator="%"*80, path=files.path2posix(self.song.subpath), - song=self.song.render(output=context['filename']), + song=self.song.render(), ) def __lt__(self, other): diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index 922ecb00..a13025ef 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -168,7 +168,7 @@ class Song: def __repr__(self): return repr((self.titles, self.data, self.fullpath)) - def render(self, output=None, *args, **kwargs): + def render(self, *args, **kwargs): """Return the code rendering this song. Arguments: diff --git a/patacrep/songs/chordpro/__init__.py b/patacrep/songs/chordpro/__init__.py index 0be23592..dcc573ab 100644 --- a/patacrep/songs/chordpro/__init__.py +++ b/patacrep/songs/chordpro/__init__.py @@ -37,7 +37,7 @@ class ChordproSong(Song): 'song': song, } - def render(self, output=None, template="song"): # pylint: disable=arguments-differ + def render(self, template="song"): # pylint: disable=arguments-differ context = { 'lang': self.lang, "titles": self.titles, diff --git a/patacrep/songs/latex/__init__.py b/patacrep/songs/latex/__init__.py index 8dd8d15b..bed2d037 100644 --- a/patacrep/songs/latex/__init__.py +++ b/patacrep/songs/latex/__init__.py @@ -29,14 +29,14 @@ class Latex2LatexSong(Song): else: self.authors = [] - def render(self, output): + def render(self): """Return the code rendering the song.""" # pylint: disable=signature-differs - if output is None: - raise ValueError(output) + if not self.datadir: + raise ValueError(self.datadir) path = files.path2posix(files.relpath( self.fullpath, - os.path.dirname(output) + os.path.dirname(self.datadir) )) return r'\import{{{}/}}{{{}}}'.format(os.path.dirname(path), os.path.basename(path)) diff --git a/test/test_chordpro/test_parser.py b/test/test_chordpro/test_parser.py index 39c37052..6834af4b 100644 --- a/test/test_chordpro/test_parser.py +++ b/test/test_chordpro/test_parser.py @@ -62,7 +62,7 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest): with disable_logging(): song = self.song_plugins[LANGUAGES[destformat]]['sgc'](sourcename, self.config) self.assertMultiLineEqual( - song.render(output=sourcename).strip(), + song.render().strip(), expectfile.read().strip(), ) From 821c5a1d3a68edbff6a277b36720ca60b3bf21c2 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sat, 7 Nov 2015 15:31:18 +0100 Subject: [PATCH 02/16] Song._parse doesn't use the config argument --- patacrep/songs/__init__.py | 6 +++--- patacrep/songs/chordpro/__init__.py | 2 +- patacrep/songs/latex/__init__.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index a13025ef..08574959 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -132,7 +132,7 @@ class Song: self.data = {} self.cached = None self.lang = None - self._parse(config) + self._parse() # Post processing of data self.subpath = subpath @@ -176,14 +176,14 @@ class Song: """ raise NotImplementedError() - def _parse(self, config): # pylint: disable=no-self-use + def _parse(self): # pylint: disable=no-self-use """Parse song. It set the following attributes: - titles: the list of (raw) titles. This list will be processed to remove prefixes. - - lang: the main language of the song, as language code.. + - lang: the main language of the song, as language code. - authors: the list of (raw) authors. This list will be processed to 'clean' it (see function :func:`patacrep.authors.processauthors`). - data: song metadata. Used (among others) to sort the songs. diff --git a/patacrep/songs/chordpro/__init__.py b/patacrep/songs/chordpro/__init__.py index dcc573ab..a8314548 100644 --- a/patacrep/songs/chordpro/__init__.py +++ b/patacrep/songs/chordpro/__init__.py @@ -25,7 +25,7 @@ class ChordproSong(Song): output_language = None - def _parse(self, config): + def _parse(self): """Parse content, and return the dictionary of song data.""" with encoding.open_read(self.fullpath, encoding=self.encoding) as song: song = parse_song(song.read(), self.fullpath) diff --git a/patacrep/songs/latex/__init__.py b/patacrep/songs/latex/__init__.py index bed2d037..29940f8b 100644 --- a/patacrep/songs/latex/__init__.py +++ b/patacrep/songs/latex/__init__.py @@ -15,7 +15,7 @@ class Latex2LatexSong(Song): """Song written in LaTeX, rendered in LaTeX""" # pylint: disable=abstract-method - def _parse(self, __config): + def _parse(self): """Parse content, and return the dictionary of song data.""" with encoding.open_read(self.fullpath, encoding=self.encoding) as song: self.data = parse_song(song.read(), self.fullpath) From f80b5a62bcee884c28149a270eb2591db9ce0890 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sat, 7 Nov 2015 15:33:02 +0100 Subject: [PATCH 03/16] Move the default_lang out of the config dict --- patacrep/songs/__init__.py | 1 + patacrep/songs/chordpro/__init__.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index 08574959..833f4fe9 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -102,6 +102,7 @@ class Song: self.datadir = datadir self.fullpath = os.path.join(self.datadir, subpath) self.encoding = config["encoding"] + self.default_lang = config["lang"] self.config = config if self.datadir and self.config['_cache']: diff --git a/patacrep/songs/chordpro/__init__.py b/patacrep/songs/chordpro/__init__.py index a8314548..323548e9 100644 --- a/patacrep/songs/chordpro/__init__.py +++ b/patacrep/songs/chordpro/__init__.py @@ -31,7 +31,7 @@ class ChordproSong(Song): song = parse_song(song.read(), self.fullpath) self.authors = song.authors self.titles = song.titles - self.lang = song.get_data_argument('language', self.config['lang']) + self.lang = song.get_data_argument('language', self.default_lang) self.data = song.meta self.cached = { 'song': song, From 9f2b77dd66aa64f413d114459db9a44d5a3320fb Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sat, 7 Nov 2015 15:35:38 +0100 Subject: [PATCH 04/16] Try to be more pythonic --- patacrep/songs/__init__.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index 833f4fe9..3258aa1f 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -96,10 +96,7 @@ class Song: ] def __init__(self, subpath, config, *, datadir=None): - if datadir is None: - self.datadir = "" - else: - self.datadir = datadir + self.datadir = datadir or "" self.fullpath = os.path.join(self.datadir, subpath) self.encoding = config["encoding"] self.default_lang = config["lang"] From 5f3f74a4c49dfd37a0341ea01b9b5163c90f529d Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sat, 7 Nov 2015 15:37:06 +0100 Subject: [PATCH 05/16] The config context is useless for jinja templates --- patacrep/songs/chordpro/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/patacrep/songs/chordpro/__init__.py b/patacrep/songs/chordpro/__init__.py index 323548e9..330b2f3e 100644 --- a/patacrep/songs/chordpro/__init__.py +++ b/patacrep/songs/chordpro/__init__.py @@ -44,7 +44,6 @@ class ChordproSong(Song): "authors": self.authors, "metadata": self.data, "render": self._render_ast, - "config": self.config, "content": self.cached['song'].content, } From 4cf604938460be2432a1bc67b4e4c1b74b37132d Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sat, 7 Nov 2015 15:46:23 +0100 Subject: [PATCH 06/16] Refactor caching functions --- patacrep/songs/__init__.py | 56 ++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index 3258aa1f..6816f534 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -102,28 +102,8 @@ class Song: self.default_lang = config["lang"] self.config = config - if self.datadir and self.config['_cache']: - # Only songs in datadirs are cached - self._filehash = hashlib.md5( - open(self.fullpath, 'rb').read() - ).hexdigest() - if os.path.exists(cached_name(datadir, subpath)): - try: - cached = pickle.load(open( - cached_name(datadir, subpath), - 'rb', - )) - if ( - cached['_filehash'] == self._filehash - and cached['_version'] == self.CACHE_VERSION - ): - for attribute in self.cached_attributes: - setattr(self, attribute, cached[attribute]) - return - except: # pylint: disable=bare-except - LOGGER.warning("Could not use cached version of {}.".format( - self.fullpath - )) + if self.config['_cache'] and self._cache_retrieved(): + return # Data extraction from the latex song self.titles = [] @@ -149,11 +129,39 @@ class Song: # Cache management self._version = self.CACHE_VERSION - self._write_cache() + if self.config['_cache']: + self._write_cache() + + def _cache_retrieved(self): + """If relevant, retrieve self from the cache.""" + if self.datadir: + # Only songs in datadirs are cached + self._filehash = hashlib.md5( + open(self.fullpath, 'rb').read() + ).hexdigest() + if os.path.exists(cached_name(datadir, subpath)): + try: + cached = pickle.load(open( + cached_name(datadir, subpath), + 'rb', + )) + if ( + cached['_filehash'] == self._filehash + and cached['_version'] == self.CACHE_VERSION + ): + for attribute in self.cached_attributes: + setattr(self, attribute, cached[attribute]) + return True + except: # pylint: disable=bare-except + LOGGER.warning("Could not use cached version of {}.".format( + self.fullpath + )) + return False def _write_cache(self): """If relevant, write a dumbed down version of self to the cache.""" - if self.datadir and self.config['_cache']: + if self.datadir: + # Only songs in datadirs can be cached cached = {} for attribute in self.cached_attributes: cached[attribute] = getattr(self, attribute) From 30af0bba20af38a9caf5c3cb37131b75e52da84f Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sat, 7 Nov 2015 15:52:45 +0100 Subject: [PATCH 07/16] Fix and refactor caching functions --- patacrep/songs/__init__.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index 6816f534..6eb3b89d 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -96,13 +96,21 @@ class Song: ] def __init__(self, subpath, config, *, datadir=None): - self.datadir = datadir or "" + if datadir is None: + self.datadir = "" + self.use_cache = False + else: + self.datadir = datadir + # Only songs in datadirs are cached + self.use_cache = ('_cache' in config) + self.fullpath = os.path.join(self.datadir, subpath) + self.subpath = subpath self.encoding = config["encoding"] self.default_lang = config["lang"] self.config = config - if self.config['_cache'] and self._cache_retrieved(): + if self._cache_retrieved(): return # Data extraction from the latex song @@ -113,7 +121,6 @@ class Song: self._parse() # Post processing of data - self.subpath = subpath self.unprefixed_titles = [ unprefixed_title( title, @@ -129,20 +136,18 @@ class Song: # Cache management self._version = self.CACHE_VERSION - if self.config['_cache']: - self._write_cache() + self._write_cache() def _cache_retrieved(self): """If relevant, retrieve self from the cache.""" - if self.datadir: - # Only songs in datadirs are cached + if self.use_cache: self._filehash = hashlib.md5( open(self.fullpath, 'rb').read() ).hexdigest() - if os.path.exists(cached_name(datadir, subpath)): + if os.path.exists(cached_name(self.datadir, self.subpath)): try: cached = pickle.load(open( - cached_name(datadir, subpath), + cached_name(self.datadir, self.subpath), 'rb', )) if ( @@ -160,7 +165,7 @@ class Song: def _write_cache(self): """If relevant, write a dumbed down version of self to the cache.""" - if self.datadir: + if self.use_cache: # Only songs in datadirs can be cached cached = {} for attribute in self.cached_attributes: From 9640b13f701c7c28cc4d02e3801a52aaf8c184da Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sat, 7 Nov 2015 15:58:05 +0100 Subject: [PATCH 08/16] Make cached_name a Song property --- patacrep/songs/__init__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index 6eb3b89d..b267ab0a 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -138,16 +138,21 @@ class Song: self._version = self.CACHE_VERSION self._write_cache() + @property + def cached_name(self): + """Name of the file used for the cache""" + return cached_name(self.datadir, self.subpath) + def _cache_retrieved(self): """If relevant, retrieve self from the cache.""" if self.use_cache: self._filehash = hashlib.md5( open(self.fullpath, 'rb').read() ).hexdigest() - if os.path.exists(cached_name(self.datadir, self.subpath)): + if os.path.exists(self.cached_name): try: cached = pickle.load(open( - cached_name(self.datadir, self.subpath), + self.cached_name, 'rb', )) if ( @@ -172,7 +177,7 @@ class Song: cached[attribute] = getattr(self, attribute) pickle.dump( cached, - open(cached_name(self.datadir, self.subpath), 'wb'), + open(self.cached_name, 'wb'), protocol=-1 ) From 9a31a21c01379e0808920ff471945122b1969ee2 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sat, 7 Nov 2015 16:01:02 +0100 Subject: [PATCH 09/16] Minor refactorisation of cache_retrieved --- patacrep/songs/__init__.py | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index b267ab0a..8d6f8d29 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -98,10 +98,10 @@ class Song: def __init__(self, subpath, config, *, datadir=None): if datadir is None: self.datadir = "" + # Only songs in datadirs may be cached self.use_cache = False else: self.datadir = datadir - # Only songs in datadirs are cached self.use_cache = ('_cache' in config) self.fullpath = os.path.join(self.datadir, subpath) @@ -145,27 +145,26 @@ class Song: def _cache_retrieved(self): """If relevant, retrieve self from the cache.""" - if self.use_cache: + if self.use_cache and os.path.exists(self.cached_name): self._filehash = hashlib.md5( open(self.fullpath, 'rb').read() ).hexdigest() - if os.path.exists(self.cached_name): - try: - cached = pickle.load(open( - self.cached_name, - 'rb', - )) - if ( - cached['_filehash'] == self._filehash - and cached['_version'] == self.CACHE_VERSION - ): - for attribute in self.cached_attributes: - setattr(self, attribute, cached[attribute]) - return True - except: # pylint: disable=bare-except - LOGGER.warning("Could not use cached version of {}.".format( - self.fullpath - )) + try: + cached = pickle.load(open( + self.cached_name, + 'rb', + )) + if ( + cached['_filehash'] == self._filehash + and cached['_version'] == self.CACHE_VERSION + ): + for attribute in self.cached_attributes: + setattr(self, attribute, cached[attribute]) + return True + except: # pylint: disable=bare-except + LOGGER.warning("Could not use cached version of {}.".format( + self.fullpath + )) return False def _write_cache(self): From 4ae7e13326f659ff9f163f45703b0491af727fd8 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sat, 7 Nov 2015 16:15:43 +0100 Subject: [PATCH 10/16] Move the filehash computation into its own (cached) method --- patacrep/songs/__init__.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index 8d6f8d29..60256429 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -88,7 +88,6 @@ class Song: "unprefixed_titles", "cached", "data", - "subpath", "lang", "authors", "_filehash", @@ -106,6 +105,7 @@ class Song: self.fullpath = os.path.join(self.datadir, subpath) self.subpath = subpath + self._filehash = None self.encoding = config["encoding"] self.default_lang = config["lang"] self.config = config @@ -143,19 +143,25 @@ class Song: """Name of the file used for the cache""" return cached_name(self.datadir, self.subpath) - def _cache_retrieved(self): - """If relevant, retrieve self from the cache.""" - if self.use_cache and os.path.exists(self.cached_name): + @property + def filehash(self): + """Compute (and cache) the md5 hash of the file""" + if self._filehash is None: self._filehash = hashlib.md5( open(self.fullpath, 'rb').read() ).hexdigest() + return self._filehash + + def _cache_retrieved(self): + """If relevant, retrieve self from the cache.""" + if self.use_cache and os.path.exists(self.cached_name): try: cached = pickle.load(open( self.cached_name, 'rb', )) if ( - cached['_filehash'] == self._filehash + cached['_filehash'] == self.filehash and cached['_version'] == self.CACHE_VERSION ): for attribute in self.cached_attributes: From 28cc48541bd3b1f9c4a0de248530fa0919f08e23 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sat, 7 Nov 2015 16:17:12 +0100 Subject: [PATCH 11/16] Use cache only if True --- patacrep/songs/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index 60256429..16f3f829 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -101,7 +101,7 @@ class Song: self.use_cache = False else: self.datadir = datadir - self.use_cache = ('_cache' in config) + self.use_cache = ('_cache' in config) and config['_cache'] self.fullpath = os.path.join(self.datadir, subpath) self.subpath = subpath From 8516f3bfa02483d2ac8732c4bfe94717c0403ab1 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sat, 7 Nov 2015 16:20:12 +0100 Subject: [PATCH 12/16] Update cache version --- patacrep/songs/__init__.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index 16f3f829..b9e4afb9 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -80,7 +80,7 @@ class Song: # Version format of cached song. Increment this number if we update # information stored in cache. - CACHE_VERSION = 2 + CACHE_VERSION = 3 # List of attributes to cache cached_attributes = [ @@ -156,10 +156,7 @@ class Song: """If relevant, retrieve self from the cache.""" if self.use_cache and os.path.exists(self.cached_name): try: - cached = pickle.load(open( - self.cached_name, - 'rb', - )) + cached = pickle.load(open(self.cached_name, 'rb',)) if ( cached['_filehash'] == self.filehash and cached['_version'] == self.CACHE_VERSION From f537c9a16c0ff3cb0d0c9d1543102f97b6857a0e Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sat, 7 Nov 2015 16:26:47 +0100 Subject: [PATCH 13/16] Use dict comprehension to generate cached data --- patacrep/songs/__init__.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index b9e4afb9..52007ff4 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -173,10 +173,7 @@ class Song: def _write_cache(self): """If relevant, write a dumbed down version of self to the cache.""" if self.use_cache: - # Only songs in datadirs can be cached - cached = {} - for attribute in self.cached_attributes: - cached[attribute] = getattr(self, attribute) + cached = {attr: getattr(self, attr) for attr in self.cached_attributes} pickle.dump( cached, open(self.cached_name, 'wb'), From 9db2a86c72d7e5c4307c168b8a26ca94eee14838 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sat, 7 Nov 2015 16:30:35 +0100 Subject: [PATCH 14/16] Be more pythonic --- patacrep/songs/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index 52007ff4..d19e22df 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -101,7 +101,7 @@ class Song: self.use_cache = False else: self.datadir = datadir - self.use_cache = ('_cache' in config) and config['_cache'] + self.use_cache = config.get('_cache', False) self.fullpath = os.path.join(self.datadir, subpath) self.subpath = subpath From 66e2e4090167176f67b201ae81a4862b92034994 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sun, 8 Nov 2015 08:49:31 +0100 Subject: [PATCH 15/16] Correctly (?) include the datadir to allow \import --- patacrep/data/templates/layout.tex | 5 +++++ patacrep/songs/latex/__init__.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/patacrep/data/templates/layout.tex b/patacrep/data/templates/layout.tex index 099772a4..135aba1b 100644 --- a/patacrep/data/templates/layout.tex +++ b/patacrep/data/templates/layout.tex @@ -27,9 +27,14 @@ \makeatletter \def\input@path{ % + % include .sty folders (* for dir in datadir *) {(( path2posix(dir) ))/latex/} % (* endfor *) + % include datadir folders (for \import) + (* for dir in datadir *) + {(( path2posix(dir) ))/} % + (* endfor *) } \makeatother diff --git a/patacrep/songs/latex/__init__.py b/patacrep/songs/latex/__init__.py index 29940f8b..86c72400 100644 --- a/patacrep/songs/latex/__init__.py +++ b/patacrep/songs/latex/__init__.py @@ -36,7 +36,7 @@ class Latex2LatexSong(Song): raise ValueError(self.datadir) path = files.path2posix(files.relpath( self.fullpath, - os.path.dirname(self.datadir) + self.datadir )) return r'\import{{{}/}}{{{}}}'.format(os.path.dirname(path), os.path.basename(path)) From ff07c71d16f43289b996341c72d25abc35814b32 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Sun, 8 Nov 2015 09:19:34 +0100 Subject: [PATCH 16/16] Use absolute import for LaTeX songs --- patacrep/data/templates/layout.tex | 7 +------ patacrep/data/templates/songs.tex | 8 ++++---- patacrep/data/templates/songs/chordpro/latex/song | 2 +- patacrep/songs/latex/__init__.py | 10 +++------- patacrep/templates.py | 4 ++-- test/test_compilation/datadir.tex.control | 8 ++++---- 6 files changed, 15 insertions(+), 24 deletions(-) diff --git a/patacrep/data/templates/layout.tex b/patacrep/data/templates/layout.tex index 135aba1b..28dba5e8 100644 --- a/patacrep/data/templates/layout.tex +++ b/patacrep/data/templates/layout.tex @@ -27,13 +27,8 @@ \makeatletter \def\input@path{ % - % include .sty folders (* for dir in datadir *) - {(( path2posix(dir) ))/latex/} % - (* endfor *) - % include datadir folders (for \import) - (* for dir in datadir *) - {(( path2posix(dir) ))/} % + {(( dir | path2posix ))/latex/} % (* endfor *) } \makeatother diff --git a/patacrep/data/templates/songs.tex b/patacrep/data/templates/songs.tex index e3562897..da48fdd7 100644 --- a/patacrep/data/templates/songs.tex +++ b/patacrep/data/templates/songs.tex @@ -81,15 +81,15 @@ (( super() )) (* for lang in _langs|sort -*) - \PassOptionsToPackage{(( lang2babel(lang) ))}{babel} + \PassOptionsToPackage{(( lang | lang2babel ))}{babel} (* endfor *) -\usepackage[(( lang2babel(lang) ))]{babel} -\lang{(( lang2babel(lang) ))} +\usepackage[(( lang | lang2babel ))]{babel} +\lang{(( lang | lang2babel ))} \usepackage{graphicx} \graphicspath{ % (* for dir in datadir *) - {(( path2posix(dir) ))/} % + {(( dir | path2posix ))/} % (* endfor *) } diff --git a/patacrep/data/templates/songs/chordpro/latex/song b/patacrep/data/templates/songs/chordpro/latex/song index e8016107..cb29cc29 100644 --- a/patacrep/data/templates/songs/chordpro/latex/song +++ b/patacrep/data/templates/songs/chordpro/latex/song @@ -1,5 +1,5 @@ (* if lang is defined -*) - \selectlanguage{(( lang2babel(lang) ))} + \selectlanguage{(( lang | lang2babel ))} (* endif *) (*- if metadata.columns is defined *) diff --git a/patacrep/songs/latex/__init__.py b/patacrep/songs/latex/__init__.py index 86c72400..d19cd10e 100644 --- a/patacrep/songs/latex/__init__.py +++ b/patacrep/songs/latex/__init__.py @@ -32,13 +32,9 @@ class Latex2LatexSong(Song): def render(self): """Return the code rendering the song.""" # pylint: disable=signature-differs - if not self.datadir: - raise ValueError(self.datadir) - path = files.path2posix(files.relpath( - self.fullpath, - self.datadir - )) - return r'\import{{{}/}}{{{}}}'.format(os.path.dirname(path), os.path.basename(path)) + filename = os.path.basename(self.fullpath) + path = os.path.abspath(os.path.dirname(self.fullpath)) + return r'\import{{{}/}}{{{}}}'.format(files.path2posix(path), filename) def set_lang(self, language): """Set the language code""" diff --git a/patacrep/templates.py b/patacrep/templates.py index 6a799295..612a99e4 100644 --- a/patacrep/templates.py +++ b/patacrep/templates.py @@ -84,8 +84,8 @@ class Renderer: self.jinjaenv.filters['escape_tex'] = _escape_tex self.jinjaenv.trim_blocks = True self.jinjaenv.lstrip_blocks = True - self.jinjaenv.globals["path2posix"] = files.path2posix - self.jinjaenv.globals["lang2babel"] = lang2babel + self.jinjaenv.filters["path2posix"] = files.path2posix + self.jinjaenv.filters["lang2babel"] = lang2babel self.template = self.jinjaenv.get_template(template) diff --git a/test/test_compilation/datadir.tex.control b/test/test_compilation/datadir.tex.control index a003cd42..14b11cb1 100644 --- a/test/test_compilation/datadir.tex.control +++ b/test/test_compilation/datadir.tex.control @@ -92,7 +92,7 @@ guitar, %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% songs/./datadir.sg -\import{datadir_datadir/songs/}{datadir.sg} +\import{@TEST_FOLDER@/datadir_datadir/songs/}{datadir.sg} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% songs/./datadir.sgc @@ -119,7 +119,7 @@ Chordpro}[ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% songs/./datadir2.sg -\import{datadir_datadir/songs/}{datadir2.sg} +\import{@TEST_FOLDER@/datadir_datadir/songs/}{datadir2.sg} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% songs/./datadir2.sgc @@ -146,7 +146,7 @@ Chordpro}[ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% songs/./relative.sg -\import{datadir_datadir/songs/}{relative.sg} +\import{@TEST_FOLDER@/datadir_datadir/songs/}{relative.sg} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% songs/./relative.sgc @@ -173,7 +173,7 @@ Chordpro}[ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% songs/./subdir/subdir.sg -\import{datadir_datadir/songs/subdir/}{subdir.sg} +\import{@TEST_FOLDER@/datadir_datadir/songs/subdir/}{subdir.sg} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% songs/./subdir/subdir.sgc