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
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')

12
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(

4
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:

2
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 *)
}

2
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 *)
}

12
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)

8
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.

6
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),

3
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'],

Loading…
Cancel
Save