From: noah morrison Date: Wed, 19 Nov 2014 04:42:41 +0000 (-0500) Subject: Updated main function to not need data file X-Git-Url: https://git.devinivas.org/?a=commitdiff_plain;h=182a85fbcdee219a0625da1142e52359e90892c9;p=chevron.git Updated main function to not need data file If no data file is present it defaults to an empty dictionary --- diff --git a/chevron/chevron.py b/chevron/chevron.py index 921d8ee..23efd00 100755 --- a/chevron/chevron.py +++ b/chevron/chevron.py @@ -379,19 +379,22 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache', return output.encode('utf-8') -def main(data, template, **kwargs): - data = data - template = template - +def main(template, data=None, **kwargs): with open(template, 'r') as template_file: - with open(data, 'r') as data_file: - args = { - 'template': template_file, - 'data': json.load(data_file) - } - - args.update(kwargs) - return render(**args) + if data is not None: + data_file = open(data, 'r') + data = json.load(data_file) + data_file.close() + else: + data = {} + + args = { + 'template': template_file, + 'data': data + } + + args.update(kwargs) + return render(**args) def cli_main(): diff --git a/test_spec.py b/test_spec.py index e5c8810..eb780e2 100755 --- a/test_spec.py +++ b/test_spec.py @@ -107,7 +107,7 @@ class ExpandedCoverage(unittest.TestCase): self.assertEqual(result, expected) def test_main(self): - result = chevron.main('tests/data.json', 'tests/test.mustache', + result = chevron.main('tests/test.mustache', 'tests/data.json', partials_path='tests') with open('tests/test.rendered', 'r') as f: