Some minor cleanups.

This commit is contained in:
Griatch 2014-12-17 00:50:05 +01:00
parent e48b5dd1d2
commit 192d12d905

View file

@ -488,21 +488,24 @@ def prompt_choice(caller, question="", prompts=None, choicefunc=None, force_choo
This sets up a simple choice questionnaire. Question will be This sets up a simple choice questionnaire. Question will be
asked, followed by a serie of prompts. Note that this isn't asked, followed by a serie of prompts. Note that this isn't
making use of the menu node system. making use of the menu node system.
prompts - prompts of choices questions=
choicefunc - functions callback to be called as func(self) when make choice (self.caller is available) prompts - list of choices
The function's definision should be like func(self, menu_node), and menu_node.key is user's choice. choicefunc - functions callback to be called as func(self) when
make choice (self.caller is available) The function's definision
should be like func(self, menu_node), and menu_node.key is user's
choice.
force_choose - force user to make a choice or not force_choose - force user to make a choice or not
""" """
# creating and defining commands # creating and defining commands
count = 0 count = 0
choices = "" choices = ""
commands = [] commands = []
for choice in prompts: for choice in utils.makeiter(prompts):
count += 1 count += 1
choices += "\n{lc%d{lt[%d]{le %s" % (count, count, choice) choices += "\n{lc%d{lt[%d]{le %s" % (count, count, choice)
cmdfunc = CmdMenuNode(key="%d" % count) cmdfunc = CmdMenuNode(key="%d" % count)
if choicefunc: if choicefunc:
cmdfunc.choicefunc = choicefunc cmdfunc.choicefunc = choicefunc
@ -511,12 +514,12 @@ def prompt_choice(caller, question="", prompts=None, choicefunc=None, force_choo
del self.caller.db._menu_data del self.caller.db._menu_data
self.choicefunc(self) self.choicefunc(self)
cmdfunc.callback = MethodType(_choicefunc, cmdfunc, CmdMenuNode) cmdfunc.callback = MethodType(_choicefunc, cmdfunc, CmdMenuNode)
commands.append(cmdfunc) commands.append(cmdfunc)
if not force_choose: if not force_choose:
choices += "\n{lc{lt[No choice]{le" choices += "\n{lc{lt[No choice]{le"
prompt = question + choices + "\nPlease choose one." prompt = question + choices + "\nPlease choose one."
errorcmd = CmdMenuNode(key=CMD_NOMATCH) errorcmd = CmdMenuNode(key=CMD_NOMATCH)
@ -532,7 +535,7 @@ def prompt_choice(caller, question="", prompts=None, choicefunc=None, force_choo
del self.caller.db._menu_data del self.caller.db._menu_data
self.choicefunc(self) self.choicefunc(self)
errorcmd.callback = MethodType(_errorcmd, errorcmd, CmdMenuNode) errorcmd.callback = MethodType(_errorcmd, errorcmd, CmdMenuNode)
defaultcmd = CmdMenuNode(key=CMD_NOINPUT) defaultcmd = CmdMenuNode(key=CMD_NOINPUT)
if force_choose: if force_choose:
def _defaultcmd(self): def _defaultcmd(self):
@ -546,7 +549,7 @@ def prompt_choice(caller, question="", prompts=None, choicefunc=None, force_choo
del self.caller.db._menu_data del self.caller.db._menu_data
self.choicefunc(self) self.choicefunc(self)
defaultcmd.callback = MethodType(_defaultcmd, defaultcmd, CmdMenuNode) defaultcmd.callback = MethodType(_defaultcmd, defaultcmd, CmdMenuNode)
# creating cmdset (this will already have look/help commands) # creating cmdset (this will already have look/help commands)
choicecmdset = MenuCmdSet() choicecmdset = MenuCmdSet()
for cmdfunc in commands: choicecmdset.add(cmdfunc) for cmdfunc in commands: choicecmdset.add(cmdfunc)
@ -554,7 +557,7 @@ def prompt_choice(caller, question="", prompts=None, choicefunc=None, force_choo
choicecmdset.add(defaultcmd) choicecmdset.add(defaultcmd)
choicecmdset.add(CmdMenuLook()) choicecmdset.add(CmdMenuLook())
choicecmdset.add(CmdMenuHelp()) choicecmdset.add(CmdMenuHelp())
# assinging menu data flags to caller. # assinging menu data flags to caller.
caller.db._menu_data = {"help": "Please select.", caller.db._menu_data = {"help": "Please select.",
"look": prompt} "look": prompt}