-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmake_bot.py
More file actions
34 lines (26 loc) · 875 Bytes
/
make_bot.py
File metadata and controls
34 lines (26 loc) · 875 Bytes
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
from jinja2 import Environment, PackageLoader, select_autoescape
import os
import yaml
from typing import Dict
from collections import defaultdict
CONFIG_DIR = "config"
CONFIG_FILE = "conf.yml"
def get_handlers(bot_config : Dict):
handlers = defaultdict(list)
for handler in bot_config["handlers"]:
h_type = handler["type"]
handlers[h_type].append(handler)
return handlers
env = Environment(
loader=PackageLoader(CONFIG_DIR, 'templates')
)
env.trim_blocks = True
env.lstrip_blocks = True
config_file = os.path.join(CONFIG_DIR, CONFIG_FILE)
with open(config_file) as f:
bot_config = yaml.safe_load(f)
handlers = get_handlers(bot_config)
template = env.get_template('bot_template.py.jinja')
t = template.render(cmd_handlers=handlers["cmd"], msg_handlers=handlers["msg"], **bot_config)
with open("bot.py", 'w') as f:
f.write(t)