Skip to content

Commit

Permalink
Add envfile input format
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrobenolt committed Jul 11, 2017
1 parent 1d0d242 commit 07a919e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions jinja2cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class MalformedXML(InvalidDataFormat):
pass


class MalformedEnv(InvalidDataFormat):
pass


def get_format(fmt):
try:
return formats[fmt]()
Expand Down Expand Up @@ -151,6 +155,23 @@ def _load_xml():
return xmltodict.parse, xml.parsers.expat.ExpatError, MalformedXML


def _load_env():
def _parse_env(data):
"""
Parse an envfile format of key=value pairs that are newline separated
"""
dict_ = {}
for line in data.splitlines():
line = line.lstrip()
# ignore empty or commented lines
if not line or line[:1] == '#':
continue
k, v = line.split('=', 1)
dict_[k] = v
return dict_
return _parse_env, Exception, MalformedEnv


# Global list of available format parsers on your system
# mapped to the callable/Exception to parse a string into a dict
formats = {
Expand All @@ -161,6 +182,7 @@ def _load_xml():
'querystring': _load_querystring,
'toml': _load_toml,
'xml': _load_xml,
'env': _load_env,
}


Expand Down

0 comments on commit 07a919e

Please sign in to comment.