| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import json |
| 14 |
import yaml |
| 15 |
|
| 16 |
from heatclient._i18n import _ |
| 17 |
|
| 18 |
if hasattr(yaml, 'CSafeLoader'): |
| 19 |
yaml_loader_base = yaml.CSafeLoader |
| 20 |
else: |
| 21 |
yaml_loader_base = yaml.SafeLoader |
| 22 |
|
| 23 |
if hasattr(yaml, 'CSafeDumper'): |
| 24 |
yaml_dumper_base = yaml.CSafeDumper |
| 25 |
else: |
| 26 |
yaml_dumper_base = yaml.SafeDumper |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class yaml_loader(yaml_loader_base): |
| 31 |
pass |
| 32 |
|
| 33 |
|
| 34 |
class yaml_dumper(yaml_dumper_base): |
| 35 |
pass |
| 36 |
|
| 37 |
|
| 38 |
def _construct_yaml_str(self, node): |
| 39 |
|
| 40 |
|
| 41 |
return self.construct_scalar(node) |
| 42 |
|
| 43 |
|
| 44 |
yaml_loader.add_constructor('tag:yaml.org,2002:str', _construct_yaml_str) |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
yaml_loader.add_constructor('tag:yaml.org,2002:timestamp', |
| 50 |
_construct_yaml_str) |
| 51 |
|
| 52 |
|
| 53 |
def parse(tmpl_str): |
| 54 |
"""Takes a string and returns a dict containing the parsed structure. |
| 55 |
|
| 56 |
This includes determination of whether the string is using the |
| 57 |
JSON or YAML format. |
| 58 |
""" |
| 59 |
|
| 60 |
tmpl_str = tmpl_str.strip() |
| 61 |
if tmpl_str.startswith('{'): |
| 62 |
tpl = json.loads(tmpl_str) |
| 63 |
else: |
| 64 |
try: |
| 65 |
tpl = yaml.load(tmpl_str, Loader=yaml_loader) |
| 66 |
except yaml.YAMLError: |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
try: |
| 71 |
tpl = yaml.load(tmpl_str, Loader=yaml.SafeLoader) |
| 72 |
except yaml.YAMLError as yea: |
| 73 |
raise ValueError(yea) |
| 74 |
else: |
| 75 |
if tpl is None: |
| 76 |
tpl = {} |
| 77 |
|
| 78 |
if not ('HeatTemplateFormatVersion' in tpl |
| 79 |
or 'heat_template_version' in tpl |
| 80 |
or 'AWSTemplateFormatVersion' in tpl): |
| 81 |
raise ValueError(_("Template format version not found.")) |
| 82 |
return tpl |