Split out Channels in to IRCChannels.Channels, not IRCChannel.Channels

This commit is contained in:
jesopo 2018-11-11 14:53:16 +00:00
parent 0f53d2e828
commit a9b6086c70
3 changed files with 56 additions and 55 deletions

View file

@ -155,56 +155,3 @@ class Channel(IRCObject.Object):
def get_user_status(self, user: IRCUser.User) -> typing.Set:
return self.user_modes.get(user, [])
class Channels(object):
def __init__(self, server: "IRCServer.Server", bot: "IRCBot.Bot",
events: EventManager.EventHook):
self._server = server
self._bot = bot
self._events = events
self._channels = {} # type: typing.Dict[str, Channel]
def __iter__(self) -> typing.Iterable[Channel]:
return (channel for channel in self._channels.values())
def __contains__(self, name: str) -> bool:
return self.contains(name)
def __len__(self) -> int:
return len(self._channels)
def __getitem__(self, name: str):
return self.get(name)
def _get_id(self, channel_name: str) -> int:
self._bot.database.channels.add(self._server.id, channel_name)
return self._bot.database.channels.get_id(self._server.id, channel_name)
def _name_lower(self, channel_name: str) -> str:
return utils.irc.lower(self._server.case_mapping, channel_name)
def contains(self, name: str) -> bool:
lower = self._name_lower(name)
return name[0] in self._server.channel_types and lower in self._channels
def add(self, name: str) -> Channel:
id = self._get_id(name)
lower = self._name_lower(name)
new_channel = Channel(lower, id, self._server, self._bot)
self._channels[lower] = new_channel
self._events.on("new.channel").call(channel=new_channel, server=self)
return new_channel
def remove(self, channel: Channel):
lower = self._name_lower(channel.name)
del self._channels[lower]
def get(self, name: str):
return self._channels[self._name_lower(name)]
def rename(self, old_name, new_name):
old_lower = self._name_lower(old_name)
new_lower = self._name_lower(new_name)
channel = self.channels.pop(old_lower)
channel.name = new_name
self._channels[new_name] = channel
self._bot.database.channels.rename(channel.id, new_lower)

54
src/IRCChannels.py Normal file
View file

@ -0,0 +1,54 @@
from src import EventManager, IRCBot, IRCChannel, IRCServer
class Channels(object):
def __init__(self, server: "IRCServer.Server", bot: "IRCBot.Bot",
events: EventManager.EventHook):
self._server = server
self._bot = bot
self._events = events
self._channels = {} # type: typing.Dict[str, Channel]
def __iter__(self) -> typing.Iterable[Channel]:
return (channel for channel in self._channels.values())
def __contains__(self, name: str) -> bool:
return self.contains(name)
def __len__(self) -> int:
return len(self._channels)
def __getitem__(self, name: str):
return self.get(name)
def _get_id(self, channel_name: str) -> int:
self._bot.database.channels.add(self._server.id, channel_name)
return self._bot.database.channels.get_id(self._server.id, channel_name)
def _name_lower(self, channel_name: str) -> str:
return utils.irc.lower(self._server.case_mapping, channel_name)
def contains(self, name: str) -> bool:
lower = self._name_lower(name)
return name[0] in self._server.channel_types and lower in self._channels
def add(self, name: str) -> Channel:
id = self._get_id(name)
lower = self._name_lower(name)
new_channel = Channel(lower, id, self._server, self._bot)
self._channels[lower] = new_channel
self._events.on("new.channel").call(channel=new_channel, server=self)
return new_channel
def remove(self, channel: Channel):
lower = self._name_lower(channel.name)
del self._channels[lower]
def get(self, name: str):
return self._channels[self._name_lower(name)]
def rename(self, old_name, new_name):
old_lower = self._name_lower(old_name)
new_lower = self._name_lower(new_name)
channel = self.channels.pop(old_lower)
channel.name = new_name
self._channels[new_name] = channel
self._bot.database.channels.rename(channel.id, new_lower)

View file

@ -1,5 +1,5 @@
import collections, socket, ssl, sys, time, typing
from src import EventManager, IRCBot, IRCChannel, IRCObject, IRCUser, utils
from src import EventManager, IRCBot, IRCChannels, IRCObject, IRCUser, utils
THROTTLE_LINES = 4
THROTTLE_SECONDS = 1
@ -38,7 +38,7 @@ class Server(IRCObject.Object):
self.users = {} # type: typing.Dict[str, IRCUser.User]
self.new_users = set([]) #type: typing.Set[IRCUser.User]
self.channels = IRCChannel.Channels(self, self.bot, self.events)
self.channels = IRCChannels.Channels(self, self.bot, self.events)
self.own_modes = {} # type: typing.Dict[str, typing.Optional[str]]
self.isupport = {} # type: typing.Dict[str, typing.Optional[str]]