diff roundup/scripts/roundup_demo.py @ 8086:39013eba1aaf

refactor: move template prompt to new function; ruff lint cleanups
author John Rouillard <rouilj@ieee.org>
date Sun, 14 Jul 2024 13:27:37 -0400
parents 05f36a527a45
children 0cb81ee2e572
line wrap: on
line diff
--- a/roundup/scripts/roundup_demo.py	Sun Jul 14 13:00:12 2024 -0400
+++ b/roundup/scripts/roundup_demo.py	Sun Jul 14 13:27:37 2024 -0400
@@ -4,10 +4,10 @@
 #
 
 import argparse
-import sys
 
 # --- patch sys.path to make sure 'import roundup' finds correct version
 import os.path as osp
+import sys
 
 thisdir = osp.dirname(osp.abspath(__file__))
 rootdir = osp.dirname(osp.dirname(thisdir))
@@ -18,13 +18,11 @@
 # --/
 
 # import also verifies python version as side effect
-from roundup import version_check                         # noqa: F401 E402
-from roundup import admin, configuration, demo, instance  # noqa: E402
-from roundup import __version__ as roundup_version        # noqa: E402
-from roundup.anypy.my_input import my_input               # noqa: E402
-from roundup.backends import list_backends                # noqa: E402
-from roundup.i18n import _                                # noqa: E402
-
+from roundup import __version__ as roundup_version  # noqa: E402
+from roundup import admin, configuration, demo, instance, version_check  # noqa: F401 E402
+from roundup.anypy.my_input import my_input  # noqa: E402
+from roundup.backends import list_backends  # noqa: E402
+from roundup.i18n import _  # noqa: E402
 
 DEFAULT_HOME = './demo'
 DEFAULT_TEMPLATE = 'classic'
@@ -46,12 +44,29 @@
     if msg:
         print(msg)
 
+
+def ask_for_template(default_template, templates):
+    import pdb; pdb.set_trace()
+    template = my_input(
+        _('Enter tracker template to use (one of (%(template_list)s)) [%(default_template)s]: ') %
+        {'template_list': ','.join(templates),
+         'default_template': default_template})
+
+    if not template:
+        template = default_template
+    elif template not in templates:
+        print("Unknown template: %s. Exiting." % template)
+        return None
+
+    return template
+
+
 def run():
     templates = admin.AdminTool().listTemplates().keys()
     backends = list_backends()
 
     cli = argparse.ArgumentParser(
-        description= """
+        description="""
 Instant gratification demo - Roundup Issue Tracker
 
   Run a demo server. Config and database files are created in
@@ -70,35 +85,35 @@
 
     cli.add_argument('-B', '--bind_address',
                      default="127.0.0.1",
-                     help=( "Choose address for server to listen at.\n"
-                            "Use 0.0.0.0 to bind to all addreses. Use\n"
-                            "the external name of the computer to bind to\n"
-                            "the external host interface.\n"
-                            "Default: %(default)s.\n\n"))
+                     help=("Choose address for server to listen at.\n"
+                           "Use 0.0.0.0 to bind to all addreses. Use\n"
+                           "the external name of the computer to bind to\n"
+                           "the external host interface.\n"
+                           "Default: %(default)s.\n\n"))
     cli.add_argument('-b', '--backend_db',
                      choices=backends,
-                     help=( "Choose backend database. Default: %s.\n\n" %
-                            DEFAULT_BACKEND))
+                     help=("Choose backend database. Default: %s.\n\n" %
+                           DEFAULT_BACKEND))
     cli.add_argument('-H', '--hostname',
                      default="localhost",
-                     help=( "Choose hostname for the server.\n"
-                            "Default: %(default)s.\n\n"
+                     help=("Choose hostname for the server.\n"
+                           "Default: %(default)s.\n\n"
                             ))
     cli.add_argument('-t', '--template',
                      choices=templates,
                      help="Use specified template. (*)\n\n")
     cli.add_argument('-p', '--port',
                      type=int,
-                     help=( "Listen at this port. Default: search for\n"
-                            "open port starting at %s\n\n" % DEFAULT_PORT))
+                     help=("Listen at this port. Default: search for\n"
+                           "open port starting at %s\n\n" % DEFAULT_PORT))
     cli.add_argument('-P', '--urlport',
                      type=int,
-                     help=( "Set docker external port. If using\n"
-                            "   docker ... -p 9090:8917 ...\n"
-                            "this should be set to 9090.\n"
-                            "Default: as selected by --port\n\n"))
+                     help=("Set docker external port. If using\n"
+                           "   docker ... -p 9090:8917 ...\n"
+                           "this should be set to 9090.\n"
+                           "Default: as selected by --port\n\n"))
     cli.add_argument('-V', '--version', action='version',
-                     version='Roundup version %s'%roundup_version,
+                     version='Roundup version %s' % roundup_version,
                      help=(
                          "Show program's version number: %s and exit\n" %
                          roundup_version))
