def convert_all_json_to_yaml(self, dirpath): for path in os.listdir(dirpath): if not path.endswith('.template') and not path.endswith('.json'): continue with open(os.path.join(dirpath, path), 'r') as f: json_str = f.read() yml_str = template_format.convert_json_to_yaml(json_str) yield (json_str, yml_str)
def convert_all_json_to_yaml(self, dirpath): for path in os.listdir(dirpath): if not path.endswith(".template") and not path.endswith(".json"): continue f = open(os.path.join(dirpath, path), "r") json_str = f.read() yml_str = template_format.convert_json_to_yaml(json_str) yield (json_str, yml_str, f.name)
def test_integer_only_keys_get_translated_correctly(self): path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates/WordPress_Single_Instance.template') with open(path, 'r') as f: json_str = f.read() yml_str = template_format.convert_json_to_yaml(json_str) match = re.search('[\s,{]\d+\s*:', yml_str) # Check that there are no matches of integer-only keys # lacking explicit quotes self.assertIsNone(match)
def test_integer_only_keys_get_translated_correctly(self): path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates/WordPress_Single_Instance.template') with open(path, 'r') as f: json_str = f.read() yml_str = template_format.convert_json_to_yaml(json_str) match = re.search(r'[\s,{]\d+\s*:', yml_str) # Check that there are no matches of integer-only keys # lacking explicit quotes self.assertIsNone(match)
def convert_directory(dirpath): for path in os.listdir(dirpath): if not path.endswith('.template') and not path.endswith('.json'): continue yamlpath = re.sub('\..*$', '.yaml', path) print('Writing to %s' % yamlpath) f = open(os.path.join(dirpath, path), 'r') out = open(os.path.join(dirpath, yamlpath), 'w') yml = template_format.convert_json_to_yaml(f.read()) out.write(yml) out.close()
#!/usr/bin/env python from heat.common.template_format import convert_json_to_yaml json = """ { "AWSTemplateFormatVersion": "2010-09-09", "Description": "MonboDB Config Server", "Mappings": { "AWSInstanceType2Arch" : { "m1.tiny" : { "Arch" : "32" }, "m1.small" : { "Arch" : "64" }, "m1.medium" : { "Arch" : "64" }, "m1.large" : { "Arch" : "64" }, "m1.xlarge" : { "Arch" : "64" } }, "DistroArch2AMI": { "F18" : { "32" : "F18-i386-cfntools", "64" : "F18-x86_64-cfntools" }, "F17" : { "32" : "F17-i386-cfntools", "64" : "F17-x86_64-cfntools" }, "U10" : { "32" : "U10-i386-cfntools", "64" : "U10-x86_64-cfntools" }, "RHEL-6.1" : { "32" : "rhel61-i386-cfntools", "64" : "rhel61-x86_64-cfntools" }, "RHEL-6.2" : { "32" : "rhel62-i386-cfntools", "64" : "rhel62-x86_64-cfntools" }, "RHEL-6.3" : { "32" : "rhel63-i386-cfntools", "64" : "rhel63-x86_64-cfntools" } } } } """ yaml = convert_json_to_yaml(json) print yaml
def get_yaml(self): yaml = convert_json_to_yaml(json.dumps(self.get_json())) yaml = yaml.replace("""HeatTemplateFormatVersion: '2012-12-12'""", """heat_template_version: '2013-05-23'""") return yaml
def convert_file(path): f = open(path, 'r') print(template_format.convert_json_to_yaml(f.read()))