Browse Source

Refactor songbook preparation

pull/203/head
Oliverpool 9 years ago
parent
commit
51c91cc3a5
  1. 13
      patacrep/content/cwd.py
  2. 1
      patacrep/data/templates/songbook_model.yml
  3. 10
      patacrep/songbook/__init__.py
  4. 37
      test/test_content/test_content.py

13
patacrep/content/cwd.py

@ -1,7 +1,5 @@
"""Change base directory before importing songs."""
import os
from patacrep.content import process_content, validate_parser_argument
from patacrep.songs import DataSubpath
@ -34,12 +32,13 @@ def parse(keyword, config, argument):
"""
subpath = argument['path']
old_songdir = config['_songdir']
sbdir = config['_outputdir']
config['_songdir'] = (
[DataSubpath(sbdir, subpath)] +
[path.clone().join(subpath) for path in config['_songdir']]
)
songdirs = []
if '_songbookfile_dir' in config:
songdirs.extend([DataSubpath(config['_songbookfile_dir'], subpath)])
songdirs.extend(path.clone().join(subpath) for path in config['_songdir'])
config['_songdir'] = songdirs
processed_content = process_content(argument.get('content'), config)
config['_songdir'] = old_songdir
return processed_content

1
patacrep/data/templates/songbook_model.yml

@ -3,6 +3,7 @@ schema:
optional:
content: //any
template: //any
_songbookfile_dir: //str
required:
_cache: //bool
_outputdir: //str

10
patacrep/songbook/__init__.py

@ -55,13 +55,15 @@ def prepare_songbook(songbook, outputdir, outputname, songbookfile_dir=None, dat
:rvalue: dict
:return: Songbook, as a dictionary.
"""
songbook['_songbookfile_dir'] = songbookfile_dir
songbook['_outputdir'] = outputdir
songbook['_outputname'] = outputname
songbook = _add_songbook_defaults(songbook)
# Gathering datadirs
songbook['_datadir'] = list(_iter_absolute_datadirs(songbook, datadir_prefix, songbookfile_dir))
songbook['_datadir'] = list(_iter_absolute_datadirs(songbook, datadir_prefix))
if 'datadir' in songbook['book']:
del songbook['book']['datadir']
@ -97,18 +99,20 @@ def _add_songbook_defaults(user_songbook):
return dict(default_songbook)
def _iter_absolute_datadirs(raw_songbook, datadir_prefix=None, songbookfile_dir=None):
def _iter_absolute_datadirs(raw_songbook, datadir_prefix=None):
"""Iterate on the absolute datadirs of the raw songbook
Appends the songfile dir at the end
"""
datadir = raw_songbook.get('book', {}).get('datadir')
songbookfile_dir = raw_songbook.get('_songbookfile_dir')
if datadir_prefix is None:
if songbookfile_dir is None:
raise patacrep.errors.SongbookError('Please specify where the datadir are located')
datadir_prefix = songbookfile_dir
datadir = raw_songbook.get('book', {}).get('datadir')
if datadir is None:
datadir = []
elif isinstance(datadir, str):

37
test/test_content/test_content.py

@ -9,10 +9,9 @@ import yaml
from pkg_resources import resource_filename
from patacrep.songs import DataSubpath
from patacrep import content, files
from patacrep.content import song, section, songsection, tex
from patacrep.build import config_model
from patacrep.songbook import prepare_songbook
from .. import logging_reduced
from .. import dynamic # pylint: disable=unused-import
@ -28,10 +27,6 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
maxDiff = None
config = None
@classmethod
def setUpClass(cls):
cls._generate_config()
@classmethod
def _iter_testmethods(cls):
"""Iterate over dynamically generated test methods"""
@ -55,8 +50,9 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
with open(sourcename, mode="r", encoding="utf8") as sourcefile:
sbcontent = yaml.load(sourcefile)
config = cls.config.copy()
config['_outputdir'] = os.path.dirname(base)
outputdir = os.path.dirname(base)
config = cls._generate_config(sbcontent, outputdir, base)
with logging_reduced('patacrep.content.song'):
expandedlist = content.process_content(sbcontent, config)
sourcelist = [cls._clean_path(elem) for elem in expandedlist]
@ -97,28 +93,27 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
raise Exception(elem)
@classmethod
def _generate_config(cls):
def _generate_config(cls, sbcontent, outputdir, base):
"""Generate the config to process the content"""
# Load the default songbook config
config = config_model('default')['en']
datadirpaths = [os.path.join(os.path.dirname(__file__), 'datadir')]
config['_datadir'] = datadirpaths
config = prepare_songbook(
{'book':{'datadir':'datadir'}, 'content': sbcontent},
outputdir,
base,
outputdir
)
config['_songdir'] = [
DataSubpath(path, 'songs')
for path in datadirpaths
]
# Load the plugins
config['_content_plugins'] = files.load_plugins(
datadirs=datadirpaths,
datadirs=config['_datadir'],
root_modules=['content'],
keyword='CONTENT_PLUGINS',
)
config['_song_plugins'] = files.load_plugins(
datadirs=datadirpaths,
datadirs=config['_datadir'],
root_modules=['songs'],
keyword='SONG_RENDERERS',
)['tsg']
cls.config = config
return config

Loading…
Cancel
Save