This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathrun_rtmbot.py
More file actions
executable file
·87 lines (69 loc) · 2.42 KB
/
run_rtmbot.py
File metadata and controls
executable file
·87 lines (69 loc) · 2.42 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
from argparse import ArgumentParser
import sys
import os
import yaml
import re
from rtmbot import RtmBot
sys.path.append(os.getcwd())
def parse_args():
parser = ArgumentParser()
parser.add_argument(
'-c',
'--config',
help='Full path to config file.',
metavar='path'
)
return parser.parse_args()
def prefix_from_plugin_name(name):
basename = name.split('.')[-1]
# Attribution: https://stackoverflow.com/a/1176023
splitted = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', basename)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', splitted).upper() + '_'
def load_overrides_from_env(config):
'''
Read and apply overrides from environment variables. For plugins, name of the class
is turned into a prefix. For example:
YourAwesomePlugin.foo_bar => YOUR_AWESOME_PLUGIN_FOO_BAR
'''
def boolish(v):
return v == '1' or v == 'true' # bool('false') == True, so do it the hard way
params = [
{'name': 'SLACK_TOKEN', 'type': str},
{'name': 'BASE_PATH', 'type': str},
{'name': 'LOGFILE', 'type': str},
{'name': 'DEBUG', 'type': boolish},
{'name': 'DAEMON', 'type': boolish}
]
# Override the rtmbot-specific variables. Here we can take advantage
# of the fact that we know what type they are supposed to be in.
config.update({
param['name']: param['type'](os.environ[param['name']])
for param in params
if param['name'] in os.environ})
# Override plugin-specific variables. Since we don't know a schema,
# treat values as string. Leave type conversion for plugins themselves.
for plugin in config.get('ACTIVE_PLUGINS', []):
prefix = prefix_from_plugin_name(plugin)
plugin_configs = [
var for var in os.environ
if var.startswith(prefix)]
# Create if necessary
if plugin not in config:
config[plugin] = {}
config[plugin].update({
param.split(prefix)[-1].lower(): os.environ[param]
for param in plugin_configs})
def main(args=None):
# load args with config path if not specified
if not args:
args = parse_args()
config = yaml.load(open(args.config or 'rtmbot.conf', 'r'))
load_overrides_from_env(config)
bot = RtmBot(config)
try:
bot.start()
except KeyboardInterrupt:
sys.exit(0)
if __name__ == "__main__":
main()