Modified the file to not run comments and added a safeguard to improper arguments.

Added checks for json keys and readable error messages.
Added checking for json files that are actually lists

Change-Id: If307a6e0c939bfac722081cce2e4934fee432e65
This commit is contained in:
Alexander Hirschfeld 2015-04-07 23:02:55 -05:00
parent 71b7e7f67c
commit 243feea429
1 changed files with 74 additions and 50 deletions

124
tools/jsonToRst.py Normal file → Executable file
View File

@ -1,4 +1,6 @@
opyright 2015 Alexander Hirschfeld
#!/usr/bin/env python
#
# Copyright 2015 Alexander Hirschfeld
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
@ -15,14 +17,6 @@ opyright 2015 Alexander Hirschfeld
import json
import sys
with open(sys.argv[1]) as f:
data = json.load(f)
outFileName = sys.argv[1].replace("json", "rst")
print("reading from", sys.argv[1])
print("writing to", outFileName)
def printHelpArrays(input):
if(len(input) == 0):
@ -33,28 +27,53 @@ def printHelpArrays(input):
return output[0:-2]
inFileName = "NONE"
for potentialFile in sys.argv:
if ".json" in potentialFile:
inFileName = potentialFile
if inFileName is "NONE":
print "Please pass the JSON file"
sys.exit(1)
print "reading from", inFileName
with open(inFileName) as f:
data = json.load(f)
outFileName = inFileName.replace("json", "rst")
print "writing to", outFileName
outFile = open(outFileName, "w")
# intro
with open(outFileName, "w") as outFile:
if not isinstance(data,dict) or data.get('id') is None:
print 'Make sure there is a valid id'
print 'Make sure this is a valid file'
sys.exit(1)
line01 = "OpenStack DefCore %s" % data["id"]
line01 = "OpenStack DefCore %s" % data["id"]
outFile.write('='*len(line01) + '\n')
outFile.write(line01 + '\n')
outFile.write('='*len(line01) + '\n')
outFile.write('='*len(line01) + '\n')
outFile.write(line01 + '\n')
outFile.write('='*len(line01) + '\n')
# Nonlooping
# Nonlooping
if data.get('platform') is None:
print "The platform section is not found"
sys.exit(1)
outFile.write("""
Status: {status}
Replaces: {replaces}
outFile.write("""
:Status: {status}
:Replaces: {replaces}
This document outlines the mandatory and advisory capabilities
required to exist in a software installation in order to be
eligible to use marks controlled by the OpenStack Foundation.
This document supersedes the companion JSON version.
This document was generated from the master JSON version.
Releases Covered
==============================
@ -62,44 +81,49 @@ Applies to {releases}
Platform Components
==============================
Required: {platformRequired}
:Required: {platformRequired}
Advisory: {platformAdvisory}
:Advisory: {platformAdvisory}
Deprecated: {platformDepricated}
:Deprecated: {platformDepric}
Removed: {platformRemoved}
""".format(status=data["status"],
replaces=data["replaces"],
releases=printHelpArrays(data["releases"]),
platformRequired=printHelpArrays(data["platform"]["required"]),
platformAdvisory=printHelpArrays(data["platform"]["advisory"]),
platformDepricated=printHelpArrays(data["platform"]["deprecated"]),
platformRemoved=printHelpArrays(data["platform"]["removed"])))
:Removed: {platformRemoved}
""".format(status=data.get("status"),
replaces=data.get("replaces"),
releases=printHelpArrays(data.get("releases")),
platformRequired=printHelpArrays(data["platform"].get("required")),
platformAdvisory=printHelpArrays(data["platform"].get("advisory")),
platformDepric=printHelpArrays(data["platform"].get("deprecated")),
platformRemoved=printHelpArrays(data["platform"].get("removed"))))
# looping
if data.get('components') is None:
print "No components found"
sys.exit(1)
# looping
components = sorted(data["components"].keys())
order = ["required", "advisory", "deprecated", "removed"]
for component in components:
outFile.write("""
components = sorted(data["components"].keys())
order = ["required", "advisory", "deprecated", "removed"]
for component in components:
outFile.write("""
{component} Component Capabilities
====================================
""".format(component=component.capitalize()))
for event in order:
outFile.write("\n{event} Capabilities \n".format(
event=event.capitalize()))
outFile.write("--------------------- \n")
if(len(data['components'][component][event]) == 0):
outFile.write("None \n")
for req in data['components'][component][event]:
outFile.write("* {name} ({project})\n".format(
name=data["capabilities"][req]["name"].capitalize(),
project=data["capabilities"][req]["project"].capitalize()))
outFile.close()
""".format(component=component.capitalize()))
for event in order:
outFile.write("\n{event} Capabilities \n".format(
event=event.capitalize()))
outFile.write("----------------------- \n")
if(len(data['components'][component][event]) == 0):
outFile.write("None \n")
for req in data['components'][component][event]:
if not data["capabilities"][req].get('name') is None:
outFile.write("* {name} ({project})\n".format(
name=data["capabilities"][req]["name"].capitalize(),
project=data["capabilities"][req].get("project")))
else:
print "{ capabilities /", req, "/ name } does not exist"
outFile.write("* {name} ({project})\n".format(
name=req.capitalize(),
project=data["capabilities"][req].get("project")))