Unittests pass for all protfuncs

This commit is contained in:
Griatch 2018-06-21 22:42:50 +02:00
parent 0100a75977
commit 2fcf6d1640
4 changed files with 52 additions and 28 deletions

View file

@ -263,21 +263,21 @@ def _obj_search(*args, **kwargs):
query = "".join(args)
session = kwargs.get("session", None)
return_list = kwargs.pop("return_list", False)
account = None
if session:
account = session.account
if not session:
raise ValueError("$obj called by Evennia without Session. This is not supported.")
account = session.account
if not account:
raise ValueError("$obj requires a logged-in account session.")
targets = search.search_object(query)
print("targets: {}".format(targets))
if return_list:
retlist = []
for target in targets:
if target.access(account, target, 'control'):
retlist.append(target)
if account:
for target in targets:
if target.access(account, target, 'control'):
retlist.append(target)
else:
retlist = targets
return retlist
else:
# single-match
@ -288,11 +288,12 @@ def _obj_search(*args, **kwargs):
"query or use $objlist instead.".format(
query=query, nmatches=len(targets)))
target = targets[0]
if not target.access(account, target, 'control'):
raise ValueError("$obj: Obj {target}(#{dbref} cannot be added - "
"Account {account} does not have 'control' access.".format(
target=target.key, dbref=target.id, account=account))
return target
if account:
if not target.access(account, target, 'control'):
raise ValueError("$obj: Obj {target}(#{dbref} cannot be added - "
"Account {account} does not have 'control' access.".format(
target=target.key, dbref=target.id, account=account))
return target
def obj(*args, **kwargs):

View file

@ -27,7 +27,7 @@ _PROTOTYPE_TAG_META_CATEGORY = "db_prototype"
_PROT_FUNCS = {}
_RE_DBREF = re.compile(r"(?<!\$obj\()#[0-9]+")
_RE_DBREF = re.compile(r"(?<!\$obj\()(#[0-9]+)")
class PermissionError(RuntimeError):
@ -52,7 +52,7 @@ for mod in settings.PROT_FUNC_MODULES:
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.
@ -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.
testing (bool, optional): Passed to protfunc. If in a testing mode, some protfuncs may
behave differently.
stacktrace (bool, optional): If set, print the stack parsing process of the protfunc-parser.
Kwargs:
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)
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
try:
result = literal_eval(result)

View file

@ -5,10 +5,10 @@ Unit tests for the prototypes and spawner
from random import randint
import mock
from anything import Anything, Something
from anything import Something
from django.test.utils import override_settings
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
@ -227,6 +227,18 @@ class TestProtFuncs(EvenniaTest):
"$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("#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):

View file

@ -63,7 +63,8 @@ Error handling:
import re
import fnmatch
from django.conf import settings
from evennia.utils import utils
from evennia.utils import utils, logger
# example/testing inline functions
@ -264,7 +265,7 @@ class InlinefuncError(RuntimeError):
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.
@ -274,6 +275,7 @@ def parse_inlinefunc(string, strip=False, available_funcs=None, **kwargs):
execute them.
available_funcs (dict, optional): Define an alternative source of functions to parse for.
If unset, use the functions found through `settings.INLINEFUNC_MODULES`.
stacktrace (bool, optional): If set, print the stacktrace to log.
Kwargs:
session (Session): This is sent to this function by Evennia when triggering
it. It is passed to the inlinefunc.
@ -307,12 +309,18 @@ def parse_inlinefunc(string, strip=False, available_funcs=None, **kwargs):
ncallable = 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):
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"]:
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)
return utils.to_str(retval, force_string=True)
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
return retval