Skip to content

Commit de0130b

Browse files
committed
refactor: move template prompt to new function; ruff lint cleanups
1 parent c606134 commit de0130b

File tree

1 file changed

+57
-50
lines changed

1 file changed

+57
-50
lines changed

roundup/scripts/roundup_demo.py

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#
55

66
import argparse
7-
import sys
87

98
# --- patch sys.path to make sure 'import roundup' finds correct version
109
import os.path as osp
10+
import sys
1111

1212
thisdir = osp.dirname(osp.abspath(__file__))
1313
rootdir = osp.dirname(osp.dirname(thisdir))
@@ -18,13 +18,11 @@
1818
# --/
1919

2020
# import also verifies python version as side effect
21-
from roundup import version_check # noqa: F401 E402
22-
from roundup import admin, configuration, demo, instance # noqa: E402
23-
from roundup import __version__ as roundup_version # noqa: E402
24-
from roundup.anypy.my_input import my_input # noqa: E402
25-
from roundup.backends import list_backends # noqa: E402
26-
from roundup.i18n import _ # noqa: E402
27-
21+
from roundup import __version__ as roundup_version # noqa: E402
22+
from roundup import admin, configuration, demo, instance, version_check # noqa: F401 E402
23+
from roundup.anypy.my_input import my_input # noqa: E402
24+
from roundup.backends import list_backends # noqa: E402
25+
from roundup.i18n import _ # noqa: E402
2826

2927
DEFAULT_HOME = './demo'
3028
DEFAULT_TEMPLATE = 'classic'
@@ -46,12 +44,29 @@ def usage(home, cli, msg=''):
4644
if msg:
4745
print(msg)
4846

47+
48+
def ask_for_template(default_template, templates):
49+
import pdb; pdb.set_trace()
50+
template = my_input(
51+
_('Enter tracker template to use (one of (%(template_list)s)) [%(default_template)s]: ') %
52+
{'template_list': ','.join(templates),
53+
'default_template': default_template})
54+
55+
if not template:
56+
template = default_template
57+
elif template not in templates:
58+
print("Unknown template: %s. Exiting." % template)
59+
return None
60+
61+
return template
62+
63+
4964
def run():
5065
templates = admin.AdminTool().listTemplates().keys()
5166
backends = list_backends()
5267

5368
cli = argparse.ArgumentParser(
54-
description= """
69+
description="""
5570
Instant gratification demo - Roundup Issue Tracker
5671
5772
Run a demo server. Config and database files are created in
@@ -70,35 +85,35 @@ def run():
7085

7186
cli.add_argument('-B', '--bind_address',
7287
default="127.0.0.1",
73-
help=( "Choose address for server to listen at.\n"
74-
"Use 0.0.0.0 to bind to all addreses. Use\n"
75-
"the external name of the computer to bind to\n"
76-
"the external host interface.\n"
77-
"Default: %(default)s.\n\n"))
88+
help=("Choose address for server to listen at.\n"
89+
"Use 0.0.0.0 to bind to all addreses. Use\n"
90+
"the external name of the computer to bind to\n"
91+
"the external host interface.\n"
92+
"Default: %(default)s.\n\n"))
7893
cli.add_argument('-b', '--backend_db',
7994
choices=backends,
80-
help=( "Choose backend database. Default: %s.\n\n" %
81-
DEFAULT_BACKEND))
95+
help=("Choose backend database. Default: %s.\n\n" %
96+
DEFAULT_BACKEND))
8297
cli.add_argument('-H', '--hostname',
8398
default="localhost",
84-
help=( "Choose hostname for the server.\n"
85-
"Default: %(default)s.\n\n"
99+
help=("Choose hostname for the server.\n"
100+
"Default: %(default)s.\n\n"
86101
))
87102
cli.add_argument('-t', '--template',
88103
choices=templates,
89104
help="Use specified template. (*)\n\n")
90105
cli.add_argument('-p', '--port',
91106
type=int,
92-
help=( "Listen at this port. Default: search for\n"
93-
"open port starting at %s\n\n" % DEFAULT_PORT))
107+
help=("Listen at this port. Default: search for\n"
108+
"open port starting at %s\n\n" % DEFAULT_PORT))
94109
cli.add_argument('-P', '--urlport',
95110
type=int,
96-
help=( "Set docker external port. If using\n"
97-
" docker ... -p 9090:8917 ...\n"
98-
"this should be set to 9090.\n"
99-
"Default: as selected by --port\n\n"))
111+
help=("Set docker external port. If using\n"
112+
" docker ... -p 9090:8917 ...\n"
113+
"this should be set to 9090.\n"
114+
"Default: as selected by --port\n\n"))
100115
cli.add_argument('-V', '--version', action='version',
101-
version='Roundup version %s'%roundup_version,
116+
version='Roundup version %s' % roundup_version,
102117
help=(
103118
"Show program's version number: %s and exit\n" %
104119
roundup_version))
@@ -109,20 +124,21 @@ def run():
109124
# add 'nuke' to choices so backend will accept nuke if only 2 args.
110125
choices = backends + ['nuke']
111126
cli.add_argument('backend', nargs='?', metavar='backend', choices=choices,
112-
help=( "Choose backend database. "
113-
"Depricated, use -b instead.\n"
127+
help=("Choose backend database. "
128+
"Depricated, use -b instead.\n"
114129
"If it is used, you *must* specify directory.\n\n"))
115130

116131
cli.add_argument('nuke', nargs='?', metavar='nuke', choices=['nuke'],
117-
help=( "The word 'nuke' will delete tracker and reset.\n"
118-
"E.G. %(prog)s -b sqlite \\ \n"
119-
"-t classic ./mytracker nuke\n") % {"prog": sys.argv[0]})
132+
help=("The word 'nuke' will delete tracker and reset.\n"
133+
"E.G. %(prog)s -b sqlite \\ \n"
134+
"-t classic ./mytracker nuke\n") % {"prog": sys.argv[0]})
120135

