move to using config.py parser stuff

This commit is contained in:
Ben Harris 2022-05-09 15:34:00 -04:00
parent 195e9015a8
commit 570dba09a6
7 changed files with 77 additions and 30 deletions

6
.gitignore vendored
View File

@ -58,9 +58,5 @@ docs/_build/
# PyBuilder
target/
tildeclub.json
tildeteam.json
tildeverse.json
config.json
venv/
account.json
config.yaml

View File

@ -1,4 +0,0 @@
{
"username": "bensbots",
"password": "my super secret password"
}

16
config.example.yaml Normal file
View File

@ -0,0 +1,16 @@
server: localhost:6667
nickname: tooter
channel: "#meta"
sasl:
username: cloaks
password: hunter3
mastodon:
- name: tildeverse
client_id: 1234566
client_secret: beeeep
access_token: you do not want to know
base_url: https://tilde.zone
channels:
- '#club'

View File

@ -1,7 +0,0 @@
{
"channels": [],
"address": "127.0.0.1",
"port": 6667,
"tls": false,
"botnick": "tooter"
}

43
config.py Normal file
View File

@ -0,0 +1,43 @@
from dataclasses import dataclass
from os.path import expanduser
from re import compile as re_compile
from typing import List, Pattern, Tuple
import yaml
@dataclass
class Config(object):
server: Tuple[str, int, bool]
nickname: str
username: str
realname: str
password: str
channel: str
sasl: Tuple[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 = False
if port_s.startswith("+"):
tls = True
port_s = port_s.lstrip("+")
port = int(port_s)
return Config(
(hostname, port, tls),
nickname,
config_yaml.get("username", nickname),
config_yaml.get("realname", nickname),
config_yaml["password"],
config_yaml["channel"],
(config_yaml["sasl"]["username"], config_yaml["sasl"]["password"]),
)

View File

@ -1,7 +0,0 @@
{
"client_id": "myclientid",
"client_secret": "myclientsecret",
"access_token": "myaccesstoken",
"base_url": "https://tilde.zone",
"channel": "remove this key if you don't want to limit the bot"
}

View File

@ -1,10 +1,11 @@
#!/usr/bin/env python3
from mastodon import Mastodon
from irctokens import build, Line
from .config import Config
from ircrobots import Bot as BaseBot
from ircrobots import Server as BaseServer
from ircrobots import ConnectionParams, SASLUserPass
from ircrobots import Server as BaseServer
from irctokens import build, Line
from mastodon import Mastodon
import asyncio
import emoji
import glob
@ -68,6 +69,14 @@ def think(line):
class Server(BaseServer):
def __init__(self,
bot: BaseBot,
name: str,
config: Config):
super().__init__(bot, name)
self._config = config
async def line_send(self, line: Line):
print(f"{self.name} > {line.format()}")
@ -102,12 +111,13 @@ class Bot(BaseBot):
async def main():
params = ConnectionParams(
config["botnick"],
host=config["address"],
port=config["port"],
tls=config["tls"],
config.get("botnick", "tooter"),
config.get("address", "127.0.0.1"),
config.get("port", 6667),
config.get("tls", False),
sasl=SASLUserPass(account["username"], account["password"]),
)
bot = Bot()
await bot.add_server("tilde", params)
await bot.run()