modify to import JSON, and not YAML
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from jinja2 import Environment, FileSystemLoader
|
from jinja2 import Environment, FileSystemLoader
|
||||||
import pprint
|
import pprint
|
||||||
import os
|
import os
|
||||||
|
import json
|
||||||
|
|
||||||
from yaml import safe_load
|
from yaml import safe_load
|
||||||
try:
|
try:
|
||||||
@@ -8,10 +9,22 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
from yaml import Loader
|
from yaml import Loader
|
||||||
|
|
||||||
def main(template_filepath,yaml_filepath,output_filepath):
|
def main(template_filepath,input_filepath,output_filepath):
|
||||||
|
|
||||||
|
with open(input_filepath, 'r') as file:
|
||||||
|
input_devices = file.read()
|
||||||
|
|
||||||
|
device_info = dict()
|
||||||
|
|
||||||
|
if os.path.splitext(input_filepath)[1] == ".json":
|
||||||
|
device_info = json.loads(input_devices)
|
||||||
|
# elif os.path.splitext(input_filepath) == ".yaml":
|
||||||
|
# devices = safe_load(input_devices)
|
||||||
|
else:
|
||||||
|
print(os.path.splitext(input_filepath))
|
||||||
|
print('{} is not a valid JSON format!\n'.format(input_filepath))
|
||||||
|
return 406
|
||||||
|
|
||||||
with open(yaml_filepath, 'r') as file:
|
|
||||||
devices = safe_load(file)
|
|
||||||
|
|
||||||
template_path = os.path.dirname(os.path.abspath(template_filepath))
|
template_path = os.path.dirname(os.path.abspath(template_filepath))
|
||||||
template_filename = os.path.basename(template_filepath)
|
template_filename = os.path.basename(template_filepath)
|
||||||
@@ -28,21 +41,24 @@ def main(template_filepath,yaml_filepath,output_filepath):
|
|||||||
#output = open(filename, mode="w", encoding="utf-8")
|
#output = open(filename, mode="w", encoding="utf-8")
|
||||||
output = open(output_filepath, mode="w", encoding="utf-8")
|
output = open(output_filepath, mode="w", encoding="utf-8")
|
||||||
|
|
||||||
|
content = template.render(device_info)
|
||||||
|
|
||||||
for host, keys in devices.items():
|
|
||||||
device = "{"
|
|
||||||
for key, value in keys.items():
|
|
||||||
device += "'{}':'{}', ".format(key,value)
|
|
||||||
device += "'Hostname':'{}' }}".format(host)
|
|
||||||
#pprint.pprint(device)
|
|
||||||
|
|
||||||
device_dict = eval(device)
|
|
||||||
#pprint.pprint(device_dict)
|
|
||||||
content = template.render( device_dict )
|
|
||||||
|
|
||||||
print('{}\n'.format(content))
|
|
||||||
output.write('{}\n'.format(content))
|
output.write('{}\n'.format(content))
|
||||||
|
|
||||||
|
# for host, keys in devices.items():
|
||||||
|
# device = "{"
|
||||||
|
# for key, value in keys.items():
|
||||||
|
# device += "'{}':'{}', ".format(key,value)
|
||||||
|
# device += "'Hostname':'{}' }}".format(host)
|
||||||
|
# #pprint.pprint(device)
|
||||||
|
|
||||||
|
# device_dict = eval(device)
|
||||||
|
# #pprint.pprint(device_dict)
|
||||||
|
# content = template.render( device_dict )
|
||||||
|
|
||||||
|
# print('{}\n'.format(content))
|
||||||
|
# output.write('{}\n'.format(content))
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@@ -56,8 +72,8 @@ if __name__ == "__main__":
|
|||||||
required=True
|
required=True
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument('-yaml',
|
parser.add_argument('-input',
|
||||||
help='YAML file with devices and config variables',
|
help='Input file with devices and config variables',
|
||||||
required=True
|
required=True
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -69,8 +85,8 @@ if __name__ == "__main__":
|
|||||||
custom_args = parser.parse_known_args()[0]
|
custom_args = parser.parse_known_args()[0]
|
||||||
|
|
||||||
template = custom_args.template
|
template = custom_args.template
|
||||||
yaml = custom_args.yaml
|
input = custom_args.input
|
||||||
output = custom_args.output
|
output = custom_args.output
|
||||||
|
|
||||||
main(template,yaml,output)
|
main(template,input,output)
|
||||||
|
|
||||||
Reference in New Issue
Block a user