diff --git a/patacrep/content/__init__.py b/patacrep/content/__init__.py index 1604204b..a65b0525 100755 --- a/patacrep/content/__init__.py +++ b/patacrep/content/__init__.py @@ -21,7 +21,7 @@ met, the corresponding parser is called. - sort - section* - - cwd + - cd # Parsers diff --git a/patacrep/content/cwd.py b/patacrep/content/addsongdir.py similarity index 57% rename from patacrep/content/cwd.py rename to patacrep/content/addsongdir.py index 7efee676..6554ccb0 100755 --- a/patacrep/content/cwd.py +++ b/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} diff --git a/patacrep/content/cd.py b/patacrep/content/cd.py new file mode 100755 index 00000000..5125a448 --- /dev/null +++ b/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} diff --git a/patacrep/content/include.py b/patacrep/content/include.py index 0755c747..5dca3940 100644 --- a/patacrep/content/include.py +++ b/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 diff --git a/patacrep/content/tex.py b/patacrep/content/tex.py index 406084a8..adf6950e 100755 --- a/patacrep/content/tex.py +++ b/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: diff --git a/test/test_content/cwd.source b/test/test_content/cwd.source index 1d0663d7..b1cc6e36 100644 --- a/test/test_content/cwd.source +++ b/test/test_content/cwd.source @@ -1,3 +1,3 @@ -- cwd: +- cd: path: subdir content: \ No newline at end of file diff --git a/test/test_content/cwd_list.source b/test/test_content/cwd_list.source index 15f85307..e95ae6ab 100644 --- a/test/test_content/cwd_list.source +++ b/test/test_content/cwd_list.source @@ -1,4 +1,4 @@ -- cwd: +- cd: path: subdir content: - "exsong.sg" diff --git a/test/test_content/sort.source b/test/test_content/sort.source index 0e6d71da..4e523a7b 100644 --- a/test/test_content/sort.source +++ b/test/test_content/sort.source @@ -1,4 +1,4 @@ -- cwd: +- addsongdir: path: "datadir_sort" content: - section: diff --git a/test/test_songbook/content.yaml b/test/test_songbook/content.yaml index 2585a91f..432f10a0 100644 --- a/test/test_songbook/content.yaml +++ b/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 diff --git a/test/test_songbook/onthefly/content.onthefly.yaml b/test/test_songbook/onthefly/content.onthefly.yaml index 3d5b585f..bb7ae271 100644 --- a/test/test_songbook/onthefly/content.onthefly.yaml +++ b/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