From c67cc4ac1cb62ea4bf60f4ada40013360d8c1fd9 Mon Sep 17 00:00:00 2001 From: Jeff Rimko Date: Mon, 23 Oct 2017 23:21:54 -0400 Subject: [PATCH] Added new `!def` tag to handle issue with including YAML as defaults and proper path handling. --- app/poppage.py | 20 ++++++++++++++++++-- tests/cli_test_1.py | 6 ++++++ tests/defaults-d1.yaml | 3 +++ tests/defaults/d5.yaml | 2 +- tests/{defaults => includes}/include-d1.yaml | 0 5 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 tests/defaults-d1.yaml rename tests/{defaults => includes}/include-d1.yaml (100%) diff --git a/app/poppage.py b/app/poppage.py index 59595f6..bc98f61 100644 --- a/app/poppage.py +++ b/app/poppage.py @@ -313,6 +313,18 @@ def __new__(cls, fpath): return str.__new__(cls, fi.read().strip()) def __repr__(self): return self + class DefaultLoader(object): + def __new__(cls, fpath): + if not op.isabs(fpath): + global _DFLTFILE + fpath = op.normpath(op.join(op.dirname(_DFLTFILE), fpath)) + with io.open(fpath) as fi: + defs = yaml.load(fi.read()) + if "inpath" in defs.keys(): + defs['inpath'] = op.abspath(op.normpath(op.join(op.dirname(fpath), defs['inpath']))) + return defs + def __repr__(self): + return str(self) class IncludeLoader(object): def __new__(cls, fpath): if not op.isabs(fpath): @@ -336,6 +348,9 @@ def file_reader_ctor(loader, node): def include_loader_ctor(loader, node): value = loader.construct_scalar(node) return IncludeLoader(value) + def default_loader_ctor(loader, node): + value = loader.construct_scalar(node) + return DefaultLoader(value) def prompter_ctor(loader, node): value = loader.construct_scalar(node) return Prompter(value) @@ -352,6 +367,7 @@ def update(d, u): yaml.add_constructor(u'!file', file_reader_ctor) yaml.add_constructor(u'!cmd', cmd_ctor) yaml.add_constructor(u'!yaml', include_loader_ctor) + yaml.add_constructor(u'!def', default_loader_ctor) yaml.add_constructor(u'!ask', prompter_ctor) # Prepare template dictionary. @@ -436,9 +452,9 @@ def main(): elif args['run']: run(utildict['inpath'], tmpldict, outpath=utildict['outpath'], execute=utildict['execute']) elif args['debug']: - print("Utility Dictionary:") + qprompt.echo("Utility Dictionary:") pprint(utildict) - print("Template Dictionary:") + qprompt.echo("Template Dictionary:") pprint(tmpldict) ##==============================================================# diff --git a/tests/cli_test_1.py b/tests/cli_test_1.py index 5822a5d..b79445b 100644 --- a/tests/cli_test_1.py +++ b/tests/cli_test_1.py @@ -70,6 +70,12 @@ def test_cli_9(test): test.assertEqual(0, retval) test.assertTrue(op.isfile("./__output__/out.py")) + def test_cli_10(test): + """Check for basic make CLI functionality with defaults.""" + retval = call("make --defaults defaults-d1.yaml") + test.assertEqual(0, retval) + test.assertTrue(op.isfile("./__output__/out.py")) + ##==============================================================# ## SECTION: Main Body # ##==============================================================# diff --git a/tests/defaults-d1.yaml b/tests/defaults-d1.yaml new file mode 100644 index 0000000..ad46ce0 --- /dev/null +++ b/tests/defaults-d1.yaml @@ -0,0 +1,3 @@ +__def__: !def includes/include-d1.yaml +filename: foo.txt +text: bar diff --git a/tests/defaults/d5.yaml b/tests/defaults/d5.yaml index b0ff76f..1f32a62 100644 --- a/tests/defaults/d5.yaml +++ b/tests/defaults/d5.yaml @@ -1,3 +1,3 @@ -__def__: !yaml include-d1.yaml +__def__: !def ../includes/include-d1.yaml filename: foo.txt text: !file ../text/t2.txt diff --git a/tests/defaults/include-d1.yaml b/tests/includes/include-d1.yaml similarity index 100% rename from tests/defaults/include-d1.yaml rename to tests/includes/include-d1.yaml