Changed lockhandler.add/replace to use LockExceptions instead of log_obj, which should also work more flexibly for handling lock errors for players. Resolves #410.

This commit is contained in:
Griatch 2014-04-21 00:24:33 +02:00
parent 9784a2a5c0
commit 3848a789a7
3 changed files with 16 additions and 22 deletions

View file

@ -8,6 +8,7 @@ from django.conf import settings
from src.objects.models import ObjectDB from src.objects.models import ObjectDB
from src.utils import create, utils, search from src.utils import create, utils, search
from src.utils.ansi import raw from src.utils.ansi import raw
from src.locks.lockhandler import LockException
from src.commands.default.muxcommand import MuxCommand from src.commands.default.muxcommand import MuxCommand
from src.commands.cmdhandler import get_and_merge_cmdsets from src.commands.cmdhandler import get_and_merge_cmdsets
@ -1555,7 +1556,10 @@ class CmdLock(ObjManipCommand):
if not obj.access(caller, 'control'): if not obj.access(caller, 'control'):
caller.msg("You are not allowed to do that.") caller.msg("You are not allowed to do that.")
return return
ok = obj.locks.add(lockdef, caller) try:
ok = obj.locks.add(lockdef)
except LockException, e:
caller.msg(str(e))
if ok: if ok:
caller.msg("Added lock '%s' to %s." % (lockdef, obj)) caller.msg("Added lock '%s' to %s." % (lockdef, obj))
return return

View file

@ -115,7 +115,8 @@ __all__ = ("LockHandler", "LockException")
# #
# Exception class # Exception class. This will be raised
# by errors in lock definitions.
# #
class LockException(Exception): class LockException(Exception):
@ -171,7 +172,6 @@ class LockHandler(object):
_cache_lockfuncs() _cache_lockfuncs()
self.obj = obj self.obj = obj
self.locks = {} self.locks = {}
self.log_obj = None
self.reset() self.reset()
def __str__(self): def __str__(self):
@ -179,12 +179,7 @@ class LockHandler(object):
def _log_error(self, message): def _log_error(self, message):
"Try to log errors back to object" "Try to log errors back to object"
if self.log_obj and hasattr(self.log_obj, 'msg'): raise LockException(message)
self.log_obj.msg(message)
elif hasattr(self.obj, 'msg'):
self.obj.msg(message)
else:
logger.log_errmsg("%s: %s" % (self.obj, message))
def _parse_lockstring(self, storage_lockstring): def _parse_lockstring(self, storage_lockstring):
""" """
@ -240,10 +235,9 @@ class LockHandler(object):
wlist.append(_("Lock: access type '%(access_type)s' changed from '%(source)s' to '%(goal)s' " % \ wlist.append(_("Lock: access type '%(access_type)s' changed from '%(source)s' to '%(goal)s' " % \
{"access_type":access_type, "source":locks[access_type][2], "goal":raw_lockstring})) {"access_type":access_type, "source":locks[access_type][2], "goal":raw_lockstring}))
locks[access_type] = (evalstring, tuple(lock_funcs), raw_lockstring) locks[access_type] = (evalstring, tuple(lock_funcs), raw_lockstring)
if wlist and self.log_obj: if wlist:
# a warning text was set, it's not an error, so only report # a warning text was set, it's not an error, so only report
# if log_obj is available. logger.log_warn("\n".join(wlist))
self._log_error("\n".join(wlist))
if elist: if elist:
# an error text was set, raise exception. # an error text was set, raise exception.
raise LockException("\n".join(elist)) raise LockException("\n".join(elist))
@ -269,15 +263,12 @@ class LockHandler(object):
""" """
self.lock_bypass = hasattr(obj, "is_superuser") and obj.is_superuser self.lock_bypass = hasattr(obj, "is_superuser") and obj.is_superuser
def add(self, lockstring, log_obj=None): def add(self, lockstring):
""" """
Add a new lockstring on the form '<access_type>:<functions>'. Multiple Add a new lockstring on the form '<access_type>:<functions>'. Multiple
access types should be separated by semicolon (;). access types should be separated by semicolon (;).
If log_obj is given, it will be fed error information.
""" """
if log_obj:
self.log_obj = log_obj
# sanity checks # sanity checks
for lockdef in lockstring.split(';'): for lockdef in lockstring.split(';'):
if not ':' in lockstring: if not ':' in lockstring:
@ -302,17 +293,16 @@ class LockHandler(object):
# cache the locks will get rid of eventual doublets # cache the locks will get rid of eventual doublets
self._cache_locks(storage_lockstring) self._cache_locks(storage_lockstring)
self._save_locks() self._save_locks()
self.log_obj = None
return True return True
def replace(self, lockstring, log_obj=None): def replace(self, lockstring):
"Replaces the lockstring entirely." "Replaces the lockstring entirely."
old_lockstring = str(self) old_lockstring = str(self)
self.clear() self.clear()
try: try:
return self.add(lockstring, log_obj) return self.add(lockstring)
except LockException: except LockException:
self.add(old_lockstring, log_obj) self.add(old_lockstring)
raise raise
def get(self, access_type=None): def get(self, access_type=None):

View file

@ -12,7 +12,7 @@ class TestLockHandler(unittest.TestCase):
def test_add(self): def test_add(self):
# lock_handler = LockHandler(obj) # lock_handler = LockHandler(obj)
# self.assertEqual(expected, lock_handler.add(lockstring, log_obj)) # self.assertEqual(expected, lock_handler.add(lockstring))
assert True # TODO: implement your test here assert True # TODO: implement your test here
def test_cache_lock_bypass(self): def test_cache_lock_bypass(self):
@ -47,7 +47,7 @@ class TestLockHandler(unittest.TestCase):
def test_replace(self): def test_replace(self):
# lock_handler = LockHandler(obj) # lock_handler = LockHandler(obj)
# self.assertEqual(expected, lock_handler.replace(lockstring, log_obj)) # self.assertEqual(expected, lock_handler.replace(lockstring))
assert True # TODO: implement your test here assert True # TODO: implement your test here
def test_reset(self): def test_reset(self):