From becea663ea1cab7305732e16d7dc4d6e74eecbcb Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 23 Sep 2015 20:18:47 +0200 Subject: [PATCH] Silent changes to `seach_*()` methods; using `search_*()` filters in templates --- patacrep/songs/__init__.py | 25 ++++++++++++------- .../chordpro/data/chordpro/content_image | 2 +- .../chordpro/data/chordpro/content_partition | 2 +- .../songs/chordpro/data/chordpro/song_header | 5 +++- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index 0eb0b7e2..792347d3 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -206,16 +206,20 @@ class Song: """Search for a file name. :param str filename: The name, as provided in the chordpro file (with or without extension). - :param list extensions: Possible extensions (with '.') + :param list extensions: Possible extensions (with '.'). Default is no extension. :param iterator directories: Other directories where to search for the file The directory where the Song file is stored is added to the list. Returns None if nothing found. + + This function can also be used as a preprocessor for a renderer: for + instance, it can compile a file, place it in a temporary folder, and + return the path to the compiled file. """ if extensions is None: extensions = [''] if directories is None: - directories = [] + directories = self.config['datadir'] songdir = os.path.dirname(self.fullpath) @@ -228,8 +232,16 @@ class Song: def search_image(self, filename): """Search for an image file""" - datadir_img = self.get_datadirs('img') - filepath = self.search_file(filename, ['', '.jpg', '.png'], datadir_img) + filepath = self.search_file( + filename, + ['', '.jpg', '.png'], + self.get_datadirs('img'), + ) + return filepath if filepath else filename + + def search_partition(self, filename): + """Search for a lilypond file""" + filepath = self.search_file(filename, ['', '.ly']) return filepath if filepath else filename @property @@ -241,11 +253,6 @@ class Song: datadir_img = self.get_datadirs('img') return self.search_file(filename, ['', '.jpg', '.png'], datadir_img) - def search_partition(self, filename): - """Search for a lilypond file""" - filepath = self.search_file(filename, ['', '.ly']) - return filepath if filepath else filename - def unprefixed_title(title, prefixes): """Remove the first prefix of the list in the beginning of title (if any). """ diff --git a/patacrep/songs/chordpro/data/chordpro/content_image b/patacrep/songs/chordpro/data/chordpro/content_image index 58e7f904..4f9bf06c 100644 --- a/patacrep/songs/chordpro/data/chordpro/content_image +++ b/patacrep/songs/chordpro/data/chordpro/content_image @@ -1 +1 @@ -{image: (( content.argument ))} +{image: (( content.argument|search_image ))} diff --git a/patacrep/songs/chordpro/data/chordpro/content_partition b/patacrep/songs/chordpro/data/chordpro/content_partition index 362c4f64..43b8761e 100644 --- a/patacrep/songs/chordpro/data/chordpro/content_partition +++ b/patacrep/songs/chordpro/data/chordpro/content_partition @@ -1 +1 @@ -{partition: ((content.argument))} +{partition: ((content.argument|search_partition))} diff --git a/patacrep/songs/chordpro/data/chordpro/song_header b/patacrep/songs/chordpro/data/chordpro/song_header index b4a59862..d3e65f0b 100644 --- a/patacrep/songs/chordpro/data/chordpro/song_header +++ b/patacrep/songs/chordpro/data/chordpro/song_header @@ -16,11 +16,14 @@ {artist: (( author[1] ))(( author[0] ))} (* endfor *) -(*- for key in ['album', 'copyright', 'cov', 'tag'] *) +(*- for key in ['album', 'copyright', 'tag'] *) (* if key in metadata -*) {(( key )): (( metadata[key] ))} (* endif *) (* endfor *) +(* if 'cov' in metadata -*) + {(( 'cov' )): (( metadata['cov'].argument|search_image ))} +(* endif *) (*- for key in metadata.keys -*) {key: (( key.keyword )): (( key.argument ))}