-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghirc
executable file
·65 lines (53 loc) · 1.55 KB
/
ghirc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
import argparse
import asyncio
import sys
import tomli
import uvicorn
import uvloop
from src import app
def main():
parser = argparse.ArgumentParser(
prog="ghirc",
description="IRC bot for GitHub webhooks",
epilog="Contribute code and bug reports to "
"https://github.com/tfaughnan/ghirc",
)
parser.add_argument(
"-c",
"--config",
default="~/.config/ghirc.toml",
help="Specify config file (default: %(default)s)",
)
args = parser.parse_args()
config = load_config(args.config)
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
uvicorn.run(
app=app.App(config),
host=config["uvicorn"]["host"],
port=config["uvicorn"]["port"],
loop="uvloop",
)
def load_config(path="~/.config/ghirc.toml"):
try:
with open(path, "rb") as f:
try:
config = tomli.load(f)
except tomli.TOMLDecodeError:
sys.exit(f"{path} is not valid toml.")
except FileNotFoundError:
sys.exit("No config file found.")
# ensure we are not missing any fields
fields = {
"uvicorn": ("host", "port"),
"github": ("secret", "events"),
"irc": ("host", "port", "ssl", "nickname", "realname", "channels"),
}
if any(
any(filter(lambda f: f not in config[section], fields[section]))
for section in fields
):
sys.exit("Missing required fields in config file.")
return config
if __name__ == "__main__":
main()