diff --git a/chevron/main.py b/chevron/main.py index 35934ee..d941ff4 100755 --- a/chevron/main.py +++ b/chevron/main.py @@ -3,11 +3,6 @@ import io import sys -try: - import yaml as json -except ImportError: # not tested - import json - try: from .renderer import render from .metadata import version @@ -18,9 +13,12 @@ def main(template, data={}, **kwargs): with io.open(template, 'r', encoding='utf-8') as template_file: + + yaml_loader = kwargs.pop('yaml_loader', None) or 'FullLoader' + if data != {}: data_file = io.open(data, 'r', encoding='utf-8') - data = json.load(data_file) + data = _load_data(data_file, yaml_loader) data_file.close() args = { @@ -32,6 +30,16 @@ def main(template, data={}, **kwargs): return render(**args) +def _load_data(file, yaml_loader): + try: + import yaml + loader = getattr(yaml, yaml_loader) # not tested + return yaml.load(file, Loader=loader) # not tested + except ImportError: + import json + return json.load(file) + + def cli_main(): """Render mustache templates using json files""" import argparse @@ -61,6 +69,9 @@ def is_dir(arg): help='The json data file', type=is_file_or_pipe, default={}) + parser.add_argument('-y', '--yaml-loader', dest='yaml_loader', + help=argparse.SUPPRESS) + parser.add_argument('-p', '--path', dest='partials_path', help='The directory where your partials reside', type=is_dir, default='.')