Generate html from json

Change-Id: Id305e17d4546d4799739ed4edfecf37f632f46fd
This commit is contained in:
ahothan 2016-02-29 08:37:20 -08:00
parent 4cc32e0fa9
commit 745738df95

View File

@ -664,23 +664,34 @@ class KloudBuster(object):
return quota_dict
def create_html(self, hfp, template, task_re):
cur_time = time.strftime('%Y-%m-%d %A %X %Z', time.localtime(time.time()))
for line in template:
line = line.replace('[[time]]', cur_time)
if CONF.label and CONF.storage:
line = line.replace('[[label]]', ' - ' + CONF.label)
elif CONF.label:
line = line.replace('[[label]]', CONF.label)
else:
line = line.replace('[[label]]', '')
line = line.replace('[[result]]', task_re)
hfp.write(line)
if not CONF.headless:
# bring up the file in the default browser
url = 'file://' + os.path.abspath(CONF.html)
webbrowser.open(url, new=2)
def create_html(hfp, template, task_re):
cur_time = time.strftime('%Y-%m-%d %A %X %Z', time.localtime(time.time()))
for line in template:
line = line.replace('[[time]]', cur_time)
if CONF.label and CONF.storage:
line = line.replace('[[label]]', ' - ' + CONF.label)
elif CONF.label:
line = line.replace('[[label]]', CONF.label)
else:
line = line.replace('[[label]]', '')
line = line.replace('[[result]]', task_re)
hfp.write(line)
if not CONF.headless:
# bring up the file in the default browser
url = 'file://' + os.path.abspath(CONF.html)
webbrowser.open(url, new=2)
def generate_charts(json_results, html_file_name):
'''Save results in HTML format file.'''
LOG.info('Saving results in HTML file: ' + html_file_name + "...")
if CONF.storage:
template_path = resource_filename(__name__, 'template_storage.html')
else:
template_path = resource_filename(__name__, 'template_http.html')
with open(html_file_name, 'w') as hfp, open(template_path, 'r') as template:
create_html(hfp,
template,
json.dumps(json_results, sort_keys=True))
def main():
cli_opts = [
@ -730,13 +741,25 @@ def main():
help="Do not read env variables"),
cfg.BoolOpt("show-config",
default=False,
help="Show the default configuration")
help="Show the default configuration"),
cfg.StrOpt("charts-from-json",
default=None,
help='create charts from json results and exit (requires --html)'),
]
CONF.register_cli_opts(cli_opts)
CONF.set_default("verbose", True)
full_version = __version__ + ', VM image: ' + kb_vm_agent.get_image_name()
CONF(sys.argv[1:], project="kloudbuster", version=full_version)
if CONF.charts_from_json:
if not CONF.html:
print 'Error: you must specify the destination html file name using --html'
sys.exit(1)
with open(CONF.charts_from_json, 'r') as jfp:
json_results = json.load(jfp)
generate_charts(json_results, CONF.html)
sys.exit(0)
if CONF.show_config:
print resource_string(__name__, "cfg.scale.yaml")
sys.exit(0)
@ -766,16 +789,7 @@ def main():
json.dump(kloudbuster.final_result, jfp, indent=4, sort_keys=True)
if CONF.html:
'''Save results in HTML format file.'''
LOG.info('Saving results in HTML file: ' + CONF.html + "...")
if CONF.storage:
template_path = resource_filename(__name__, 'template_storage.html')
else:
template_path = resource_filename(__name__, 'template_http.html')
with open(CONF.html, 'w') as hfp, open(template_path, 'r') as template:
kloudbuster.create_html(hfp,
template,
json.dumps(kloudbuster.final_result, sort_keys=True))
generate_charts(kloudbuster.final_result, CONF.html)
if __name__ == '__main__':