3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-25 04:02:45 +01:00

Break up classes.ChannelState into structures.(IRC)CaseInsensitiveDict

This commit is contained in:
James Lu 2017-08-21 21:31:50 -07:00
parent 6981869c06
commit 3e150d8514
2 changed files with 50 additions and 31 deletions

View File

@ -17,7 +17,6 @@ import inspect
import ipaddress import ipaddress
import queue import queue
import functools import functools
import collections
import string import string
import re import re
@ -38,18 +37,10 @@ class ProtocolError(RuntimeError):
### Internal classes (users, servers, channels) ### Internal classes (users, servers, channels)
class ChannelState(collections.abc.MutableMapping): class ChannelState(structures.IRCCaseInsensitiveDict):
""" """
A dictionary storing channels case insensitively. Channel objects are initialized on access. A dictionary storing channels case insensitively. Channel objects are initialized on access.
""" """
def __init__(self, irc):
self._data = {}
self._irc = irc
def _keymangle(self, key):
"""Converts the given key to lowercase."""
return self._irc.to_lower(key)
def __getitem__(self, key): def __getitem__(self, key):
key = self._keymangle(key) key = self._keymangle(key)
@ -60,27 +51,6 @@ class ChannelState(collections.abc.MutableMapping):
return self._data[key] return self._data[key]
def __setitem__(self, key, value):
self._data[self._keymangle(key)] = value
def __delitem__(self, key):
del self._data[self._keymangle(key)]
def __iter__(self):
return iter(self._data)
def __len__(self):
return len(self._data)
def copy(self, *args, **kwargs):
return self._data.copy(*args, **kwargs)
def __contains__(self, key):
return self._data.__contains__(self._keymangle(key))
def __repr__(self):
return "ChannelState(%s)" % self._data.keys()
class PyLinkNetworkCore(utils.DeprecatedAttributesObject, utils.CamelCaseToSnakeCase): class PyLinkNetworkCore(utils.DeprecatedAttributesObject, utils.CamelCaseToSnakeCase):
"""Base IRC object for PyLink.""" """Base IRC object for PyLink."""

View File

@ -25,6 +25,55 @@ class KeyedDefaultdict(collections.defaultdict):
value = self[key] = self.default_factory(key) value = self[key] = self.default_factory(key)
return value return value
class CaseInsensitiveDict(collections.abc.MutableMapping):
"""
A dictionary storing items case insensitively.
"""
def __init__(self):
self._data = {}
def _keymangle(self, key):
"""Converts the given key to lowercase."""
return key.lower()
def __getitem__(self, key):
key = self._keymangle(key)
return self._data[key]
def __setitem__(self, key, value):
self._data[self._keymangle(key)] = value
def __delitem__(self, key):
del self._data[self._keymangle(key)]
def __iter__(self):
return iter(self._data)
def __len__(self):
return len(self._data)
def copy(self, *args, **kwargs):
return self._data.copy(*args, **kwargs)
def __contains__(self, key):
return self._data.__contains__(self._keymangle(key))
def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, self._data)
class IRCCaseInsensitiveDict(CaseInsensitiveDict):
"""
A dictionary storing channels case insensitively. Channel objects are initialized on access.
"""
def __init__(self, irc):
super().__init__()
self._irc = irc
def _keymangle(self, key):
"""Converts the given key to lowercase."""
return self._irc.to_lower(key)
class DataStore: class DataStore:
""" """
Generic database class. Plugins should use a subclass of this such as JSONDataStore or Generic database class. Plugins should use a subclass of this such as JSONDataStore or