From 56c7c39b05f2cd0203315a525c4f3e28935cc188 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Tue, 22 Sep 2015 15:07:22 +0200 Subject: [PATCH] Allow custom path to song templates --- patacrep/songs/chordpro/__init__.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/patacrep/songs/chordpro/__init__.py b/patacrep/songs/chordpro/__init__.py index f6c12ad3..30264445 100644 --- a/patacrep/songs/chordpro/__init__.py +++ b/patacrep/songs/chordpro/__init__.py @@ -14,6 +14,15 @@ class ChordproSong(Song): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + self.template_paths = [os.path.abspath(pkg_resources.resource_filename(__name__, 'data'))] + + def add_template_path(self, absolute_path): + """Add a template path (at the beginning, so that it's choosen first).""" + self.template_paths.insert(0, absolute_path) + + def get_template_paths(self, output_format): + """Get the template path for a given output_format.""" + return [os.path.join(path, output_format) for path in self.template_paths] def parse(self, config): """Parse content, and return the dictionary of song data.""" @@ -40,10 +49,9 @@ class ChordproSong(Song): "config": self.config, "content": content, } - jinjaenv = Environment(loader=FileSystemLoader(os.path.join( - os.path.abspath(pkg_resources.resource_filename(__name__, 'data')), - output_format, - ))) + jinjaenv = Environment(loader=FileSystemLoader( + self.get_template_paths(output_format) + )) jinjaenv.filters['search_image'] = self.search_image jinjaenv.filters['search_partition'] = self.search_partition