diff --git a/evennia/prototypes/protfuncs.py b/evennia/prototypes/protfuncs.py index 4c9d9a4a5f..6dff62ef96 100644 --- a/evennia/prototypes/protfuncs.py +++ b/evennia/prototypes/protfuncs.py @@ -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): diff --git a/evennia/prototypes/prototypes.py b/evennia/prototypes/prototypes.py index 81bb4188a2..ac343b3ec6 100644 --- a/evennia/prototypes/prototypes.py +++ b/evennia/prototypes/prototypes.py @@ -27,7 +27,7 @@ _PROTOTYPE_TAG_META_CATEGORY = "db_prototype" _PROT_FUNCS = {} -_RE_DBREF = re.compile(r"(? {}".format(value, available_functions, result)) - result = value_to_obj_or_any(result) err = None try: result = literal_eval(result) diff --git a/evennia/prototypes/tests.py b/evennia/prototypes/tests.py index c49292bbf5..0eeb236fb2 100644 --- a/evennia/prototypes/tests.py +++ b/evennia/prototypes/tests.py @@ -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): diff --git a/evennia/utils/inlinefuncs.py b/evennia/utils/inlinefuncs.py index f60f9f0d8a..d62493c786 100644 --- a/evennia/utils/inlinefuncs.py +++ b/evennia/utils/inlinefuncs.py @@ -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