Add functioning, if primitive edit/add to decorator

This commit is contained in:
Griatch 2018-04-09 23:13:54 +02:00
parent dc7cd2e69a
commit c340c08adb
2 changed files with 45 additions and 27 deletions

View file

@ -1170,10 +1170,11 @@ def list_node(option_generator, select=None, examine=None, edit=None, add=None,
except Exception: except Exception:
caller.msg("|rInvalid choice.|n") caller.msg("|rInvalid choice.|n")
else: else:
try: if select:
return select(caller, selection) try:
except Exception: return select(caller, selection)
logger.log_trace() except Exception:
logger.log_trace()
return None return None
def _input_parser(caller, raw_string, **kwargs): def _input_parser(caller, raw_string, **kwargs):
@ -1185,7 +1186,7 @@ def list_node(option_generator, select=None, examine=None, edit=None, add=None,
which processors are available. which processors are available.
""" """
mode, selection, new_value = None, None, None mode, selection, args = None, None, None
available_choices = kwargs.get("available_choices", []) available_choices = kwargs.get("available_choices", [])
cmd, args = re.search(r"(^[a-zA-Z]*)\s*(.*?)$", raw_string).groups() cmd, args = re.search(r"(^[a-zA-Z]*)\s*(.*?)$", raw_string).groups()
@ -1193,13 +1194,12 @@ def list_node(option_generator, select=None, examine=None, edit=None, add=None,
cmd = cmd.lower().strip() cmd = cmd.lower().strip()
if cmd.startswith('a') and add: if cmd.startswith('a') and add:
mode = "add" mode = "add"
new_value = args
else: else:
selection, new_value = re.search(r"(^[0-9]*)\s*(.*?)$", args).groups() selection, args = re.search(r"(^[0-9]*)\s*(.*?)$", args).groups()
try: try:
selection = int(selection) - 1 selection = int(selection) - 1
except ValueError: except ValueError:
caller.msg("|rInvalid input|n") mode = "look"
else: else:
# edits are on the form 'edit <num> <args> # edits are on the form 'edit <num> <args>
if cmd.startswith("e") and edit: if cmd.startswith("e") and edit:
@ -1212,7 +1212,7 @@ def list_node(option_generator, select=None, examine=None, edit=None, add=None,
caller.msg("|rInvalid index|n") caller.msg("|rInvalid index|n")
mode = None mode = None
return mode, selection, new_value return mode, selection, args
def _relay_to_edit_or_add(caller, raw_string, **kwargs): def _relay_to_edit_or_add(caller, raw_string, **kwargs):
pass pass
@ -1222,9 +1222,11 @@ def list_node(option_generator, select=None, examine=None, edit=None, add=None,
mode = kwargs.get("list_mode", None) mode = kwargs.get("list_mode", None)
option_list = option_generator(caller) if callable(option_generator) else option_generator option_list = option_generator(caller) if callable(option_generator) else option_generator
print("option_list: {}, {}".format(option_list, mode))
npages = 0 npages = 0
page_index = 0 page_index = 0
page = None page = []
options = [] options = []
if option_list: if option_list:
@ -1241,7 +1243,7 @@ def list_node(option_generator, select=None, examine=None, edit=None, add=None,
if mode == "arbitrary": if mode == "arbitrary":
# freeform input, we must parse it for the allowed commands (look/edit) # freeform input, we must parse it for the allowed commands (look/edit)
mode, selection, new_value = _input_parser(caller, raw_string, mode, selection, args = _input_parser(caller, raw_string,
**{"available_choices": page}) **{"available_choices": page})
if examine and mode == "look": if examine and mode == "look":
@ -1262,7 +1264,7 @@ def list_node(option_generator, select=None, examine=None, edit=None, add=None,
elif add and mode == 'add': elif add and mode == 'add':
# add mode - the selection is the new value # add mode - the selection is the new value
try: try:
text, options = add(caller, selection, **kwargs) text, options = add(caller, args)
except Exception: except Exception:
logger.log_trace() logger.log_trace()
text = "|rCould not add." text = "|rCould not add."
@ -1270,7 +1272,7 @@ def list_node(option_generator, select=None, examine=None, edit=None, add=None,
elif edit and mode == 'edit': elif edit and mode == 'edit':
try: try:
text, options = edit(caller, selection, **kwargs) text, options = edit(caller, selection, args)
except Exception: except Exception:
logger.log_trace() logger.log_trace()
text = "|Could not edit." text = "|Could not edit."
@ -1279,14 +1281,13 @@ def list_node(option_generator, select=None, examine=None, edit=None, add=None,
else: else:
# normal mode - list # normal mode - list
if select: # We have a processor to handle selecting an entry
# We have a processor to handle selecting an entry
# dynamic, multi-page option list. Each selection leads to the `select` # dynamic, multi-page option list. Each selection leads to the `select`
# callback being called with a result from the available choices # callback being called with a result from the available choices
options.extend([{"desc": opt, options.extend([{"desc": opt,
"goto": (_select_parser, "goto": (_select_parser,
{"available_choices": page})} for opt in page]) {"available_choices": page})} for opt in page])
if npages > 1: if npages > 1:
# if the goto callable returns None, the same node is rerun, and # if the goto callable returns None, the same node is rerun, and

View file

@ -1066,16 +1066,19 @@ def _caller_tags(caller):
tags = prot.get("tags") tags = prot.get("tags")
return tags return tags
def _add_tags(caller, tag, **kwargs):
def _add_tag(caller, tag, **kwargs):
tag = tag.strip().lower() tag = tag.strip().lower()
metaprot = _get_menu_metaprot(caller) metaprot = _get_menu_metaprot(caller)
tags = metaprot.tags prot = metaprot.prototype
tags = prot.get('tags', [])
if tags: if tags:
if tag not in tags: if tag not in tags:
tags.append(tag) tags.append(tag)
else: else:
tags = [tag] tags = [tag]
metaprot.tags = tags prot['tags'] = tags
_set_menu_metaprot(caller, "prototype", prot)
text = kwargs.get("text") text = kwargs.get("text")
if not text: if not text:
text = "Added tag {}. (return to continue)".format(tag) text = "Added tag {}. (return to continue)".format(tag)
@ -1084,12 +1087,26 @@ def _add_tags(caller, tag, **kwargs):
return text, options return text, options
def _edit_tag(caller, tag, **kwargs): def _edit_tag(caller, old_tag, new_tag, **kwargs):
tag = tag.strip().lower()
metaprot = _get_menu_metaprot(caller) metaprot = _get_menu_metaprot(caller)
#TODO change in evmenu so one can do e 3 <new tag> right away, parse & store value in kwarg prototype = metaprot.prototype
tags = prototype.get('tags', [])
@list_node(_caller_tags, edit=_edit_tags) old_tag = old_tag.strip().lower()
new_tag = new_tag.strip().lower()
tags[tags.index(old_tag)] = new_tag
prototype['tags'] = tags
_set_menu_metaprot(caller, 'prototype', prototype)
text = kwargs.get('text')
if not text:
text = "Changed tag {} to {}.".format(old_tag, new_tag)
options = {"key": "_default",
"goto": lambda caller: None}
return text, options
@list_node(_caller_tags, edit=_edit_tag, add=_add_tag)
def node_tags(caller): def node_tags(caller):
text = "Set the prototype's |yTags|n." text = "Set the prototype's |yTags|n."
options = _wizard_options("tags", "attrs", "locks") options = _wizard_options("tags", "attrs", "locks")