diff options
-rw-r--r-- | .hgignore | 1 | ||||
-rw-r--r-- | mrbelvedere/template_parser.py | 22 | ||||
-rw-r--r-- | tests/test_template_parser.py | 36 |
3 files changed, 59 insertions, 0 deletions
@@ -1,5 +1,6 @@ | |||
1 | \.pyc$ | 1 | \.pyc$ |
2 | \.coverage$ | 2 | \.coverage$ |
3 | \.swp$ | ||
3 | ^build$ | 4 | ^build$ |
4 | ^dist$ | 5 | ^dist$ |
5 | ^build$ | 6 | ^build$ |
diff --git a/mrbelvedere/template_parser.py b/mrbelvedere/template_parser.py new file mode 100644 index 0000000..6217d30 --- /dev/null +++ b/mrbelvedere/template_parser.py | |||
@@ -0,0 +1,22 @@ | |||
1 | """ Parse build templates | ||
2 | """ | ||
3 | import yaml | ||
4 | |||
5 | |||
6 | class TemplateParser(object): | ||
7 | |||
8 | def __init__(self): | ||
9 | self.yaml = yaml | ||
10 | self._current_template_file = None | ||
11 | self.template_data = None | ||
12 | |||
13 | @property | ||
14 | def template_file(self): | ||
15 | return self._current_template_file | ||
16 | |||
17 | @template_file.setter | ||
18 | def template_file(self, file): | ||
19 | self._current_template_file = open(file) | ||
20 | |||
21 | def load_template(self): | ||
22 | self.template_data = self.yaml.load(self.template_file.read()).get('template') | ||
diff --git a/tests/test_template_parser.py b/tests/test_template_parser.py new file mode 100644 index 0000000..5cb6e2b --- /dev/null +++ b/tests/test_template_parser.py | |||
@@ -0,0 +1,36 @@ | |||
1 | #!/usr/bin/env python | ||
2 | from dingus import Dingus, DingusTestCase, DontCare | ||
3 | import nose.tools as nose_tools | ||
4 | from mrbelvedere import template_parser | ||
5 | from mrbelvedere.template_parser import TemplateParser | ||
6 | import yaml | ||
7 | |||
8 | class WhenSettingUp(DingusTestCase(TemplateParser, exclude=['yaml'])): | ||
9 | |||
10 | def setup(self): | ||
11 | super(WhenSettingUp, self).setup() | ||
12 | self.template_parser = TemplateParser() | ||
13 | |||
14 | def should_setup_yaml(self): | ||
15 | assert isinstance(self.template_parser.yaml, type(yaml)) | ||
16 | |||
17 | def should_degine_template_data_attribute(self): | ||
18 | assert self.template_parser.template_data is None | ||
19 | |||
20 | def should_define_file_attribute(self): | ||
21 | assert self.template_parser._current_template_file is None | ||
22 | |||
23 | def should_set_file_to_parse(self): | ||
24 | self.template_parser.template_file = 'examples/webserver.yaml' | ||
25 | assert self.template_parser.template_file is not None | ||
26 | |||
27 | def should_load_template(self): | ||
28 | self.template_parser.template_file = 'examples/webserver.yaml' | ||
29 | self.template_parser.load_template() | ||
30 | assert self.template_parser.template_data is not None | ||
31 | |||
32 | def should_raise_io_error(self): | ||
33 | try: | ||
34 | self.template_parser.template_file = 'examples/invalid_template.yaml' | ||
35 | except IOError: | ||
36 | assert True | ||