Ported optparse to argparse as it is deprecated

master
Ferry Boender 4 years ago
parent 60f7a56d5a
commit 1757c65284
  1. 116
      src/scriptform.py

@ -6,7 +6,7 @@ Main ScriptForm program
""" """
import sys import sys
import optparse import argparse
import os import os
import json import json
import logging import logging
@ -150,34 +150,52 @@ def main(): # pragma: no cover
""" """
main method main method
""" """
usage = [ parser = argparse.ArgumentParser(description='My Application.')
sys.argv[0] + " [option] (--start|--stop) <form_definition.json>", parser.add_argument('--version',
" " + sys.argv[0] + " --generate-pw", action='version',
] version='%(prog)s %%VERSION%%')
parser = optparse.OptionParser(version="%%VERSION%%") parser.add_argument('-g', '--generate-pw',
parser.set_usage('\n'.join(usage)) action='store_true',
default=False,
parser.add_option("-g", "--generate-pw", dest="generate_pw", help='Generate password')
action="store_true", default=False, parser.add_argument('-p', '--port',
help="Generate password") metavar='PORT',
parser.add_option("-p", "--port", dest="port", action="store", type="int", dest='port',
default=8081, help="Port to listen on (default=8081)") type=int,
parser.add_option("-f", "--foreground", dest="foreground", default=8081,
action="store_true", default=False, help='Port to listen on (default=8081)')
help="Run in foreground (debugging)") parser.add_argument('-f', '--foreground',
parser.add_option("-r", "--reload", dest="reload", action="store_true", dest='foreground',
default=False, action='store_true',
help="Reload form config on every request (DEV)") default=False,
parser.add_option("--pid-file", dest="pid_file", action="store", help='Run in foreground (debugging)')
default=None, help="Pid file") parser.add_argument('-r', '--reload',
parser.add_option("--log-file", dest="log_file", action="store", dest='reload',
default=None, help="Log file") action='store_true',
parser.add_option("--start", dest="action_start", action="store_true", default=False,
default=None, help="Start daemon") help='Reload form config on every request (DEV)')
parser.add_option("--stop", dest="action_stop", action="store_true", parser.add_argument('--pid-file',
default=None, help="Stop daemon") metavar='PATH',
dest='pid_file',
(options, args) = parser.parse_args() type=str,
default=None,
help='Pid file')
parser.add_argument('--log-file',
metavar='PATH',
dest='log_file',
type=str,
default=None,
help='Log file')
parser.add_argument('--stop',
dest='action_stop',
action='store_true',
default=None,
help='Stop daemon')
parser.add_argument(dest='config',
metavar="CONFIG_FILE",
help="Path to form definition config",
)
options = parser.parse_args()
if options.generate_pw: if options.generate_pw:
# Generate a password for use in the `users` section # Generate a password for use in the `users` section
@ -190,35 +208,23 @@ def main(): # pragma: no cover
sys.stdout.write("{}\n".format(sha)) sys.stdout.write("{}\n".format(sha))
sys.exit(0) sys.exit(0)
else: else:
if not options.action_stop and not args: # Switch to dir of form definition configuration
parser.error("Insufficient number of arguments") formconfig_path = os.path.realpath(options.config)
if not options.action_stop and not options.action_start: os.chdir(os.path.dirname(formconfig_path))
options.action_start = True
# If a form configuration was specified, change to that dir so we can
# find the job scripts and such.
if args:
path = os.path.dirname(args[0])
if path:
os.chdir(path)
args[0] = os.path.basename(args[0])
# Initialize daemon so we can start or stop it
daemon = Daemon(options.pid_file, options.log_file, daemon = Daemon(options.pid_file, options.log_file,
foreground=options.foreground) foreground=options.foreground)
log = logging.getLogger('MAIN')
try: if options.action_stop:
if options.action_start: daemon.stop()
cache = not options.reload sys.exit(0)
scriptform_instance = ScriptForm(args[0], cache=cache) else:
daemon.register_shutdown_callback(scriptform_instance.shutdown) cache = not options.reload
daemon.start() scriptform_instance = ScriptForm(formconfig_path, cache=cache)
scriptform_instance.run(listen_port=options.port) daemon.register_shutdown_callback(scriptform_instance.shutdown)
elif options.action_stop: daemon.start()
daemon.stop() scriptform_instance.run(listen_port=options.port)
sys.exit(0)
except Exception as err:
log.exception(err)
raise
if __name__ == "__main__": # pragma: no cover if __name__ == "__main__": # pragma: no cover

Loading…
Cancel
Save