externalise order form: create script orderform that orders the infos in a form file
This commit is contained in:
parent
b5ca04bc87
commit
febb2414a7
1 changed files with 53 additions and 0 deletions
53
orderform.py
Executable file
53
orderform.py
Executable file
|
@ -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"])
|
Loading…
Reference in a new issue