Refactor ChannelHandler. Fix issues with new cmdset merge procedures.

This commit is contained in:
Griatch 2016-10-12 23:31:39 +02:00
parent 196f1ad270
commit a61a33e87d
4 changed files with 49 additions and 54 deletions

View file

@ -145,7 +145,7 @@ class ChannelHandler(object):
Initializes the channel handler's internal state.
"""
self.cached_channel_cmds = []
self.cached_channel_cmds = {}
self.cached_cmdsets = {}
def __str__(self):
@ -160,7 +160,8 @@ class ChannelHandler(object):
Reset the cache storage.
"""
self.cached_channel_cmds = []
self.cached_channel_cmds = {}
self.cached_cmdsets = {}
def add(self, channel):
"""
@ -195,7 +196,7 @@ class ChannelHandler(object):
cmd.__doc__ = cmd.__doc__.format(channelkey=key,
lower_channelkey=key.strip().lower(),
channeldesc=channel.attributes.get("desc", default="").strip())
self.cached_channel_cmds.append(cmd)
self.cached_channel_cmds[channel] = cmd
self.cached_cmdsets = {}
add_channel = add # legacy alias
@ -208,10 +209,10 @@ class ChannelHandler(object):
global _CHANNELDB
if not _CHANNELDB:
from evennia.comms.models import ChannelDB as _CHANNELDB
self.cached_channel_cmds = []
self.cached_channel_cmds = {}
self.cached_cmdsets = {}
for channel in _CHANNELDB.objects.get_all_channels():
self.add_channel(channel)
self.add(channel)
def get_cmdset(self, source_object):
"""
@ -221,7 +222,7 @@ class ChannelHandler(object):
Args:
source_object (Object): An object subscribing to one
or more channels.
hannelhandler import CHANNEL_HANDLER
Returns:
cmdsets (list): The Channel-Cmdsets `source_object` has
access to.
@ -230,14 +231,18 @@ hannelhandler import CHANNEL_HANDLER
if source_object in self.cached_cmdsets:
return self.cached_cmdsets[source_object]
else:
# create a new cmdset holding all channels
chan_cmdset = cmdset.CmdSet()
chan_cmdset.key = 'ChannelCmdSet'
chan_cmdset.priority = 101
chan_cmdset.duplicates = True
for cmd in [cmd for cmd in self.cached_channel_cmds
if cmd.access(source_object, 'send')]:
chan_cmdset.add(cmd)
# create a new cmdset holding all viable channels
chan_cmdset = None
chan_cmds = [channelcmd for channel, channelcmd in self.cached_channel_cmds.iteritems()
if channel.subscriptions.has(source_object)
and channelcmd.access(source_object, 'send')]
if chan_cmds:
chan_cmdset = cmdset.CmdSet()
chan_cmdset.key = 'ChannelCmdSet'
chan_cmdset.priority = 101
chan_cmdset.duplicates = True
for cmd in chan_cmds:
chan_cmdset.add(cmd)
self.cached_cmdsets[source_object] = chan_cmdset
return chan_cmdset

View file

@ -109,7 +109,7 @@ class Msg(SharedMemoryModel):
db_hide_from_objects = models.ManyToManyField("objects.ObjectDB", related_name='hide_from_objects_set', null=True, blank=True)
db_hide_from_channels = models.ManyToManyField("ChannelDB", related_name='hide_from_channels_set', null=True, blank=True)
db_tags = models.ManyToManyField(Tag, null=True, blank=True,
db_tags = models.ManyToManyField(Tag, null=True, blank=True,
help_text='tags on this message. Tags are simple string markers to identify, group and alias messages.')
# Database manager
@ -331,7 +331,7 @@ class Msg(SharedMemoryModel):
"""
return self.locks.check(accessing_obj,
access_type=access_type, default=default)
#------------------------------------------------------------
#
# TempMsg
@ -448,6 +448,11 @@ class SubscriptionHandler(object):
"""
self.obj = obj
self._cache = None
def _recache(self):
self._cache = {player : True for player in self.obj.db_subscriptions.all()}
self._cache.update({obj : True for obj in self.obj.db_object_subscriptions.all()})
def has(self, entity):
"""
@ -463,12 +468,9 @@ class SubscriptionHandler(object):
subscriber.
"""
clsname = entity.__dbclass__.__name__
if clsname == "PlayerDB":
return entity in self.obj.db_subscriptions.all()
elif clsname == "ObjectDB":
return entity in self.obj.db_object_subscriptions.all()
if self._cache is None:
self._recache()
return entity in self._cache
def add(self, entity):
"""
@ -492,10 +494,11 @@ class SubscriptionHandler(object):
self.obj.db_object_subscriptions.add(subscriber)
elif clsname == "PlayerDB":
self.obj.db_subscriptions.add(subscriber)
self._recache()
def remove(self, entity):
"""
Remove a subecriber from the channel.
Remove a subscriber from the channel.
Args:
entity (Player, Object or list): The entity or
@ -510,6 +513,7 @@ class SubscriptionHandler(object):
self.obj.db_subscriptions.remove(entity)
elif clsname == "ObjectDB":
self.obj.db_object_subscriptions.remove(entity)
self._recache()
def all(self):
"""
@ -520,8 +524,9 @@ class SubscriptionHandler(object):
may be a mix of Players and Objects!
"""
return list(self.obj.db_subscriptions.all()) + \
list(self.obj.db_object_subscriptions.all())
if self._cache is None:
self._recache()
return self._cache
def clear(self):
"""
@ -530,6 +535,7 @@ class SubscriptionHandler(object):
"""
self.obj.db_subscriptions.clear()
self.obj.db_object_subscriptions.clear()
self._cache = None
class ChannelDB(TypedObject):