diff --git a/orderform.py b/orderform.py new file mode 100755 index 0000000..5bacfab --- /dev/null +++ b/orderform.py @@ -0,0 +1,53 @@ +#! /usr/bin/python3 +# -*- coding: UTF-8 -*- + +"""Order the infos in a form file. + +Input is: + a config file + +Output is: + the same config file ordered as described in readformdata + +""" + +import os.path +import argparse +import readformdata + + +def parse(): + """Create the argument parser (including the help) and parse the arguments. + + Options/ Arguments: + formfile + + """ + parser = argparse.ArgumentParser( + description="Order a form file.") + parser.add_argument("formfile", + help="The form file to be ordered.") + parser.add_argument("--output", default=None, # nargs=1, + # None = input + help="The newly created file." + + " (Default: input is overridden.)") + args = vars(parser.parse_args()) + args["output"] = (args["formfile"] + if args["output"] is None + else args["output"]) + for f in (args["formfile"], args["output"]): + if not os.path.isfile(f): + parser.print_help() + raise argparse.ArgumentTypeError(f + " is not a valid file.") + return args + + +if __name__ == "__main__": + try: + arguments = parse() + except argparse.ArgumentTypeError as e: + print("Error while parsing the command line arguments:\n", e) + else: + config = readformdata.listFields([], arguments["formfile"]) + # make dict out of [[]] list + dict from config file + readformdata.writeFields(config, arguments["output"])