-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2af9b23
commit 388df6d
Showing
2 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
Program that: | ||
- checks JSON schema of flavors of Analyers and Responders | ||
- Fixes JSON file with missing data | ||
This program can be run on the whole repository or on a specific JSON file | ||
""" | ||
|
||
import json | ||
import jsonschema | ||
from jsonschema import validate | ||
import sys | ||
import os | ||
import argparse | ||
|
||
FLAVOR_FILE="utils/flavors/flavor_schema.json" | ||
|
||
def printJson(fjson:dict) -> str: | ||
print(json.dumps(fjson, indent=4)) | ||
|
||
|
||
def printSuccess(success:bool, filepath:str) -> str: | ||
if success: | ||
print("{} {}".format("\u2705",filepath)) | ||
else: | ||
print("{} {}".format("\u274c",filepath)) | ||
|
||
def openJsonFile(filename:str) -> dict: | ||
try: | ||
with open(filename, "r") as fjson: | ||
j = json.load(fjson) | ||
fjson.close() | ||
return j | ||
except OSError: | ||
print("Could not open/read file:", fjson) | ||
sys.exit() | ||
|
||
def fixJsonFlavorFile(jsonfile:dict) -> dict: | ||
service_logo = { | ||
"path": "", | ||
"caption": "logo" | ||
} | ||
|
||
screenshots = [ | ||
{ | ||
"path": "", | ||
"caption": "" | ||
} | ||
] | ||
for I in [ | ||
"registration_required", | ||
"subscription_required", | ||
"free_subscription", | ||
"service_homepage", | ||
]: | ||
if I in jsonfile: | ||
break | ||
jsonfile[I] = "N/A" | ||
if "service_logo" not in jsonfile: | ||
jsonfile["service_logo"] = service_logo | ||
if "screenshots" not in jsonfile: | ||
jsonfile["screenshots"] = screenshots | ||
return jsonfile | ||
|
||
def validateFlavorFormat(flavorfile:str, schemafile:str) -> str: | ||
# flavorSchema = openJsonFile(FLAVOR_FILE) | ||
flavorSchema = openJsonFile(schemafile) | ||
fjson = openJsonFile(flavorfile) | ||
|
||
validator = jsonschema.Draft7Validator(flavorSchema, format_checker=jsonschema.draft7_format_checker) | ||
errors = sorted(validator.iter_errors(fjson),key=lambda e: e.path) | ||
if not errors: | ||
printSuccess(True, flavorfile) | ||
else: | ||
printSuccess(False, flavorfile) | ||
for error in errors: | ||
print("{}: {}".format(error.path,error.message)) | ||
# print('------') | ||
|
||
|
||
def run(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("-r", "--report", help="Generate report for all JSON flavors of all Analyzers and responders", action="store_true", default=False) | ||
parser.add_argument("-f", "--file", help="Validate JSON of the Flavor definition file") | ||
parser.add_argument("-s", "--schema", help="JSON Schema of a flavor", default="utils/flavors/flavor_schema.json") | ||
|
||
args = parser.parse_args() | ||
try: | ||
if os.path.isfile(args.schema) and args.schema.endswith(".json"): | ||
if args.report: | ||
path = ["analyzers", "responders"] | ||
for p in path: | ||
for neuron in os.listdir(p): | ||
# print(os.path.join(p,neuron)) | ||
for file in os.listdir(os.path.join(p,neuron)): | ||
if file.endswith(".json"): | ||
filepath = os.path.join(p,neuron,file) | ||
validateFlavorFormat(filepath, args.schema) | ||
|
||
if args.file: | ||
filepath = args.file | ||
try: | ||
if os.path.isfile(filepath) and filepath.endswith(".json"): | ||
validateFlavorFormat(filepath, args.schema) | ||
else: | ||
print("Error: Check this is a file, json formatted, and it has .json extention\n {}".format(filepath)) | ||
except Exception as e: | ||
print(e) | ||
|
||
except Exception as e: | ||
print(e) | ||
|
||
if __name__ == '__main__': | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"version": { | ||
"type": "string" | ||
}, | ||
"author": { | ||
"type": "string" | ||
}, | ||
"url": { | ||
"type": "string", | ||
"format": "uri" | ||
}, | ||
"license": { | ||
"type": "string" | ||
}, | ||
"description": { | ||
"type": "string" | ||
}, | ||
"dataTypeList": { | ||
"type": "array" | ||
}, | ||
"command": { | ||
"type": "string" | ||
}, | ||
"baseConfig": { | ||
"type": "string" | ||
}, | ||
"config": { | ||
"type": "object", | ||
"properties": { | ||
"service": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"configurationItems": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definition/configurationItem" | ||
} | ||
}, | ||
"registration_required": { | ||
"type": [ | ||
"boolean", | ||
"string" | ||
] | ||
}, | ||
"subscription_required": { | ||
"type": [ | ||
"boolean", | ||
"string" | ||
] | ||
}, | ||
"free_subscription": { | ||
"type": [ | ||
"boolean", | ||
"string" | ||
] | ||
}, | ||
"service_homepage": { | ||
"type": "string" | ||
}, | ||
"service_logo": { | ||
"type": "object" | ||
}, | ||
"screenshots": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definition/screenshot" | ||
} | ||
} | ||
}, | ||
"required": [ | ||
"name", | ||
"version", | ||
"author", | ||
"url", | ||
"license", | ||
"description", | ||
"dataTypeList", | ||
"command", | ||
"baseConfig", | ||
"registration_required", | ||
"subscription_required", | ||
"free_subscription" | ||
], | ||
"definition": { | ||
"configurationItem": { | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"description": { | ||
"type": "string" | ||
}, | ||
"multi": { | ||
"type": "boolean" | ||
}, | ||
"required": { | ||
"type": "boolean" | ||
}, | ||
"defaultValue": { | ||
"type": ["string", "number","boolean", "array"] | ||
} | ||
}, | ||
"required": [ | ||
"name", | ||
"description", | ||
"multi", | ||
"required" | ||
] | ||
}, | ||
"screenshot": { | ||
"type": "object", | ||
"properties": { | ||
"path": { | ||
"type": "string" | ||
}, | ||
"caption": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"path", | ||
"caption" | ||
] | ||
} | ||
} | ||
} |