Skip to content

Commit 2e7d1c1

Browse files
author
Clark Perkins
committed
Support yaml blueprint templates
1 parent 0dcb4cd commit 2e7d1c1

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

stackdio/cli/blueprints/generator.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
from __future__ import print_function
1+
from __future__ import print_function, unicode_literals
22

3-
import sys
4-
import os
53
import json
4+
import os
5+
import sys
66

77
import click
88
import yaml
99
from jinja2 import Environment, FileSystemLoader, StrictUndefined, meta
1010
from jinja2.exceptions import TemplateNotFound, TemplateSyntaxError, UndefinedError
11-
from jinja2.nodes import Assign, Block, Const, If, Not
1211
from jinja2.filters import do_replace, evalcontextfilter
12+
from jinja2.nodes import Assign, Block, Const, If, Not
1313

1414

1515
class BlueprintException(Exception):
@@ -232,12 +232,12 @@ def generate(self, template_file, var_files=(), variables=None,
232232
if prompt:
233233
# Prompt for missing vars
234234
for var in sorted(missing_vars):
235-
context[var] = self.prompt('{0}: '.format(var))
235+
context[var] = self.prompt('{}: '.format(var))
236236
else:
237237
# Print an error
238238
error_str = 'Missing variables:\n'
239239
for var in sorted(missing_vars):
240-
error_str += ' {0}\n'.format(var)
240+
error_str += ' {}\n'.format(var)
241241
self.error_exit(error_str, 0)
242242

243243
# Find the set of optional variables (ones inside of a 'if <var> is not undefined'
@@ -259,14 +259,14 @@ def generate(self, template_file, var_files=(), variables=None,
259259
if null_vars and not suppress_warnings:
260260
warn_str = '\nWARNING: Null variables (replaced with empty string):\n'
261261
for var in null_vars:
262-
warn_str += ' {0}\n'.format(var)
262+
warn_str += ' {}\n'.format(var)
263263
self.warning(warn_str, 0)
264264

265265
# Print a warning if there's unset optional variables
266266
if optional_vars and not suppress_warnings:
267267
warn_str = '\nWARNING: Missing optional variables:\n'
268268
for var in sorted(optional_vars):
269-
warn_str += ' {0}\n'.format(var)
269+
warn_str += ' {}\n'.format(var)
270270
self.warning(warn_str, 0)
271271

272272
# Generate the blueprint
@@ -276,24 +276,31 @@ def generate(self, template_file, var_files=(), variables=None,
276276
set_vars.update(context)
277277
context = set_vars
278278

279-
template_json = template.render(**context)
279+
rendered_template = template.render(**context)
280280

281281
if debug:
282282
click.echo('\n')
283-
click.echo(template_json)
283+
click.echo(rendered_template)
284284
click.echo('\n')
285285

286-
# Return a dict object rather than a string
287-
return json.loads(template_json)
286+
template_extension = template_file.split('.')[-1]
287+
288+
if template_extension in ('json',):
289+
# Return a dict object rather than a string
290+
return json.loads(rendered_template)
291+
elif template_extension in ('yaml', 'yml'):
292+
return yaml.safe_load(rendered_template)
293+
else:
294+
self.error_exit('Template extension {} is not valid.'.format(template_extension))
288295

289296
except TemplateNotFound:
290-
self.error_exit('Your template file {0} was not found.'.format(template_file))
297+
self.error_exit('Your template file {} was not found.'.format(template_file))
291298
except TemplateSyntaxError as e:
292-
self.error_exit('Invalid template error at line {0}:\n{1}'.format(
299+
self.error_exit('Invalid template error at line {}:\n{}'.format(
293300
e.lineno,
294301
str(e)
295302
))
296303
except UndefinedError as e:
297-
self.error_exit('Missing variable: {0}'.format(str(e)))
304+
self.error_exit('Missing variable: {}'.format(str(e)))
298305
# except ValueError:
299306
# self.error_exit('Invalid JSON. Check your template file.')

0 commit comments

Comments
 (0)