[misc] Add some type annotations

This commit is contained in:
Thomas Lindner 2023-03-19 01:15:37 +01:00
parent 13c20ca245
commit 767c92000b

View file

@ -29,7 +29,7 @@ class Message:
**kwargs (object, optional): Other platform-specific data.
"""
def __init__(self, text, **kwargs):
def __init__(self, text: str, **kwargs):
self.text = text
self.__dict__.update(kwargs)
@ -82,17 +82,17 @@ class Censor:
self.__hood_censors.append(self)
self.status = BotStatus.INSTANTIATED
def start(self):
def start(self) -> None:
"""Start the bot."""
if self.__task is None:
self.__task = create_task(self.__run())
def stop(self):
def stop(self) -> None:
"""Stop the bot."""
if self.__task is not None:
self.__task.cancel()
async def __run(self):
async def __run(self) -> None:
await self.hood.load()
self.__task.set_name("{0} {1}".format(self.__class__.__name__, self.hood.name))
try:
@ -104,7 +104,7 @@ class Censor:
self.__task = None
self.status = BotStatus.STOPPED
async def run(self):
async def run(self) -> None:
"""Entry point for a bot.
Note: Override this in the derived bot class.
@ -112,14 +112,14 @@ class Censor:
pass
@classmethod
async def destroy_hood(cls, hood):
async def destroy_hood(cls, hood) -> None:
"""Remove all of its database entries.
Note: Override this in the derived bot class.
"""
pass
async def publish(self, message):
async def publish(self, message: Message) -> bool:
"""Distribute a message to the bots in a hood.
Args:
@ -132,14 +132,14 @@ class Censor:
await censor._inbox.put(message)
return True
async def receive(self):
async def receive(self) -> Message:
"""Receive a message.
Returns (Message): Received message
"""
return await self._inbox.get()
async def __is_appropriate(self, message):
async def __is_appropriate(self, message: Message) -> bool:
for badword in await BadWord.objects.filter(hood=self.hood).all():
if search(badword.pattern, message.text, IGNORECASE):
logger.debug(
@ -186,13 +186,13 @@ class Spawner:
self.__instances.append(self)
@classmethod
async def init_all(cls):
async def init_all(cls) -> None:
"""Instantiate and start a bot for every row in the corresponding ORM model."""
for spawner in cls.__instances:
await spawner._init()
@classmethod
async def destroy_hood(cls, hood):
async def destroy_hood(cls, hood) -> None:
for spawner in cls.__instances:
for pk in list(spawner.__bots):
bot = spawner.__bots[pk]
@ -201,11 +201,11 @@ class Spawner:
bot.stop()
await spawner.BotClass.destroy_hood(hood)
async def _init(self):
async def _init(self) -> None:
for item in await self.ORMClass.objects.all():
self.start(item)
def start(self, item):
def start(self, item) -> None:
"""Instantiate and start a bot with the provided ORM object.
Example:
@ -221,7 +221,7 @@ class Spawner:
if bot.enabled:
bot.start()
def stop(self, item):
def stop(self, item) -> None:
"""Stop and delete a bot.
Args:
@ -231,7 +231,7 @@ class Spawner:
if bot is not None:
bot.stop()
def get(self, item):
def get(self, item) -> Censor:
"""Get a running bot.
Args: