# We do all our imports at the top of our program. import argparse import os import sys import validictory import json import ConsoleUtils # Give the program a name. program_name = 'ValidationWalker' # Describe what the program does beiefly. program_description = 'Validates ScoreWalker output against JSON schemas.' author = 'Chris Diesch' # The argument parser for the program. parser = argparse.ArgumentParser(prog=program_name, description=program_description, add_help=False) # Error and Warning console values: red_error = '\033[91mError:\033[0m' yellow_warning = '\033[93mWARNING:\033[0m' blue_okay = '\033[94mOK\033[0m' build_date = '2017.07.31' program_version = '1.0.0' side_bound_char = '|' line_break_char = '-' corner_char = '+' line_break_size = 80 line_break = line_break_char * line_break_size printer = ConsoleUtils.SLPrinter(program_name) sys.stdout = printer schema = {} schema_loc = r'C:\Users\chris\Documents\Code\Git\scorewalker-utils\ValidationWalker\ClassificationOutput.json' with open(schema_loc) as reader: schema = json.loads(reader.read()) # This is the main function of the program. def main(walker_output): print('Validating JSON in "%s"' % walker_output) with open(walker_output) as walker_json: validictory.validate(json.load(walker_json), schema) print('JSON in "%s" is valid' % walker_output) # This is where we do a check of the arguments If necessary (perhaps you only take certain inputs for an argument). # NOTE: We MUST define each optional argument as global here, so that we may change the default value defined above. def check_args(walker_output): fatal_errors = False if not os.path.exists(walker_output): print('Error: File at "%s" does not exist' % walker_output) fatal_errors = True # We only exit if a required argument was bad, we can handle optional arguments. if fatal_errors: parser.print_help() print('Exiting...') exit(0) # Prints the argument values passed in. def show_args(walker_output): print('Loading schema from "%s"' % schema_loc) print('ScoreWalker output to validate: "%s"' % walker_output) # This may or may not need to be changed depending on your use-case, it generates a header based on the values: # side_bound_char, line_break, program_name, program_version, build_date, and author def show_header(): printer.write_no_prefix(ConsoleUtils.get_header(program_name, program_version, build_date, author, 80)) # Set up arguments here; more groups can be made if required. def make_args(): # A group for required arguments required_args = parser.add_argument_group('Required') optional_args = parser.add_argument_group('Optional') required_args.add_argument('-i', '--input_file', required=True, help='Use {arg} as the path to the file to validate against the schema.') optional_args.add_argument('-h', '--help', action="help", help='Prints the help message.') # This is where we call the main method from. if __name__ == '__main__': show_header() make_args() # Get the arguments, we don't need to do a check for required arguments, since the ArgumentParser class does that # for us already in the 'parse_args' method. We can do custom argument checks in our 'check_args' method. args = parser.parse_args() # Get the argument. walker_file = args.input_file # Do an argument check. show_args(walker_file) check_args(walker_file) # Now we can run... main(walker_file)