Remove prototypes._RE_DBREF and add new dbref() to protfuncs
This commit is contained in:
parent
f6d62e2f69
commit
979d24a12f
3 changed files with 14 additions and 39 deletions
|
|
@ -37,6 +37,7 @@ prototype key (this value must be possible to serialize in an Attribute).
|
||||||
|
|
||||||
from ast import literal_eval
|
from ast import literal_eval
|
||||||
from random import randint as base_randint, random as base_random, choice as base_choice
|
from random import randint as base_randint, random as base_random, choice as base_choice
|
||||||
|
import re
|
||||||
|
|
||||||
from evennia.utils import search
|
from evennia.utils import search
|
||||||
from evennia.utils.utils import justify as base_justify, is_iter, to_str
|
from evennia.utils.utils import justify as base_justify, is_iter, to_str
|
||||||
|
|
@ -325,3 +326,15 @@ def objlist(*args, **kwargs):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return ["#{}".format(obj.id) for obj in _obj_search(return_list=True, *args, **kwargs)]
|
return ["#{}".format(obj.id) for obj in _obj_search(return_list=True, *args, **kwargs)]
|
||||||
|
|
||||||
|
|
||||||
|
def dbref(*args, **kwargs):
|
||||||
|
"""
|
||||||
|
Usage $dbref(<#dbref>)
|
||||||
|
Returns one Object searched globally by #dbref. Error if #dbref is invalid.
|
||||||
|
"""
|
||||||
|
_RE_DBREF = re.compile(r"\#[0-9]+")
|
||||||
|
if not args or len(args) < 1 or _RE_DBREF.match(args[0]) is None:
|
||||||
|
raise ValueError('$dbref requires a valid #dbref argument.')
|
||||||
|
|
||||||
|
return obj(args[0])
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ Handling storage of prototypes, both database-based ones (DBPrototypes) and thos
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
|
||||||
import hashlib
|
import hashlib
|
||||||
import time
|
import time
|
||||||
from ast import literal_eval
|
from ast import literal_eval
|
||||||
|
|
@ -33,8 +32,6 @@ _PROTOTYPE_TAG_CATEGORY = "from_prototype"
|
||||||
_PROTOTYPE_TAG_META_CATEGORY = "db_prototype"
|
_PROTOTYPE_TAG_META_CATEGORY = "db_prototype"
|
||||||
PROT_FUNCS = {}
|
PROT_FUNCS = {}
|
||||||
|
|
||||||
_RE_DBREF = re.compile(r"\$dbref\((\#[0-9]+)\)")
|
|
||||||
|
|
||||||
|
|
||||||
class PermissionError(RuntimeError):
|
class PermissionError(RuntimeError):
|
||||||
pass
|
pass
|
||||||
|
|
@ -576,9 +573,6 @@ def protfunc_parser(value, available_functions=None, testing=False, stacktrace=F
|
||||||
|
|
||||||
available_functions = PROT_FUNCS if available_functions is None else available_functions
|
available_functions = PROT_FUNCS if available_functions is None else available_functions
|
||||||
|
|
||||||
# $dbref(#Number) becomes $obj(#Number)
|
|
||||||
value = _RE_DBREF.sub("$obj(\\1)", value)
|
|
||||||
|
|
||||||
result = inlinefuncs.parse_inlinefunc(
|
result = inlinefuncs.parse_inlinefunc(
|
||||||
value, available_funcs=available_functions,
|
value, available_funcs=available_functions,
|
||||||
stacktrace=stacktrace, testing=testing, **kwargs)
|
stacktrace=stacktrace, testing=testing, **kwargs)
|
||||||
|
|
|
||||||
|
|
@ -263,38 +263,6 @@ class TestProtFuncs(EvenniaTest):
|
||||||
"prototype_desc": "testing prot",
|
"prototype_desc": "testing prot",
|
||||||
"key": "ExampleObj"}
|
"key": "ExampleObj"}
|
||||||
|
|
||||||
def test_RE_DBREF(self):
|
|
||||||
def check_RE_DBREF(value, expected_value,
|
|
||||||
exp_is_match, exp_is_search):
|
|
||||||
try:
|
|
||||||
mm = protlib._RE_DBREF.match(value)
|
|
||||||
ms = protlib._RE_DBREF.search(value)
|
|
||||||
sub1 = protlib._RE_DBREF.sub("$obj(\\1)", value)
|
|
||||||
assert expected_value == sub1
|
|
||||||
assert (exp_is_match and mm) or (not exp_is_match and mm is None)
|
|
||||||
assert (exp_is_search and ms) or (not exp_is_search and ms is None)
|
|
||||||
except Exception as e:
|
|
||||||
self.fail("%r" % ((value, mm, ms, expected_value, exp_is_match, exp_is_search, e),))
|
|
||||||
|
|
||||||
check_RE_DBREF('1234', '1234', False, False)
|
|
||||||
check_RE_DBREF('#1234', '#1234', False, False)
|
|
||||||
check_RE_DBREF('(#1234)', '(#1234)', False, False)
|
|
||||||
check_RE_DBREF('obj(#1234)', 'obj(#1234)', False, False)
|
|
||||||
check_RE_DBREF('dbref(#1234)', 'dbref(#1234)', False, False)
|
|
||||||
check_RE_DBREF('$obj(#1234)', '$obj(#1234)', False, False)
|
|
||||||
check_RE_DBREF('$dbref(#1234)', '$obj(#1234)', True, True)
|
|
||||||
check_RE_DBREF('obj($obj(#1234))', 'obj($obj(#1234))', False, False)
|
|
||||||
check_RE_DBREF('obj($dbref(#1234))', 'obj($obj(#1234))', False, True)
|
|
||||||
check_RE_DBREF('some #1234 value', 'some #1234 value', False, False)
|
|
||||||
check_RE_DBREF('some (#1234) value', 'some (#1234) value', False, False)
|
|
||||||
check_RE_DBREF('some obj(#1234) value', 'some obj(#1234) value', False, False)
|
|
||||||
check_RE_DBREF('some dbref(#1234) value', 'some dbref(#1234) value', False, False)
|
|
||||||
check_RE_DBREF('some $dbref(#1234) value', 'some $obj(#1234) value', False, True)
|
|
||||||
check_RE_DBREF('some obj($obj(#1234) value)', 'some obj($obj(#1234) value)', False, False)
|
|
||||||
check_RE_DBREF('some obj($dbref(#1234) value)', 'some obj($obj(#1234) value)', False, True)
|
|
||||||
check_RE_DBREF('some dbref($obj(#1234) value)', 'some dbref($obj(#1234) value)', False, False)
|
|
||||||
check_RE_DBREF('some dbref($dbref(#1234) value)', 'some dbref($obj(#1234) value)', False, True)
|
|
||||||
|
|
||||||
@mock.patch("evennia.prototypes.protfuncs.base_random", new=mock.MagicMock(return_value=0.5))
|
@mock.patch("evennia.prototypes.protfuncs.base_random", new=mock.MagicMock(return_value=0.5))
|
||||||
@mock.patch("evennia.prototypes.protfuncs.base_randint", new=mock.MagicMock(return_value=5))
|
@mock.patch("evennia.prototypes.protfuncs.base_randint", new=mock.MagicMock(return_value=5))
|
||||||
def test_protfuncs(self):
|
def test_protfuncs(self):
|
||||||
|
|
@ -419,7 +387,7 @@ class TestProtFuncs(EvenniaTest):
|
||||||
mocked__obj_search.assert_not_called()
|
mocked__obj_search.assert_not_called()
|
||||||
|
|
||||||
with mock.patch("evennia.prototypes.protfuncs._obj_search", wraps=protofuncs._obj_search) as mocked__obj_search:
|
with mock.patch("evennia.prototypes.protfuncs._obj_search", wraps=protofuncs._obj_search) as mocked__obj_search:
|
||||||
self.assertEqual(protlib.protfunc_parser("$dbref(Char)", session=self.session), '<UNKNOWN>')
|
# self.assertEqual(protlib.protfunc_parser("$dbref(Char)", session=self.session), '<UNKNOWN>')
|
||||||
mocked__obj_search.assert_not_called()
|
mocked__obj_search.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue