From 182a85fbcdee219a0625da1142e52359e90892c9 Mon Sep 17 00:00:00 2001 From: noah morrison Date: Tue, 18 Nov 2014 23:42:41 -0500 Subject: [PATCH] Updated main function to not need data file If no data file is present it defaults to an empty dictionary --- chevron/chevron.py | 27 +++++++++++++++------------ test_spec.py | 2 +- 2 files changed, 16 insertions(+), 13 deletions(-) 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: -- 2.47.3