Skip to content

Commit

Permalink
Added new !def tag to handle issue with including YAML as defaults …
Browse files Browse the repository at this point in the history
…and proper path handling.
  • Loading branch information
jeffrimko committed Oct 24, 2017
1 parent 8452506 commit c67cc4a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
20 changes: 18 additions & 2 deletions app/poppage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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)

##==============================================================#
Expand Down
6 changes: 6 additions & 0 deletions tests/cli_test_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
##==============================================================#
Expand Down
3 changes: 3 additions & 0 deletions tests/defaults-d1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__def__: !def includes/include-d1.yaml
filename: foo.txt
text: bar
2 changes: 1 addition & 1 deletion tests/defaults/d5.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__def__: !yaml include-d1.yaml
__def__: !def ../includes/include-d1.yaml
filename: foo.txt
text: !file ../text/t2.txt
File renamed without changes.

0 comments on commit c67cc4a

Please sign in to comment.