121136
cli_args = cli.parse_args()
122137

123138
# collect all positional args in order in array to parse
124139
# strip all None.
125-
cli_args.cmd = [ x for x in [cli_args.directory, cli_args.backend, cli_args.nuke] if x != None ]
140+
cli_args.cmd = [x for x in [cli_args.directory, cli_args.backend, cli_args.nuke]
141+
if x is not None]
126142

127143
try:
128144
nuke = cli_args.cmd[-1] == 'nuke'
@@ -155,43 +171,34 @@ def run():
155171
# if there is no tracker in home, force nuke
156172
try:
157173
instance.open(home)
158-
valid_home = True
159174
except configuration.NoConfigError:
160175
nuke = True
161-
valid_home = False
162176

163177
# if we are to create the tracker, prompt for settings
164178
if nuke:
165179
# FIXME: i'd like to have an option to abort the tracker creation
166180
# say, by entering a single dot. but i cannot think of
167181
# appropriate prompt for that.
168-
if not cli_args.template in templates:
169-
template = my_input(
170-
_('Enter tracker template to use (one of (%(template_list)s)) [%(default_template)s]: ') %
171-
{ 'template_list': ','.join(templates),
172-
'default_template': template})
182+
if cli_args.template not in templates:
183+
template = ask_for_template(template, templates)
173184
if not template:
174-
template = DEFAULT_TEMPLATE
175-
elif template not in templates:
176-
print("Unknown template: %s. Exiting." % template)
177-
exit(1)
185+
sys.exit(1)
178186
# install
179187
url_port = cli_args.urlport or cli_args.port or DEFAULT_PORT
180188
demo.install_demo(home, backend,
181189
admin.AdminTool().listTemplates()[template]['path'],
182190
use_port=url_port, use_host=cli_args.hostname)
183-
else:
184-
# make sure that no options are specified that are only useful on initialization.
185-
if ( cli_args.backend or cli_args.template or
186-
cli_args.backend_db ):
187-
usage(home, cli, msg=(
188-
"Specifying backend or template is only allowed when\n"
189-
"creating a tracker or with nuke.\n"))
190-
exit(1)
191+
elif (cli_args.backend or cli_args.template or cli_args.backend_db):
192+
# options were specified that are only useful on initialization.
193+
usage(home, cli, msg=(
194+
"Specifying backend or template is only allowed when\n"
195+
"creating a tracker or with nuke.\n"))
196+
sys.exit(1)
191197
# run
192198
demo.run_demo(home, bind_addr=cli_args.bind_address,
193199
bind_port=cli_args.port)
194200

201+
195202
if __name__ == '__main__':
196203
run()
197204

0 commit comments

Comments
 (0)