diff --git a/patacrep/content/cwd.py b/patacrep/content/cwd.py index 5a05bb16..efc37553 100755 --- a/patacrep/content/cwd.py +++ b/patacrep/content/cwd.py @@ -1,5 +1,7 @@ """Change base directory before importing songs.""" +import os + from patacrep.content import process_content, validate_parser_argument from patacrep.songs import DataSubpath @@ -26,14 +28,16 @@ def parse(keyword, config, argument): for, and then processes the content. The 'path' is added: - - first as a relative path to the current directory; + - first as a relative path to the *.sb file directory; - then as a relative path to every path already present in config['songdir']. """ subpath = argument['path'] old_songdir = config['_songdir'] + sbdir = os.path.dirname(config['_filepath']) + config['_songdir'] = ( - [DataSubpath(".", subpath)] + + [DataSubpath(sbdir, subpath)] + [path.clone().join(subpath) for path in config['_songdir']] ) processed_content = process_content(argument.get('content'), config) diff --git a/patacrep/data/templates/songbook_model.yml b/patacrep/data/templates/songbook_model.yml index 7567de47..425935e6 100644 --- a/patacrep/data/templates/songbook_model.yml +++ b/patacrep/data/templates/songbook_model.yml @@ -5,6 +5,7 @@ schema: template: //any required: _cache: //bool + _filepath: //str _datadir: type: //arr contents: //str diff --git a/patacrep/songbook/__main__.py b/patacrep/songbook/__main__.py index adc581c5..3e9e592b 100644 --- a/patacrep/songbook/__main__.py +++ b/patacrep/songbook/__main__.py @@ -153,6 +153,9 @@ def main(): default_songbook.update(user_songbook) songbook = dict(default_songbook) + songbook['_filepath'] = os.path.abspath(songbook_path) + sbdir = os.path.dirname(songbook['_filepath']) + # Gathering datadirs datadirs = [] if options.datadir: @@ -162,16 +165,13 @@ def main(): if isinstance(songbook['book']['datadir'], str): songbook['book']['datadir'] = [songbook['book']['datadir']] datadirs += [ - os.path.join( - os.path.dirname(os.path.abspath(songbook_path)), - path - ) + os.path.join(sbdir, path) for path in songbook['book']['datadir'] ] del songbook['book']['datadir'] # Default value - datadirs.append(os.path.dirname(os.path.abspath(songbook_path))) + datadirs.append(sbdir) songbook['_datadir'] = datadirs songbook['_cache'] = options.cache[0] diff --git a/test/test_content/sorted.source b/test/test_content/sorted.source index f9806058..ef51ebb6 100644 --- a/test/test_content/sorted.source +++ b/test/test_content/sorted.source @@ -1,4 +1,7 @@ - sorted: key: "@title" - sorted: - key: [by, "@title"] \ No newline at end of file + key: [by, "@title"] + content: + cwd: + path: "./" diff --git a/test/test_content/test_content.py b/test/test_content/test_content.py index 1c749198..590dde91 100644 --- a/test/test_content/test_content.py +++ b/test/test_content/test_content.py @@ -53,8 +53,10 @@ 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['_filepath'] = base with logging_reduced('patacrep.content.song'): - expandedlist = content.process_content(sbcontent, cls.config.copy()) + expandedlist = content.process_content(sbcontent, config) sourcelist = [cls._clean_path(elem) for elem in expandedlist] controlname = "{}.control".format(base) diff --git a/test/test_songbook/content.sb b/test/test_songbook/content.sb index 2d37d9d0..56f1a75f 100644 --- a/test/test_songbook/content.sb +++ b/test/test_songbook/content.sb @@ -11,9 +11,14 @@ content: - sorted: - songsection: Test of song section - cwd: + # relative to sg file path: content_datadir/content content: - "song.csg" - "song.tsg" + - cwd: + # relative to datadir + path: ../content + content: - tex: foo.tex - include: include.sbc \ No newline at end of file