Skip to content

Commit

Permalink
Merge 476cd2d into 78f1a38
Browse files Browse the repository at this point in the history
  • Loading branch information
binokkio committed Dec 27, 2020
2 parents 78f1a38 + 476cd2d commit 1a0d2e8
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions chevron/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand All @@ -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
Expand Down Expand Up @@ -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='.')
Expand Down

0 comments on commit 1a0d2e8

Please sign in to comment.