irc
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tooter/config.py

58 lines
1.5 KiB

from dataclasses import dataclass
from mastodon import Mastodon
from typing import Tuple, Dict
import yaml
@dataclass
class Config(object):
server: Tuple[str, int, bool]
nickname: str
username: str
realname: str
channel: str
sasl: Tuple[str, str]
mastodon_accounts: Dict[str, Mastodon]
assigned_channels: Dict[str, str]
def load(filepath: str):
with open(filepath) as file:
config_yaml = yaml.safe_load(file.read())
nickname = config_yaml["nickname"]
server = config_yaml["server"]
hostname, port_s = server.split(":", 1)
tls = None
if port_s.startswith("+"):
tls = True
port_s = port_s.lstrip("+")
port = int(port_s)
accounts = {}
channels = {}
for acct in config_yaml["mastodon"]:
accounts[acct["name"]] = Mastodon(
client_id=acct["client_id"],
client_secret=acct["client_secret"],
access_token=acct["access_token"],
api_base_url=acct["api_base_url"],
)
if "channels" in acct:
for channel in acct["channels"]:
channels[channel] = acct["name"]
return Config(
(hostname, port, tls),
nickname,
config_yaml.get("username", nickname),
config_yaml.get("realname", nickname),
config_yaml["channel"],
(config_yaml["sasl"]["username"], config_yaml["sasl"]["password"]),
mastodon_accounts=accounts,
assigned_channels=channels,
)