simple_lib: Add type annotations.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
parent
01d5106e9a
commit
3b4867ad46
2 changed files with 22 additions and 22 deletions
|
@ -22,7 +22,6 @@ exclude = [
|
||||||
"zulip_bots/zulip_bots/bots",
|
"zulip_bots/zulip_bots/bots",
|
||||||
"zulip_bots/zulip_bots/bots_unmaintained",
|
"zulip_bots/zulip_bots/bots_unmaintained",
|
||||||
# Excluded out of laziness:
|
# Excluded out of laziness:
|
||||||
"zulip_bots/zulip_bots/simple_lib.py",
|
|
||||||
"zulip_bots/zulip_bots/tests/test_lib.py",
|
"zulip_bots/zulip_bots/tests/test_lib.py",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -1,61 +1,62 @@
|
||||||
import configparser
|
import configparser
|
||||||
import sys
|
import sys
|
||||||
|
from typing import IO, Any, Dict, Optional
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from zulip_bots.lib import BotIdentity
|
from zulip_bots.lib import BotIdentity
|
||||||
|
|
||||||
|
|
||||||
class SimpleStorage:
|
class SimpleStorage:
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
self.data = dict()
|
self.data: Dict[str, Any] = dict()
|
||||||
|
|
||||||
def contains(self, key):
|
def contains(self, key: str) -> bool:
|
||||||
return key in self.data
|
return key in self.data
|
||||||
|
|
||||||
def put(self, key, value):
|
def put(self, key: str, value: Any) -> None:
|
||||||
self.data[key] = value
|
self.data[key] = value
|
||||||
|
|
||||||
def get(self, key):
|
def get(self, key: str) -> Any:
|
||||||
return self.data[key]
|
return self.data[key]
|
||||||
|
|
||||||
|
|
||||||
class MockMessageServer:
|
class MockMessageServer:
|
||||||
# This class is needed for the incrementor bot, which
|
# This class is needed for the incrementor bot, which
|
||||||
# actually updates messages!
|
# actually updates messages!
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
self.message_id = 0
|
self.message_id = 0
|
||||||
self.messages = dict()
|
self.messages: Dict[int, Dict[str, Any]] = dict()
|
||||||
|
|
||||||
def send(self, message):
|
def send(self, message: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
self.message_id += 1
|
self.message_id += 1
|
||||||
message["id"] = self.message_id
|
message["id"] = self.message_id
|
||||||
self.messages[self.message_id] = message
|
self.messages[self.message_id] = message
|
||||||
return message
|
return message
|
||||||
|
|
||||||
def add_reaction(self, reaction_data):
|
def add_reaction(self, reaction_data: object) -> Dict[str, Any]:
|
||||||
return dict(result="success", msg="", uri=f"https://server/messages/{uuid4()}/reactions")
|
return dict(result="success", msg="", uri=f"https://server/messages/{uuid4()}/reactions")
|
||||||
|
|
||||||
def update(self, message):
|
def update(self, message: Dict[str, Any]) -> None:
|
||||||
self.messages[message["message_id"]] = message
|
self.messages[message["message_id"]] = message
|
||||||
|
|
||||||
def upload_file(self, file):
|
def upload_file(self, file: IO[Any]) -> Dict[str, Any]:
|
||||||
return dict(result="success", msg="", uri=f"https://server/user_uploads/{uuid4()}")
|
return dict(result="success", msg="", uri=f"https://server/user_uploads/{uuid4()}")
|
||||||
|
|
||||||
|
|
||||||
class TerminalBotHandler:
|
class TerminalBotHandler:
|
||||||
def __init__(self, bot_config_file, message_server):
|
def __init__(self, bot_config_file: Optional[str], message_server: MockMessageServer) -> None:
|
||||||
self.bot_config_file = bot_config_file
|
self.bot_config_file = bot_config_file
|
||||||
self._storage = SimpleStorage()
|
self._storage = SimpleStorage()
|
||||||
self.message_server = message_server
|
self.message_server = message_server
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def storage(self):
|
def storage(self) -> SimpleStorage:
|
||||||
return self._storage
|
return self._storage
|
||||||
|
|
||||||
def identity(self):
|
def identity(self) -> BotIdentity:
|
||||||
return BotIdentity("bot name", "bot-email@domain")
|
return BotIdentity("bot name", "bot-email@domain")
|
||||||
|
|
||||||
def react(self, message, emoji_name):
|
def react(self, message: Dict[str, Any], emoji_name: str) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Mock adding an emoji reaction and print it in the terminal.
|
Mock adding an emoji reaction and print it in the terminal.
|
||||||
"""
|
"""
|
||||||
|
@ -64,7 +65,7 @@ class TerminalBotHandler:
|
||||||
dict(message_id=message["id"], emoji_name=emoji_name, reaction_type="unicode_emoji")
|
dict(message_id=message["id"], emoji_name=emoji_name, reaction_type="unicode_emoji")
|
||||||
)
|
)
|
||||||
|
|
||||||
def send_message(self, message):
|
def send_message(self, message: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Print the message sent in the terminal and store it in a mock message server.
|
Print the message sent in the terminal and store it in a mock message server.
|
||||||
"""
|
"""
|
||||||
|
@ -90,7 +91,7 @@ class TerminalBotHandler:
|
||||||
# id to the message instead of actually displaying it.
|
# id to the message instead of actually displaying it.
|
||||||
return self.message_server.send(message)
|
return self.message_server.send(message)
|
||||||
|
|
||||||
def send_reply(self, message, response):
|
def send_reply(self, message: Dict[str, Any], response: str) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Print the reply message in the terminal and store it in a mock message server.
|
Print the reply message in the terminal and store it in a mock message server.
|
||||||
"""
|
"""
|
||||||
|
@ -102,7 +103,7 @@ class TerminalBotHandler:
|
||||||
response_message = dict(content=response)
|
response_message = dict(content=response)
|
||||||
return self.message_server.send(response_message)
|
return self.message_server.send(response_message)
|
||||||
|
|
||||||
def update_message(self, message):
|
def update_message(self, message: Dict[str, Any]) -> None:
|
||||||
"""
|
"""
|
||||||
Update a previously sent message and print the result in the terminal.
|
Update a previously sent message and print the result in the terminal.
|
||||||
Throw an IndexError if the message id is invalid.
|
Throw an IndexError if the message id is invalid.
|
||||||
|
@ -117,14 +118,14 @@ class TerminalBotHandler:
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def upload_file_from_path(self, file_path):
|
def upload_file_from_path(self, file_path: str) -> Dict[str, Any]:
|
||||||
with open(file_path) as file:
|
with open(file_path) as file:
|
||||||
return self.upload_file(file)
|
return self.upload_file(file)
|
||||||
|
|
||||||
def upload_file(self, file):
|
def upload_file(self, file: IO[Any]) -> Dict[str, Any]:
|
||||||
return self.message_server.upload_file(file)
|
return self.message_server.upload_file(file)
|
||||||
|
|
||||||
def get_config_info(self, bot_name, optional=False):
|
def get_config_info(self, bot_name: str, optional: bool = False) -> Dict[str, Any]:
|
||||||
if self.bot_config_file is None:
|
if self.bot_config_file is None:
|
||||||
if optional:
|
if optional:
|
||||||
return dict()
|
return dict()
|
||||||
|
|
Loading…
Add table
Reference in a new issue