Skip to content

Commit 37e5367

Browse files
committed
WIP: Allow fields to be created in adminui.Menus.
Convert modules.echo.admin to use adminui.Menu (see #63).
1 parent 52d9af0 commit 37e5367

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

metabot/modules/echo.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,22 @@ def echo(ctx, msg, data): # pylint: disable=missing-docstring
3737
def admin(ctx, msg, frame):
3838
"""Handle /admin BOTNAME echo."""
3939

40-
modconf = frame.value
41-
command, _, text = frame.text.partition(' ')
42-
command = command.lower()
43-
44-
if not command:
40+
menu = adminui.Menu()
41+
for command, data in sorted(frame.value.items()):
42+
menu.add(command, desc=data.get('text', '').replace('\n', ' '))
43+
frame, handler = menu.select(ctx, msg, frame, create=True)
44+
if not handler:
4545
msg.action = 'Choose a command'
4646
msg.add(
4747
"Type the name of a command to add (like <code>rules</code>\u2014don't include a slash "
4848
'at the beginning!), or select an existing echo.')
49-
for command, data in sorted(modconf.items()):
50-
title = '/' + command
51-
if data.get('text'):
52-
title = '%s (%s)' % (title, data['text'].replace('\n', ' '))
53-
msg.button(title, command)
54-
return
49+
return menu.display(ctx, msg, frame, 'command')
5550

56-
msg.path(command)
51+
msg.path(frame.field)
5752

5853
adminui.Menu(
5954
('text', adminui.freeform,
60-
'The message, sticker, or image to send in response to /%s.' % command),
55+
'The message, sticker, or image to send in response to /%s.' % frame.field),
6156
('paginate', adminui.bool, 'For multiline messages, display just one line at a time?'),
6257
('private', adminui.bool, 'Send the message in group chats, or just in private?'),
63-
).handle(ctx, msg, adminui.Frame(modconf, command, None, text))
58+
).handle(ctx, msg, frame)

metabot/modules/test_echo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def test_admin(conversation): # pylint: disable=redefined-outer-name
166166
Bot Admin › modulestestbot › echo: <b>Choose a command</b>
167167
168168
Type the name of a command to add (like <code>rules</code>—don't include a slash at the beginning!), or select an existing echo.
169-
[/echotest (new message) | /admin modulestestbot echo echotest]
169+
[echotest new message | /admin modulestestbot echo echotest]
170170
[Back | /admin modulestestbot]
171171
"""
172172

@@ -186,7 +186,7 @@ def test_admin(conversation): # pylint: disable=redefined-outer-name
186186
Bot Admin › modulestestbot › echo: <b>Choose a command</b>
187187
188188
Type the name of a command to add (like <code>rules</code>—don't include a slash at the beginning!), or select an existing echo.
189-
[/echotest (new message) | /admin modulestestbot echo echotest]
190-
[/textless | /admin modulestestbot echo textless]
189+
[echotest new message | /admin modulestestbot echo echotest]
190+
[textless | /admin modulestestbot echo textless]
191191
[Back | /admin modulestestbot]
192192
"""

metabot/util/adminui.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,17 @@ def add(self, field, handler=True, desc=None):
4444

4545
self.fields += ((field, handler, desc),)
4646

47-
def select(self, unused_ctx, msg, frame):
47+
def select(self, unused_ctx, msg, frame, create=False):
4848
"""Identify the selected subframe."""
4949

5050
field, _, text = frame.text.lstrip().partition(' ')
51+
field = field.lower()
5152
for fieldname, handler, desc in self.fields:
52-
if fieldname == field:
53-
return Frame(frame.value, field, desc, text), handler
53+
if fieldname.lower() == field:
54+
return Frame(frame.value, fieldname, desc, text), handler
5455
if field:
56+
if create:
57+
return Frame(frame.value, field, None, text), create
5558
msg.add("I can't set <code>%s</code>.", field)
5659
return frame, None
5760

0 commit comments

Comments
 (0)