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