Skip to content

Commit

Permalink
implement jinja2 template rendering closes #62
Browse files Browse the repository at this point in the history
  • Loading branch information
mogproject committed Oct 5, 2015
1 parent 35585cb commit 3131f88
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/easy_menu/setting/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import yaml
import six
from six.moves.urllib.request import urlopen
from jinja2 import Environment

from easy_menu.setting import arg_parser
from easy_menu.exceptions import SettingError, ConfigError, EncodingError
Expand Down Expand Up @@ -147,7 +148,16 @@ def _load_data(self, is_command, path_or_url_or_cmdline):

# If --encode option is not set, we use utf-8 for parsing YAML file.
encoding = self.encoding or 'utf-8'
menu = yaml.load(data.decode(encoding))
data_str = data.decode(encoding)

# apply jinja2 template rendering
try:
data_str = Environment().from_string(data_str).render()
finally:
pass

# load as YAML string
menu = yaml.load(data_str)

# update cache data (Note: cache property is mutable!)
self.cache[(is_command, path_or_url_or_cmdline)] = menu
Expand Down
37 changes: 33 additions & 4 deletions tests/easy_menu/setting/test_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,35 @@ def test_load_data(self):
]},
{'Menu 6': ['echo 1', 'echo 2', 'false', 'echo 3']}]}
)
# template
with self.withAssertOutput('Reading file: tests/resources/with_template.yml\n', '') as (out, err):
self.assertEqual(Setting(stdout=out, stderr=err)._load_data(False, 'tests/resources/with_template.yml'),
{'Main Menu': [
{'Menu 1': 'echo 1'},
{'Menu 2': 'echo 2'},
{'Menu 3': 'echo 3'},
]})
with self.withAssertOutput('Reading file: tests/resources/with_template_utf8_ja.yml\n', '') as (out, err):
self.assertEqual(
Setting(stdout=out, stderr=err)._load_data(False, 'tests/resources/with_template_utf8_ja.yml'),
{
"メインメニュー": [
{"サービス稼動状態確認": "echo 'サービス稼動状態確認'"},
{"サーバリソース状況確認": "echo 'サーバリソース状況確認'"},
{"業務状態制御メニュー": [
{"業務状態確認": "echo '業務状態確認'"},
{"業務開始": "echo '業務開始'"},
{"業務終了": "echo '業務終了'"}
]},
{"Webサービス制御メニュー": [
{"Webサービス状態確認": "echo 'Webサービス状態確認'"},
{"Webサービス起動": "echo 'Webサービス起動'"},
{"Webサービス停止": "echo 'Webサービス停止'"}
]},
{"サーバ再起動": "echo 'サーバ再起動'"}
]
}
)

@mock.patch('easy_menu.setting.setting.urlopen')
def test_load_data_http(self, urlopen_mock):
Expand All @@ -260,6 +289,9 @@ def test_load_data_error(self):
('error_not_exist.yml', 'Failed to open.'),
('error_parser.yml', 'YAML format error: expected \'<document start>\', but found \'<scalar>\'\n'
' in "<unicode string>", line 1, column 3:\n \'\':1\n ^'),
('error_scanner.yml',
'YAML format error: while scanning a quoted scalar\n in "<unicode string>", line 1, column 1:\n "'
'\n ^\nfound unexpected end of stream\n in "<unicode string>", line 1, column 2:\n "\n ^'),
('error_parser_utf8_ja.yml', 'YAML format error: while parsing a flow mapping\n'
' in "<unicode string>", line 1, column 1:\n'
' {\n'
Expand All @@ -268,9 +300,6 @@ def test_load_data_error(self):
' in "<unicode string>", line 3, column 7:\n'
""" {"サービス稼動状態確認": "echo 'サービス稼動状態確認'"},\n"""
' ^'),
('error_scanner.yml',
'YAML format error: while scanning a quoted scalar\n in "<unicode string>", line 1, column 1:\n "'
'\n ^\nfound unexpected end of stream\n in "<unicode string>", line 2, column 1:\n \n ^')
]

# we don't use assertRaisesRegexp because the message contains utf-8 characters
Expand All @@ -281,7 +310,7 @@ def test_load_data_error(self):
Setting(config_path=path, stdout=out, stderr=err).load_config()
self.assertEqual(str(cm.exception) if six.PY3 else cm.exception.message, '%s: %s' % (path, expect))

def test_load_data_sjis(self):
def test_load_data_error_sjis(self):
self.maxDiff = None

self.assertRaisesRegexp(
Expand Down
7 changes: 7 additions & 0 deletions tests/resources/with_template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% set cmd = 'echo' %}
---
Main Menu:
{% for i in [1, 2, 3] %}
- Menu {{ i }}: {{ cmd }} {{ i }}
{% endfor %}

18 changes: 18 additions & 0 deletions tests/resources/with_template_utf8_ja.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% set cmd = 'echo' %}
{
"メインメニュー": [
{"サービス稼動状態確認": "{{ cmd }} 'サービス稼動状態確認'"},
{"サーバリソース状況確認": "echo 'サーバリソース状況確認'"},
{"業務状態制御メニュー": [
{"業務状態確認": "echo '業務状態確認'"},
{"業務開始": "echo '業務開始'"},
{"業務終了": "echo '業務終了'"}
]},
{"Webサービス制御メニュー": [
{"Webサービス状態確認": "echo 'Webサービス状態確認'"},
{"Webサービス起動": "echo 'Webサービス起動'"},
{"Webサービス停止": "echo 'Webサービス停止'"}
]},
{"サーバ再起動": "echo 'サーバ再起動'"}
]
}

0 comments on commit 3131f88

Please sign in to comment.