Unittests pass for all protfuncs
This commit is contained in:
parent
ec52ca1d55
commit
e601e03884
4 changed files with 52 additions and 28 deletions
|
|
@ -263,21 +263,21 @@ def _obj_search(*args, **kwargs):
|
||||||
query = "".join(args)
|
query = "".join(args)
|
||||||
session = kwargs.get("session", None)
|
session = kwargs.get("session", None)
|
||||||
return_list = kwargs.pop("return_list", False)
|
return_list = kwargs.pop("return_list", False)
|
||||||
|
account = None
|
||||||
|
|
||||||
if not session:
|
if session:
|
||||||
raise ValueError("$obj called by Evennia without Session. This is not supported.")
|
|
||||||
account = session.account
|
account = session.account
|
||||||
if not account:
|
|
||||||
raise ValueError("$obj requires a logged-in account session.")
|
|
||||||
targets = search.search_object(query)
|
|
||||||
|
|
||||||
print("targets: {}".format(targets))
|
targets = search.search_object(query)
|
||||||
|
|
||||||
if return_list:
|
if return_list:
|
||||||
retlist = []
|
retlist = []
|
||||||
|
if account:
|
||||||
for target in targets:
|
for target in targets:
|
||||||
if target.access(account, target, 'control'):
|
if target.access(account, target, 'control'):
|
||||||
retlist.append(target)
|
retlist.append(target)
|
||||||
|
else:
|
||||||
|
retlist = targets
|
||||||
return retlist
|
return retlist
|
||||||
else:
|
else:
|
||||||
# single-match
|
# single-match
|
||||||
|
|
@ -288,6 +288,7 @@ def _obj_search(*args, **kwargs):
|
||||||
"query or use $objlist instead.".format(
|
"query or use $objlist instead.".format(
|
||||||
query=query, nmatches=len(targets)))
|
query=query, nmatches=len(targets)))
|
||||||
target = targets[0]
|
target = targets[0]
|
||||||
|
if account:
|
||||||
if not target.access(account, target, 'control'):
|
if not target.access(account, target, 'control'):
|
||||||
raise ValueError("$obj: Obj {target}(#{dbref} cannot be added - "
|
raise ValueError("$obj: Obj {target}(#{dbref} cannot be added - "
|
||||||
"Account {account} does not have 'control' access.".format(
|
"Account {account} does not have 'control' access.".format(
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ _PROTOTYPE_TAG_META_CATEGORY = "db_prototype"
|
||||||
_PROT_FUNCS = {}
|
_PROT_FUNCS = {}
|
||||||
|
|
||||||
|
|
||||||
_RE_DBREF = re.compile(r"(?<!\$obj\()#[0-9]+")
|
_RE_DBREF = re.compile(r"(?<!\$obj\()(#[0-9]+)")
|
||||||
|
|
||||||
|
|
||||||
class PermissionError(RuntimeError):
|
class PermissionError(RuntimeError):
|
||||||
|
|
@ -52,7 +52,7 @@ for mod in settings.PROT_FUNC_MODULES:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
def protfunc_parser(value, available_functions=None, testing=False, **kwargs):
|
def protfunc_parser(value, available_functions=None, testing=False, stacktrace=False, **kwargs):
|
||||||
"""
|
"""
|
||||||
Parse a prototype value string for a protfunc and process it.
|
Parse a prototype value string for a protfunc and process it.
|
||||||
|
|
||||||
|
|
@ -65,6 +65,7 @@ def protfunc_parser(value, available_functions=None, testing=False, **kwargs):
|
||||||
available_functions (dict, optional): Mapping of name:protfunction to use for this parsing.
|
available_functions (dict, optional): Mapping of name:protfunction to use for this parsing.
|
||||||
testing (bool, optional): Passed to protfunc. If in a testing mode, some protfuncs may
|
testing (bool, optional): Passed to protfunc. If in a testing mode, some protfuncs may
|
||||||
behave differently.
|
behave differently.
|
||||||
|
stacktrace (bool, optional): If set, print the stack parsing process of the protfunc-parser.
|
||||||
|
|
||||||
Kwargs:
|
Kwargs:
|
||||||
session (Session): Passed to protfunc. Session of the entity spawning the prototype.
|
session (Session): Passed to protfunc. Session of the entity spawning the prototype.
|
||||||
|
|
@ -91,11 +92,9 @@ def protfunc_parser(value, available_functions=None, testing=False, **kwargs):
|
||||||
value = _RE_DBREF.sub("$obj(\\1)", value)
|
value = _RE_DBREF.sub("$obj(\\1)", value)
|
||||||
|
|
||||||
result = inlinefuncs.parse_inlinefunc(
|
result = inlinefuncs.parse_inlinefunc(
|
||||||
value, available_funcs=available_functions, testing=testing, **kwargs)
|
value, available_funcs=available_functions,
|
||||||
|
stacktrace=stacktrace, testing=testing, **kwargs)
|
||||||
|
|
||||||
# at this point we have a string where all procfuncs were parsed
|
|
||||||
# print("parse_inlinefuncs(\"{}\", available_funcs={}) => {}".format(value, available_functions, result))
|
|
||||||
result = value_to_obj_or_any(result)
|
|
||||||
err = None
|
err = None
|
||||||
try:
|
try:
|
||||||
result = literal_eval(result)
|
result = literal_eval(result)
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,10 @@ Unit tests for the prototypes and spawner
|
||||||
|
|
||||||
from random import randint
|
from random import randint
|
||||||
import mock
|
import mock
|
||||||
from anything import Anything, Something
|
from anything import Something
|
||||||
from django.test.utils import override_settings
|
from django.test.utils import override_settings
|
||||||
from evennia.utils.test_resources import EvenniaTest
|
from evennia.utils.test_resources import EvenniaTest
|
||||||
from evennia.prototypes import spawner, prototypes as protlib, protfuncs
|
from evennia.prototypes import spawner, prototypes as protlib
|
||||||
|
|
||||||
from evennia.prototypes.prototypes import _PROTOTYPE_TAG_META_CATEGORY
|
from evennia.prototypes.prototypes import _PROTOTYPE_TAG_META_CATEGORY
|
||||||
|
|
||||||
|
|
@ -227,6 +227,18 @@ class TestProtFuncs(EvenniaTest):
|
||||||
"$eval({'test': '1', 2:3, 3: $toint(3.5)})"), {'test': '1', 2: 3, 3: 3})
|
"$eval({'test': '1', 2:3, 3: $toint(3.5)})"), {'test': '1', 2: 3, 3: 3})
|
||||||
|
|
||||||
self.assertEqual(protlib.protfunc_parser("$obj(#1)", session=self.session), '#1')
|
self.assertEqual(protlib.protfunc_parser("$obj(#1)", session=self.session), '#1')
|
||||||
|
self.assertEqual(protlib.protfunc_parser("#1", session=self.session), '#1')
|
||||||
|
self.assertEqual(protlib.protfunc_parser("$obj(Char)", session=self.session), '#6')
|
||||||
|
self.assertEqual(protlib.protfunc_parser("$obj(Char)", session=self.session), '#6')
|
||||||
|
self.assertEqual(protlib.protfunc_parser("$objlist(#1)", session=self.session), ['#1'])
|
||||||
|
|
||||||
|
self.assertEqual(protlib.value_to_obj(
|
||||||
|
protlib.protfunc_parser("#6", session=self.session)), self.char1)
|
||||||
|
self.assertEqual(protlib.value_to_obj_or_any(
|
||||||
|
protlib.protfunc_parser("#6", session=self.session)), self.char1)
|
||||||
|
self.assertEqual(protlib.value_to_obj_or_any(
|
||||||
|
protlib.protfunc_parser("[1,2,3,'#6',5]", session=self.session)),
|
||||||
|
[1, 2, 3, self.char1, 5])
|
||||||
|
|
||||||
|
|
||||||
class TestPrototypeStorage(EvenniaTest):
|
class TestPrototypeStorage(EvenniaTest):
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,8 @@ Error handling:
|
||||||
import re
|
import re
|
||||||
import fnmatch
|
import fnmatch
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from evennia.utils import utils
|
|
||||||
|
from evennia.utils import utils, logger
|
||||||
|
|
||||||
|
|
||||||
# example/testing inline functions
|
# example/testing inline functions
|
||||||
|
|
@ -264,7 +265,7 @@ class InlinefuncError(RuntimeError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def parse_inlinefunc(string, strip=False, available_funcs=None, **kwargs):
|
def parse_inlinefunc(string, strip=False, available_funcs=None, stacktrace=False, **kwargs):
|
||||||
"""
|
"""
|
||||||
Parse the incoming string.
|
Parse the incoming string.
|
||||||
|
|
||||||
|
|
@ -274,6 +275,7 @@ def parse_inlinefunc(string, strip=False, available_funcs=None, **kwargs):
|
||||||
execute them.
|
execute them.
|
||||||
available_funcs (dict, optional): Define an alternative source of functions to parse for.
|
available_funcs (dict, optional): Define an alternative source of functions to parse for.
|
||||||
If unset, use the functions found through `settings.INLINEFUNC_MODULES`.
|
If unset, use the functions found through `settings.INLINEFUNC_MODULES`.
|
||||||
|
stacktrace (bool, optional): If set, print the stacktrace to log.
|
||||||
Kwargs:
|
Kwargs:
|
||||||
session (Session): This is sent to this function by Evennia when triggering
|
session (Session): This is sent to this function by Evennia when triggering
|
||||||
it. It is passed to the inlinefunc.
|
it. It is passed to the inlinefunc.
|
||||||
|
|
@ -307,12 +309,18 @@ def parse_inlinefunc(string, strip=False, available_funcs=None, **kwargs):
|
||||||
ncallable = 0
|
ncallable = 0
|
||||||
nlparens = 0
|
nlparens = 0
|
||||||
|
|
||||||
# print("STRING: {} =>".format(string))
|
if stacktrace:
|
||||||
|
out = "STRING: {} =>".format(string)
|
||||||
|
print(out)
|
||||||
|
logger.log_info(out)
|
||||||
|
|
||||||
for match in _RE_TOKEN.finditer(string):
|
for match in _RE_TOKEN.finditer(string):
|
||||||
gdict = match.groupdict()
|
gdict = match.groupdict()
|
||||||
|
|
||||||
# print(" MATCH: {}".format({key: val for key, val in gdict.items() if val}))
|
if stacktrace:
|
||||||
|
out = " MATCH: {}".format({key: val for key, val in gdict.items() if val})
|
||||||
|
print(out)
|
||||||
|
logger.log_info(out)
|
||||||
|
|
||||||
if gdict["singlequote"]:
|
if gdict["singlequote"]:
|
||||||
stack.append(gdict["singlequote"])
|
stack.append(gdict["singlequote"])
|
||||||
|
|
@ -399,7 +407,11 @@ def parse_inlinefunc(string, strip=False, available_funcs=None, **kwargs):
|
||||||
retval = "" if strip else func(*args, **kwargs)
|
retval = "" if strip else func(*args, **kwargs)
|
||||||
return utils.to_str(retval, force_string=True)
|
return utils.to_str(retval, force_string=True)
|
||||||
retval = "".join(_run_stack(item) for item in stack)
|
retval = "".join(_run_stack(item) for item in stack)
|
||||||
# print("STACK: \n{} => {}\n".format(stack, retval))
|
if stacktrace:
|
||||||
|
out = "STACK: \n{} => {}\n".format(stack, retval)
|
||||||
|
print(out)
|
||||||
|
logger.log_info(out)
|
||||||
|
|
||||||
# execute the stack
|
# execute the stack
|
||||||
return retval
|
return retval
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue