Browse Source

Merge pull request #207 from patacrep/content_cd

`cd` evolution (content plugin)
pull/211/head
oliverpool 9 years ago
parent
commit
284f896c22
  1. 2
      patacrep/content/__init__.py
  2. 22
      patacrep/content/addsongdir.py
  3. 36
      patacrep/content/cd.py
  4. 19
      patacrep/content/include.py
  5. 1
      patacrep/content/tex.py
  6. 2
      test/test_content/cwd.source
  7. 2
      test/test_content/cwd_list.source
  8. 2
      test/test_content/sort.source
  9. 6
      test/test_songbook/content.yaml
  10. 4
      test/test_songbook/onthefly/content.onthefly.yaml

2
patacrep/content/__init__.py

@ -21,7 +21,7 @@ met, the corresponding parser is called.
- sort
- section*
- cwd
- cd
# Parsers

22
patacrep/content/cwd.py → patacrep/content/addsongdir.py

@ -1,4 +1,4 @@
"""Change base directory before importing songs."""
"""Add a path directory to the 'songdir' list."""
from patacrep.content import process_content, validate_parser_argument
from patacrep.songs import DataSubpath
@ -6,39 +6,35 @@ from patacrep.songs import DataSubpath
#pylint: disable=unused-argument
@validate_parser_argument("""
type: //rec
required:
path: //str
optional:
content: //any
required:
path: //str
""")
def parse(keyword, config, argument):
"""Return a list songs, whith a different base path.
"""Return a list of songs, whith an another base path.
Arguments:
- keyword: unused;
- config: the current songbook configuration dictionary;
- argument: a dict containing:
path: string specifying the path to use as root;
path: string specifying the path to add to the songdirs list;
content: songbook content, that is parsed by
patacrep.content.process_content().
This function adds 'path' to the directories where songs are searched
for, and then processes the content.
The 'path' is added:
- first as a relative path to the *.yaml file directory;
- then as a relative path to every path already present in
config['songdir'] (which are 'song' dir inside the datadirs).
The 'path' is added as a relative path to the dir of the songbook file.
"""
subpath = argument['path']
old_songdir = config['_songdir']
config['_songdir'] = [path.clone().join(subpath) for path in config['_songdir']]
if '_songbookfile_dir' in config:
config['_songdir'].insert(0, DataSubpath(config['_songbookfile_dir'], subpath))
config['_songdir'] = [DataSubpath(config['_songbookfile_dir'], subpath)]
config['_songdir'].extend(old_songdir)
processed_content = process_content(argument.get('content'), config)
config['_songdir'] = old_songdir
return processed_content
CONTENT_PLUGINS = {'cwd': parse}
CONTENT_PLUGINS = {'addsongdir': parse}

36
patacrep/content/cd.py

@ -0,0 +1,36 @@
"""Change base directory before importing songs."""
from patacrep.content import process_content, validate_parser_argument
#pylint: disable=unused-argument
@validate_parser_argument("""
type: //rec
required:
path: //str
optional:
content: //any
""")
def parse(keyword, config, argument):
"""Return a list of songs, whith a different base path.
Arguments:
- keyword: unused;
- config: the current songbook configuration dictionary;
- argument: a dict containing:
path: string specifying the path to append to current songdirs;
content: songbook content, that is parsed by
patacrep.content.process_content().
The 'path' is added as a relative path to every path already present
in config['songdir'] (which are 'songs' dir inside the datadirs).
"""
subpath = argument['path']
old_songdir = config['_songdir']
config['_songdir'] = [path.clone().join(subpath) for path in config['_songdir']]
processed_content = process_content(argument.get('content'), config)
config['_songdir'] = old_songdir
return processed_content
CONTENT_PLUGINS = {'cd': parse}

19
patacrep/content/include.py

@ -10,22 +10,23 @@ import logging
import yaml
from patacrep.content import process_content, ContentError, ContentList, validate_parser_argument
from patacrep import encoding, errors, files
from patacrep import encoding, errors
LOGGER = logging.getLogger(__name__)
def load_from_datadirs(path, datadirs):
"""Load 'path' from one of the datadirs.
def load_from_datadirs(filename, songdirs):
"""Load 'filename' from one of the songdirs.
Raise an exception if it was found if none of the datadirs of 'config'.
Raise an exception if it was not found in any songdir.
"""
for filepath in files.iter_datadirs(datadirs, "songs", path):
if os.path.exists(filepath):
return filepath
for path in songdirs:
fullpath = os.path.join(path.fullpath, filename)
if os.path.exists(fullpath):
return fullpath
# File not found
raise ContentError(
"include",
errors.notfound(path, list(files.iter_datadirs(datadirs)))
errors.notfound(filename, list(songdirs))
)
#pylint: disable=unused-argument
@ -53,7 +54,7 @@ def parse(keyword, config, argument):
for path in argument:
try:
filepath = load_from_datadirs(path, config['_datadir'])
filepath = load_from_datadirs(path, config['_songdir'])
except ContentError as error:
new_contentlist.append_error(error)
continue

1
patacrep/content/tex.py

@ -45,7 +45,6 @@ def parse(keyword, argument, config):
filelist = ContentList()
basefolders = itertools.chain(
(path.fullpath for path in config['_songdir']),
files.iter_datadirs(config['_datadir']),
files.iter_datadirs(config['_datadir'], 'latex'),
)
for filename in argument:

2
test/test_content/cwd.source

@ -1,3 +1,3 @@
- cwd:
- cd:
path: subdir
content:

2
test/test_content/cwd_list.source

@ -1,4 +1,4 @@
- cwd:
- cd:
path: subdir
content:
- "exsong.sg"

2
test/test_content/sort.source

@ -1,4 +1,4 @@
- cwd:
- addsongdir:
path: "datadir_sort"
content:
- section:

6
test/test_songbook/content.yaml

@ -12,14 +12,14 @@ content:
- sort:
- songsection: Test of song section
- setcounter: 101
- cwd:
- addsongdir:
# relative to yaml songfile
path: content_datadir/content
content:
- "song.csg"
- "song.tsg"
- cwd:
# relative to datadir
- cd:
# relative to datadir 'songs' dir
path: ../content
content:
- tex: foo.tex

4
test/test_songbook/onthefly/content.onthefly.yaml

@ -11,8 +11,8 @@ content:
- section: Test of section
- sort:
- songsection: Test of song section
- cwd:
# relative to datadir 'song' dir
- cd:
# relative to datadir 'songs' dir
path: ../content
content:
- tex: foo.tex

Loading…
Cancel
Save