Structure handlers to allow .get() to return lists

See #1154. In the end I didn't modify the Attributehandler and
TagHandler like this, instead I added the `return_list` argument
for cases when one wants a guaranteed return.
This commit is contained in:
Griatch 2017-08-27 14:56:05 +02:00
parent 05a3d0435d
commit 92df3ce5ae
13 changed files with 86 additions and 1608 deletions

View file

@ -172,23 +172,25 @@ class ChannelHandler(object):
Initializes the channel handler's internal state.
"""
self.cached_channel_cmds = {}
self.cached_cmdsets = {}
self._cached_channel_cmds = {}
self._cached_cmdsets = {}
self._cached_channels = {}
def __str__(self):
"""
Returns the string representation of the handler
"""
return ", ".join(str(cmd) for cmd in self.cached_channel_cmds)
return ", ".join(str(cmd) for cmd in self._cached_channel_cmds)
def clear(self):
"""
Reset the cache storage.
"""
self.cached_channel_cmds = {}
self.cached_cmdsets = {}
self._cached_channel_cmds = {}
self._cached_cmdsets = {}
self._cached_channels = {}
def add(self, channel):
"""
@ -221,9 +223,11 @@ class ChannelHandler(object):
key = channel.key
cmd.__doc__ = cmd.__doc__.format(channelkey=key,
lower_channelkey=key.strip().lower(),
channeldesc=channel.attributes.get("desc", default="").strip())
self.cached_channel_cmds[channel] = cmd
self.cached_cmdsets = {}
channeldesc=channel.attributes.get(
"desc", default="").strip())
self._cached_channel_cmds[channel] = cmd
self._cached_channels[key] = channel
self._cached_cmdsets = {}
add_channel = add # legacy alias
def remove(self, channel):
@ -247,11 +251,28 @@ class ChannelHandler(object):
global _CHANNELDB
if not _CHANNELDB:
from evennia.comms.models import ChannelDB as _CHANNELDB
self.cached_channel_cmds = {}
self.cached_cmdsets = {}
self._cached_channel_cmds = {}
self._cached_cmdsets = {}
self._cached_channels = {}
for channel in _CHANNELDB.objects.get_all_channels():
self.add(channel)
def get(self, channelname=None):
"""
Get a channel from the handler, or all channels
Args:
channelame (str, optional): Channel key, case insensitive.
Returns
channels (list): The matching channels in a list, or all
channels in the handler.
"""
if channelname:
channel = self._cached_channels.get(channelname.lower(), None)
return [channel] if channel else []
return self._cached_channels.values()
def get_cmdset(self, source_object):
"""
Retrieve cmdset for channels this source_object has
@ -266,12 +287,12 @@ class ChannelHandler(object):
access to.
"""
if source_object in self.cached_cmdsets:
return self.cached_cmdsets[source_object]
if source_object in self._cached_cmdsets:
return self._cached_cmdsets[source_object]
else:
# create a new cmdset holding all viable channels
chan_cmdset = None
chan_cmds = [channelcmd for channel, channelcmd in self.cached_channel_cmds.iteritems()
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:
@ -281,7 +302,7 @@ class ChannelHandler(object):
chan_cmdset.duplicates = True
for cmd in chan_cmds:
chan_cmdset.add(cmd)
self.cached_cmdsets[source_object] = chan_cmdset
self._cached_cmdsets[source_object] = chan_cmdset
return chan_cmdset