Skip to content

Commit

Permalink
Added ability to pass in DFLTFILE as single argument. Bumped version.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffrimko committed Nov 4, 2017
1 parent 5fb7e60 commit 2d1f524
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
= PopPage Changelog

== poppage-0.6.4 (2017-11-04)
=== Added
- Can now pass in DFLTFILE as a single argument, command will be read from the file.

== poppage-0.6.3 (2017-11-01)
=== Fixed
- Fixes issue with incorrect implicit OUTPATH when one is not provided.
Expand Down
15 changes: 9 additions & 6 deletions app/poppage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""PopPage is a utility for generating output from templates.
Usage:
poppage INPATH
poppage make [options] [(--string KEY VAL) | (--file KEY PATH)]...
poppage check [options]
poppage run [options] [(--string KEY VAL) | (--file KEY PATH)]...
Expand Down Expand Up @@ -80,7 +81,7 @@
##==============================================================#

#: Application version string.
__version__ = "0.6.3"
__version__ = "0.6.4"

#: Key separator.
KEYSEP = "::"
Expand Down Expand Up @@ -209,6 +210,8 @@ def render_file(tmplpath, tmpldict):
def make(inpath, tmpldict, outpath=None, **kwargs):
"""Generates a file or directory based on the given input
template/dictionary."""
if not outpath:
outpath = os.getcwd()
if op.isfile(inpath):
return make_file(inpath, tmpldict, outpath=outpath)
else:
Expand Down Expand Up @@ -295,7 +298,7 @@ def check(inpath, echo=False, **kwargs):
def run(inpath, tmpldict, outpath=None, execute=None):
"""Handles logic for `run` command."""
if not outpath:
outpath = "__temp-poppage-" + _getrands(6)
outpath = op.join(os.getcwd(), "__temp-poppage-" + _getrands(6))
make(inpath, tmpldict, outpath=outpath)
qprompt.hrule()
if not execute:
Expand All @@ -321,13 +324,13 @@ def main():
exit_err("Must supply INPATH!")

# Handle command.
if args['check']:
if utildict['command'] == "check":
check(utildict['inpath'], echo=True)
elif args['make']:
elif utildict['command'] == "make":
make(utildict['inpath'], tmpldict, outpath=utildict['outpath'])
elif args['run']:
elif utildict['command'] == "run":
run(utildict['inpath'], tmpldict, outpath=utildict['outpath'], execute=utildict['execute'])
elif args['debug']:
elif utildict['command'] == "debug":
qprompt.echo("Utility Dictionary:")
pprint(utildict)
qprompt.echo("Template Dictionary:")
Expand Down
2 changes: 1 addition & 1 deletion app/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

setup(
name = "poppage",
version = "0.6.3",
version = "0.6.4",
author = "Jeff Rimko",
author_email = "[email protected]",
description = "Utility for generating files and directories from templates.",
Expand Down
15 changes: 11 additions & 4 deletions app/utilconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,15 @@ def get_cliopts(args):
val = args.get("--" + key)
if val:
opts[key] = val
cmd = [c for c in ['check','debug','make','run'] if args.get(c)]
if cmd:
opts['command'] = cmd[0]
return opts

def get_defopts(dfltdict):
opts = {}
if "__opt__" in dfltdict.keys():
for key in ['execute', 'outpath']:
for key in ['execute', 'outpath', 'command']:
opts[key] = (dfltdict.get('__opt__', {}) or {}).get(key)
for key in ['inpath']:
# Make paths absolute based on the location of the defaults file.
Expand All @@ -147,7 +150,8 @@ def get_tmpldict(args):
if dfltfile:
global _DFLTFILE
_DFLTFILE = op.abspath(dfltfile)
tmpldict = yaml.load(fsys.File(dfltfile).read())
data = fsys.File(dfltfile).read()
tmpldict = yaml.load(data)
tmpldict = update(tmpldict, {k:v for k,v in zip(args['--string'], args['VAL'])})
tmpldict = update(tmpldict, {k:fsys.File(v).read().strip() for k,v in zip(args['--file'], args['PATH'])})

Expand All @@ -174,11 +178,14 @@ def get_tmpldict(args):

def parse(args):
"""Parses the given arguments into a template dictionary."""
if args.get('INPATH'):
args['--defaults'] = args['INPATH']
if not op.isfile(args['--defaults']):
from poppage import exit_err
exit_err("Given path could not be found: `%s`" % (args['--defaults']))
tmpldict = get_tmpldict(args)
utildict = get_defopts(tmpldict)
utildict.update(get_cliopts(args))
if not utildict.get('outpath'):
utildict['outpath'] = os.getcwd()
return utildict, tmpldict

##==============================================================#
Expand Down
9 changes: 9 additions & 0 deletions tests/cli_test_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ def test_cli_14(test):
test.assertTrue(op.isfile("./foo/bar.txt"))
test.assertEqual(File("./foo/bar.txt").read(), "hello baz!")

def test_cli_15(test):
"""Check for basic make CLI functionality with defaults."""
with Cwd("__output__"):
retval = call("../defaults/d7.yaml", app_path="../../app")
test.assertEqual(0, retval)
test.assertTrue(op.isdir("./foo"))
test.assertTrue(op.isfile("./foo/bar.txt"))
test.assertEqual(File("./foo/bar.txt").read(), "hello baz!")

##==============================================================#
## SECTION: Main Body #
##==============================================================#
Expand Down
1 change: 1 addition & 0 deletions tests/defaults/d7.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__opt__:
command: make
inpath: ../templates/{{mydir}}
mydir: foo
myfile: bar
Expand Down

0 comments on commit 2d1f524

Please sign in to comment.