@@ -109,20 +124,21 @@
     # add 'nuke' to choices so backend will accept nuke if only 2 args.
     choices = backends + ['nuke']
     cli.add_argument('backend', nargs='?', metavar='backend', choices=choices,
-                     help=( "Choose backend database. "
-                            "Depricated, use -b instead.\n"
+                     help=("Choose backend database. "
+                           "Depricated, use -b instead.\n"
            "If it is used, you *must* specify directory.\n\n"))
 
     cli.add_argument('nuke', nargs='?', metavar='nuke', choices=['nuke'],
-                     help=( "The word 'nuke' will delete tracker and reset.\n"
-                            "E.G. %(prog)s -b sqlite \\ \n"
-                            "-t classic ./mytracker nuke\n") % {"prog": sys.argv[0]})
+                     help=("The word 'nuke' will delete tracker and reset.\n"
+                           "E.G. %(prog)s -b sqlite \\ \n"
+                           "-t classic ./mytracker nuke\n") % {"prog": sys.argv[0]})
 
     cli_args = cli.parse_args()
 
     # collect all positional args in order in array to parse
     # strip all None.
-    cli_args.cmd = [ x for x in [cli_args.directory, cli_args.backend, cli_args.nuke] if x != None ]
+    cli_args.cmd = [x for x in [cli_args.directory, cli_args.backend, cli_args.nuke]
+                    if x is not None]
 
     try:
         nuke = cli_args.cmd[-1] == 'nuke'
@@ -155,43 +171,34 @@
     # if there is no tracker in home, force nuke
     try:
         instance.open(home)
-        valid_home = True
     except configuration.NoConfigError:
         nuke = True
-        valid_home = False
 
     # if we are to create the tracker, prompt for settings
     if nuke:
         # FIXME: i'd like to have an option to abort the tracker creation
         #   say, by entering a single dot.  but i cannot think of
         #   appropriate prompt for that.
-        if not cli_args.template in templates:
-            template = my_input(
-            _('Enter tracker template to use (one of (%(template_list)s)) [%(default_template)s]: ') %
-            { 'template_list': ','.join(templates),
-              'default_template': template})
+        if cli_args.template not in templates:
+            template = ask_for_template(template, templates)
             if not template:
-                template = DEFAULT_TEMPLATE
-            elif template not in templates:
-                print("Unknown template: %s. Exiting." % template)
-                exit(1)
+                sys.exit(1)
         # install
         url_port = cli_args.urlport or cli_args.port or DEFAULT_PORT
         demo.install_demo(home, backend,
                           admin.AdminTool().listTemplates()[template]['path'],
                           use_port=url_port, use_host=cli_args.hostname)
-    else:
-        # make sure that no options are specified that are only useful on initialization.
-        if ( cli_args.backend or cli_args.template or
-             cli_args.backend_db ):
-            usage(home, cli, msg=(
-                "Specifying backend or template is only allowed when\n"
-                "creating a tracker or with nuke.\n"))
-            exit(1)
+    elif (cli_args.backend or cli_args.template or cli_args.backend_db):
+        # options were specified that are only useful on initialization.
+        usage(home, cli, msg=(
+            "Specifying backend or template is only allowed when\n"
+            "creating a tracker or with nuke.\n"))
+        sys.exit(1)
     # run
     demo.run_demo(home, bind_addr=cli_args.bind_address,
                   bind_port=cli_args.port)
 
+
 if __name__ == '__main__':
     run()
 

Roundup Issue Tracker: http://roundup-tracker.org/