Ran black on codes

This commit is contained in:
Griatch 2020-09-27 17:55:08 +02:00
parent fad306b932
commit 7e58fee171
2 changed files with 54 additions and 31 deletions

View file

@ -47,6 +47,7 @@ from ast import literal_eval
from evennia import EvMenu from evennia import EvMenu
from fnmatch import fnmatch from fnmatch import fnmatch
# i18n # i18n
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
@ -54,7 +55,8 @@ _RE_NODE = re.compile(r"#\s*?NODE\s+?(?P<nodename>\S+?)$", re.I + re.M)
_RE_OPTIONS_SEP = re.compile(r"##\s*?OPTIONS\s*?$", re.I + re.M) _RE_OPTIONS_SEP = re.compile(r"##\s*?OPTIONS\s*?$", re.I + re.M)
_RE_CALLABLE = re.compile(r"\S+?\(\)", re.I + re.M) _RE_CALLABLE = re.compile(r"\S+?\(\)", re.I + re.M)
_RE_CALLABLE = re.compile( _RE_CALLABLE = re.compile(
r"(?P<funcname>\S+?)(?:\((?P<kwargs>[\S\s]+?=[\S\s]+?)\)|\(\))", re.I+re.M) r"(?P<funcname>\S+?)(?:\((?P<kwargs>[\S\s]+?=[\S\s]+?)\)|\(\))", re.I + re.M
)
_HELP_NO_OPTION_MATCH = _("Choose an option or try 'help'.") _HELP_NO_OPTION_MATCH = _("Choose an option or try 'help'.")
@ -68,10 +70,11 @@ _OPTION_COMMENT_START = "#"
# Input/option/goto handler functions that allows for dynamically generated # Input/option/goto handler functions that allows for dynamically generated
# nodes read from the menu template. # nodes read from the menu template.
def _generated_goto_func(caller, raw_string, **kwargs): def _generated_goto_func(caller, raw_string, **kwargs):
goto = kwargs['goto'] goto = kwargs["goto"]
goto_callables = kwargs['goto_callables'] goto_callables = kwargs["goto_callables"]
current_nodename = kwargs['current_nodename'] current_nodename = kwargs["current_nodename"]
if _RE_CALLABLE.match(goto): if _RE_CALLABLE.match(goto):
gotofunc = goto.strip()[:-2] gotofunc = goto.strip()[:-2]
@ -84,9 +87,9 @@ def _generated_goto_func(caller, raw_string, **kwargs):
def _generated_input_goto_func(caller, raw_string, **kwargs): def _generated_input_goto_func(caller, raw_string, **kwargs):
gotomap = kwargs['gotomap'] gotomap = kwargs["gotomap"]
goto_callables = kwargs['goto_callables'] goto_callables = kwargs["goto_callables"]
current_nodename = kwargs['current_nodename'] current_nodename = kwargs["current_nodename"]
# start with glob patterns # start with glob patterns
for pattern, goto in gotomap.items(): for pattern, goto in gotomap.items():
@ -158,8 +161,7 @@ def parse_menu_template(caller, menu_template, goto_callables=None):
inputparsemap = {} inputparsemap = {}
for inum, optline in enumerate(optionlist): for inum, optline in enumerate(optionlist):
if (optline.startswith(_OPTION_COMMENT_START) if optline.startswith(_OPTION_COMMENT_START) or _OPTION_SEP_MARKER not in optline:
or _OPTION_SEP_MARKER not in optline):
# skip comments or invalid syntax # skip comments or invalid syntax
continue continue
key = "" key = ""
@ -181,17 +183,21 @@ def parse_menu_template(caller, menu_template, goto_callables=None):
if main_key.startswith(_OPTION_INPUT_MARKER): if main_key.startswith(_OPTION_INPUT_MARKER):
# if we have a pattern, build the arguments for _default later # if we have a pattern, build the arguments for _default later
pattern = main_key[len(_OPTION_INPUT_MARKER):].strip() pattern = main_key[len(_OPTION_INPUT_MARKER) :].strip()
inputparsemap[pattern] = goto inputparsemap[pattern] = goto
print(f"registering input goto {pattern} -> {goto}") print(f"registering input goto {pattern} -> {goto}")
else: else:
# a regular goto string/callable target # a regular goto string/callable target
option = { option = {
"key": key, "key": key,
"goto": (_generated_goto_func, { "goto": (
"goto": goto, _generated_goto_func,
"current_nodename": nodename, {
"goto_callables": goto_callables}) "goto": goto,
"current_nodename": nodename,
"goto_callables": goto_callables,
},
),
} }
if desc: if desc:
option["desc"] = desc option["desc"] = desc
@ -199,14 +205,19 @@ def parse_menu_template(caller, menu_template, goto_callables=None):
if inputparsemap: if inputparsemap:
# if this exists we must create a _default entry too # if this exists we must create a _default entry too
options.append({ options.append(
"key": "_default", {
"goto": (_generated_input_goto_func, { "key": "_default",
"gotomap": inputparsemap, "goto": (
"current_nodename": nodename, _generated_input_goto_func,
"goto_callables": goto_callables {
}) "gotomap": inputparsemap,
}) "current_nodename": nodename,
"goto_callables": goto_callables,
},
),
}
)
return options return options
@ -233,9 +244,15 @@ def parse_menu_template(caller, menu_template, goto_callables=None):
return _parse(caller, menu_template, goto_callables) return _parse(caller, menu_template, goto_callables)
def template2menu(caller, menu_template, goto_callables=None, def template2menu(
startnode="start", startnode_input=None, persistent=False, caller,
**kwargs): menu_template,
goto_callables=None,
startnode="start",
startnode_input=None,
persistent=False,
**kwargs,
):
""" """
Helper function to generate and start an EvMenu based on a menu template Helper function to generate and start an EvMenu based on a menu template
string. string.
@ -268,19 +285,25 @@ def template2menu(caller, menu_template, goto_callables=None,
startnode_kwargs.update(startnode_input[1]) startnode_kwargs.update(startnode_input[1])
menu_tree = parse_menu_template(caller, menu_template, goto_callables) menu_tree = parse_menu_template(caller, menu_template, goto_callables)
EvMenu(caller, menu_tree, EvMenu(
startnode_input=(startnode_raw, startnode_kwargs), caller,
persistent=True, **kwargs) menu_tree,
startnode_input=(startnode_raw, startnode_kwargs),
persistent=True,
**kwargs,
)
def gotonode3(caller, raw_string, **kwargs): def gotonode3(caller, raw_string, **kwargs):
print("in gotonode3", caller, raw_string, kwargs) print("in gotonode3", caller, raw_string, kwargs)
return None return None
def foo(caller, raw_string, **kwargs): def foo(caller, raw_string, **kwargs):
print("in foo", caller, raw_string, kwargs) print("in foo", caller, raw_string, kwargs)
return "node2" return "node2"
def bar(caller, raw_string, **kwargs): def bar(caller, raw_string, **kwargs):
print("in bar", caller, raw_string, kwargs) print("in bar", caller, raw_string, kwargs)
return "bar" return "bar"
@ -288,8 +311,7 @@ def bar(caller, raw_string, **kwargs):
def test_generator(caller): def test_generator(caller):
MENU_TEMPLATE = \ MENU_TEMPLATE = """
"""
# node start # node start
Neque ea alias perferendis molestiae eligendi. Debitis exercitationem Neque ea alias perferendis molestiae eligendi. Debitis exercitationem

View file

@ -548,7 +548,8 @@ class EvMenu:
if clashing_kwargs: if clashing_kwargs:
raise RuntimeError( raise RuntimeError(
f"Evmenu startnode_inputs includes kwargs {tuple(clashing_kwargs)} that " f"Evmenu startnode_inputs includes kwargs {tuple(clashing_kwargs)} that "
"clashes with EvMenu's internal usage.") "clashes with EvMenu's internal usage."
)
# start the menu # start the menu
self.goto(self._startnode, startnode_input, **startnode_kwargs) self.goto(self._startnode, startnode_input, **startnode_kwargs)