diff --git a/patacrep/build.py b/patacrep/build.py index a720b9b1..0342959b 100644 --- a/patacrep/build.py +++ b/patacrep/build.py @@ -8,7 +8,7 @@ import threading import os.path from subprocess import Popen, PIPE, call, check_call -from patacrep import __DATADIR__, authors, content, errors, files +from patacrep import authors, content, errors, files from patacrep.index import process_sxd from patacrep.templates import TexBookRenderer from patacrep.songs import DataSubpath @@ -71,8 +71,6 @@ class Songbook(object): "Ignoring non-existent datadir '{}'.".format(path) ) - abs_datadir.append(__DATADIR__) - self.config['datadir'] = abs_datadir self.config['_songdir'] = [ DataSubpath(path, 'songs') diff --git a/patacrep/content/include.py b/patacrep/content/include.py index 73bfa82a..30c99238 100644 --- a/patacrep/content/include.py +++ b/patacrep/content/include.py @@ -10,24 +10,22 @@ import sys import logging from patacrep.content import process_content, ContentError -from patacrep import encoding -from patacrep import errors +from patacrep import encoding, errors, files LOGGER = logging.getLogger(__name__) -def load_from_datadirs(path, config=None): +def load_from_datadirs(path, datadirs): """Load 'path' from one of the datadirs. Raise an exception if it was found if none of the datadirs of 'config'. """ - for datadir in config.get("datadir", []): - filepath = os.path.join(datadir, path) + for filepath in files.iter_datadirs(datadirs, path): if os.path.exists(filepath): return filepath # File not found raise ContentError( "include", - errors.notfound(path, config.get("datadir", [])), + errors.notfound(path, list(files.iter_datadirs(datadirs))) ) #pylint: disable=unused-argument @@ -43,7 +41,7 @@ def parse(keyword, config, argument, contentlist): new_contentlist = [] for path in contentlist: - filepath = load_from_datadirs(path, config) + filepath = load_from_datadirs(path, config.get('datadir', [])) content_file = None try: with encoding.open_read( diff --git a/patacrep/content/tex.py b/patacrep/content/tex.py index 8bb7236d..5c9f7425 100755 --- a/patacrep/content/tex.py +++ b/patacrep/content/tex.py @@ -36,8 +36,8 @@ def parse(keyword, argument, contentlist, config): ) filelist = [] basefolders = [path.fullpath for path in config['_songdir']] +\ - config['datadir'] + \ - [os.path.join(path, "latex") for path in config['datadir']] + list(files.iter_datadirs(config['datadir'])) + \ + list(files.iter_datadirs(config['datadir'], 'latex')) for filename in contentlist: checked_file = None for path in basefolders: diff --git a/patacrep/data/templates/layout.tex b/patacrep/data/templates/layout.tex index 28dba5e8..63227530 100644 --- a/patacrep/data/templates/layout.tex +++ b/patacrep/data/templates/layout.tex @@ -27,7 +27,7 @@ \makeatletter \def\input@path{ % - (* for dir in datadir *) + (* for dir in datadir|iter_datadirs *) {(( dir | path2posix ))/latex/} % (* endfor *) } diff --git a/patacrep/data/templates/songs.tex b/patacrep/data/templates/songs.tex index da48fdd7..07ed0f33 100644 --- a/patacrep/data/templates/songs.tex +++ b/patacrep/data/templates/songs.tex @@ -88,7 +88,7 @@ \usepackage{graphicx} \graphicspath{ % - (* for dir in datadir *) + (* for dir in datadir|iter_datadirs*) {(( dir | path2posix ))/} % (* endfor *) } diff --git a/patacrep/files.py b/patacrep/files.py index a204f8e7..b3d2f25a 100644 --- a/patacrep/files.py +++ b/patacrep/files.py @@ -9,6 +9,7 @@ import re import sys from patacrep import utils +from patacrep import __DATADIR__ LOGGER = logging.getLogger(__name__) @@ -129,3 +130,14 @@ def load_plugins(datadirs, root_modules, keyword): if hasattr(module, keyword): plugins.update(getattr(module, keyword)) return plugins + +def iter_datadirs(datadirs, *subpath): + """Iterate over datadirs. + + The default datadir is returned last. + + Subpath are appended after each datadir. + """ + for path in datadirs: + yield os.path.join(path, *subpath) + yield os.path.join(__DATADIR__, *subpath) diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index d19e22df..2b1a5768 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -2,14 +2,13 @@ import errno import hashlib -import jinja2 import logging import os import pickle import re -from patacrep.authors import process_listauthors from patacrep import files, encoding +from patacrep.authors import process_listauthors LOGGER = logging.getLogger(__name__) @@ -210,10 +209,7 @@ class Song: def iter_datadirs(self, *subpath): """Return an iterator of existing datadirs (with an optionnal subpath) """ - for directory in self.config['datadir']: - fullpath = os.path.join(directory, *subpath) - if os.path.isdir(fullpath): - yield fullpath + yield from files.iter_datadirs(self.config['datadir'], *subpath) def search_datadir_file(self, filename, extensions=None, directories=None): """Search for a file name. diff --git a/patacrep/templates.py b/patacrep/templates.py index 612a99e4..b329cc71 100644 --- a/patacrep/templates.py +++ b/patacrep/templates.py @@ -4,7 +4,6 @@ from jinja2 import Environment, FileSystemLoader, ChoiceLoader, \ TemplateNotFound, nodes from jinja2.ext import Extension from jinja2.meta import find_referenced_templates as find_templates -import os import re import json @@ -85,6 +84,7 @@ class Renderer: self.jinjaenv.trim_blocks = True self.jinjaenv.lstrip_blocks = True self.jinjaenv.filters["path2posix"] = files.path2posix + self.jinjaenv.filters["iter_datadirs"] = files.iter_datadirs self.jinjaenv.filters["lang2babel"] = lang2babel self.template = self.jinjaenv.get_template(template) @@ -105,8 +105,8 @@ class TexBookRenderer(Renderer): self.lang = lang # Load templates in filesystem ... loaders = [ - FileSystemLoader(os.path.join(datadir, 'templates')) - for datadir in datadirs + FileSystemLoader(datadir) + for datadir in files.iter_datadirs(datadirs, 'templates') ] jinjaenv = Environment( loader=ChoiceLoader(loaders), diff --git a/test/test_chordpro/test_parser.py b/test/test_chordpro/test_parser.py index 6834af4b..2be90ce5 100644 --- a/test/test_chordpro/test_parser.py +++ b/test/test_chordpro/test_parser.py @@ -8,7 +8,7 @@ import os import unittest from pkg_resources import resource_filename -from patacrep import files, __DATADIR__ +from patacrep import files from patacrep.build import DEFAULT_CONFIG from patacrep.encoding import open_read @@ -74,7 +74,6 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest): if 'datadir' not in cls.config: cls.config['datadir'] = [] cls.config['datadir'].append('datadir') - cls.config['datadir'].append(__DATADIR__) cls.song_plugins = files.load_plugins( datadirs=cls.config['datadir'],