Browse Source

Append default datadir to each datadir list (or iterator)

pull/175/head
Louis 9 years ago
parent
commit
46f3790a89
  1. 4
      patacrep/build.py
  2. 12
      patacrep/content/include.py
  3. 4
      patacrep/content/tex.py
  4. 2
      patacrep/data/templates/layout.tex
  5. 2
      patacrep/data/templates/songs.tex
  6. 12
      patacrep/files.py
  7. 8
      patacrep/songs/__init__.py
  8. 6
      patacrep/templates.py
  9. 3
      test/test_chordpro/test_parser.py

4
patacrep/build.py

@ -8,7 +8,7 @@ import threading
import os.path import os.path
from subprocess import Popen, PIPE, call, check_call 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.index import process_sxd
from patacrep.templates import TexBookRenderer from patacrep.templates import TexBookRenderer
from patacrep.songs import DataSubpath from patacrep.songs import DataSubpath
@ -71,8 +71,6 @@ class Songbook(object):
"Ignoring non-existent datadir '{}'.".format(path) "Ignoring non-existent datadir '{}'.".format(path)
) )
abs_datadir.append(__DATADIR__)
self.config['datadir'] = abs_datadir self.config['datadir'] = abs_datadir
self.config['_songdir'] = [ self.config['_songdir'] = [
DataSubpath(path, 'songs') DataSubpath(path, 'songs')

12
patacrep/content/include.py

@ -10,24 +10,22 @@ import sys
import logging import logging
from patacrep.content import process_content, ContentError from patacrep.content import process_content, ContentError
from patacrep import encoding from patacrep import encoding, errors, files
from patacrep import errors
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
def load_from_datadirs(path, config=None): def load_from_datadirs(path, datadirs):
"""Load 'path' from one of the datadirs. """Load 'path' from one of the datadirs.
Raise an exception if it was found if none of the datadirs of 'config'. Raise an exception if it was found if none of the datadirs of 'config'.
""" """
for datadir in config.get("datadir", []): for filepath in files.iter_datadirs(datadirs, path):
filepath = os.path.join(datadir, path)
if os.path.exists(filepath): if os.path.exists(filepath):
return filepath return filepath
# File not found # File not found
raise ContentError( raise ContentError(
"include", "include",
errors.notfound(path, config.get("datadir", [])), errors.notfound(path, list(files.iter_datadirs(datadirs)))
) )
#pylint: disable=unused-argument #pylint: disable=unused-argument
@ -43,7 +41,7 @@ def parse(keyword, config, argument, contentlist):
new_contentlist = [] new_contentlist = []
for path in contentlist: for path in contentlist:
filepath = load_from_datadirs(path, config) filepath = load_from_datadirs(path, config.get('datadir', []))
content_file = None content_file = None
try: try:
with encoding.open_read( with encoding.open_read(

4
patacrep/content/tex.py

@ -36,8 +36,8 @@ def parse(keyword, argument, contentlist, config):
) )
filelist = [] filelist = []
basefolders = [path.fullpath for path in config['_songdir']] +\ basefolders = [path.fullpath for path in config['_songdir']] +\
config['datadir'] + \ list(files.iter_datadirs(config['datadir'])) + \
[os.path.join(path, "latex") for path in config['datadir']] list(files.iter_datadirs(config['datadir'], 'latex'))
for filename in contentlist: for filename in contentlist:
checked_file = None checked_file = None
for path in basefolders: for path in basefolders:

2
patacrep/data/templates/layout.tex

@ -27,7 +27,7 @@
\makeatletter \makeatletter
\def\input@path{ % \def\input@path{ %
(* for dir in datadir *) (* for dir in datadir|iter_datadirs *)
{(( dir | path2posix ))/latex/} % {(( dir | path2posix ))/latex/} %
(* endfor *) (* endfor *)
} }

2
patacrep/data/templates/songs.tex

@ -88,7 +88,7 @@
\usepackage{graphicx} \usepackage{graphicx}
\graphicspath{ % \graphicspath{ %
(* for dir in datadir *) (* for dir in datadir|iter_datadirs*)
{(( dir | path2posix ))/} % {(( dir | path2posix ))/} %
(* endfor *) (* endfor *)
} }

12
patacrep/files.py

@ -9,6 +9,7 @@ import re
import sys import sys
from patacrep import utils from patacrep import utils
from patacrep import __DATADIR__
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -129,3 +130,14 @@ def load_plugins(datadirs, root_modules, keyword):
if hasattr(module, keyword): if hasattr(module, keyword):
plugins.update(getattr(module, keyword)) plugins.update(getattr(module, keyword))
return plugins 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)

8
patacrep/songs/__init__.py

@ -2,14 +2,13 @@
import errno import errno
import hashlib import hashlib
import jinja2
import logging import logging
import os import os
import pickle import pickle
import re import re
from patacrep.authors import process_listauthors
from patacrep import files, encoding from patacrep import files, encoding
from patacrep.authors import process_listauthors
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -210,10 +209,7 @@ class Song:
def iter_datadirs(self, *subpath): def iter_datadirs(self, *subpath):
"""Return an iterator of existing datadirs (with an optionnal subpath) """Return an iterator of existing datadirs (with an optionnal subpath)
""" """
for directory in self.config['datadir']: yield from files.iter_datadirs(self.config['datadir'], *subpath)
fullpath = os.path.join(directory, *subpath)
if os.path.isdir(fullpath):
yield fullpath
def search_datadir_file(self, filename, extensions=None, directories=None): def search_datadir_file(self, filename, extensions=None, directories=None):
"""Search for a file name. """Search for a file name.

6
patacrep/templates.py

@ -4,7 +4,6 @@ from jinja2 import Environment, FileSystemLoader, ChoiceLoader, \
TemplateNotFound, nodes TemplateNotFound, nodes
from jinja2.ext import Extension from jinja2.ext import Extension
from jinja2.meta import find_referenced_templates as find_templates from jinja2.meta import find_referenced_templates as find_templates
import os
import re import re
import json import json
@ -85,6 +84,7 @@ class Renderer:
self.jinjaenv.trim_blocks = True self.jinjaenv.trim_blocks = True
self.jinjaenv.lstrip_blocks = True self.jinjaenv.lstrip_blocks = True
self.jinjaenv.filters["path2posix"] = files.path2posix self.jinjaenv.filters["path2posix"] = files.path2posix
self.jinjaenv.filters["iter_datadirs"] = files.iter_datadirs
self.jinjaenv.filters["lang2babel"] = lang2babel self.jinjaenv.filters["lang2babel"] = lang2babel
self.template = self.jinjaenv.get_template(template) self.template = self.jinjaenv.get_template(template)
@ -105,8 +105,8 @@ class TexBookRenderer(Renderer):
self.lang = lang self.lang = lang
# Load templates in filesystem ... # Load templates in filesystem ...
loaders = [ loaders = [
FileSystemLoader(os.path.join(datadir, 'templates')) FileSystemLoader(datadir)
for datadir in datadirs for datadir in files.iter_datadirs(datadirs, 'templates')
] ]
jinjaenv = Environment( jinjaenv = Environment(
loader=ChoiceLoader(loaders), loader=ChoiceLoader(loaders),

3
test/test_chordpro/test_parser.py

@ -8,7 +8,7 @@ import os
import unittest import unittest
from pkg_resources import resource_filename from pkg_resources import resource_filename
from patacrep import files, __DATADIR__ from patacrep import files
from patacrep.build import DEFAULT_CONFIG from patacrep.build import DEFAULT_CONFIG
from patacrep.encoding import open_read from patacrep.encoding import open_read
@ -74,7 +74,6 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
if 'datadir' not in cls.config: if 'datadir' not in cls.config:
cls.config['datadir'] = [] cls.config['datadir'] = []
cls.config['datadir'].append('datadir') cls.config['datadir'].append('datadir')
cls.config['datadir'].append(__DATADIR__)
cls.song_plugins = files.load_plugins( cls.song_plugins = files.load_plugins(
datadirs=cls.config['datadir'], datadirs=cls.config['datadir'],

Loading…
Cancel
Save