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

1
patacrep/data/templates/songbook_model.yml

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

10
patacrep/songbook/__init__.py

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

37
test/test_content/test_content.py

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

Loading…
Cancel
Save