Skip to content

Commit 5f0a9f4

Browse files
René SamselnigRené Samselnig
authored andcommitted
Chg: Changes content in rtmbot to allow using its slack_client.
1 parent 22fb111 commit 5f0a9f4

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

rtmbot.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414

1515
from slackclient import SlackClient
1616

17-
def dbg(debug_string):
18-
if debug:
19-
logging.info(debug_string)
17+
logger = logging.getLogger(__name__)
2018

2119
class RtmBot(object):
2220
def __init__(self, token):
@@ -47,7 +45,7 @@ def autoping(self):
4745
def input(self, data):
4846
if "type" in data:
4947
function_name = "process_" + data["type"]
50-
dbg("got {}".format(function_name))
48+
logger.debug("got {}".format(function_name))
5149
for plugin in self.bot_plugins:
5250
plugin.register_jobs()
5351
plugin.do(function_name, data)
@@ -71,30 +69,31 @@ def load_plugins(self):
7169
sys.path.insert(0, plugin)
7270
sys.path.insert(0, directory+'/plugins/')
7371
for plugin in glob.glob(directory+'/plugins/*.py') + glob.glob(directory+'/plugins/*/*.py'):
74-
logging.info(plugin)
72+
logger.info('Plugin: %s', plugin)
7573
name = plugin.split('/')[-1][:-3]
7674
# try:
77-
self.bot_plugins.append(Plugin(name))
75+
self.bot_plugins.append(Plugin(name, bot))
7876
# except:
7977
# print "error loading plugin %s" % name
8078

8179
class Plugin(object):
82-
def __init__(self, name, plugin_config={}):
80+
def __init__(self, name, bot, plugin_config={}):
8381
self.name = name
8482
self.jobs = []
8583
self.module = __import__(name)
8684
self.register_jobs()
8785
self.outputs = []
8886
if name in config:
89-
logging.info("config found for: " + name)
87+
logger.info("config found for: " + name)
9088
self.module.config = config[name]
89+
self.module.bot = bot
9190
if 'setup' in dir(self.module):
9291
self.module.setup()
9392
def register_jobs(self):
9493
if 'crontable' in dir(self.module):
9594
for interval, function in self.module.crontable:
9695
self.jobs.append(Job(interval, eval("self.module."+function)))
97-
logging.info(self.module.crontable)
96+
logger.debug('Crontable: %s', self.module.crontable)
9897
self.module.crontable = []
9998
else:
10099
self.module.crontable = []
@@ -105,14 +104,14 @@ def do(self, function_name, data):
105104
try:
106105
eval("self.module."+function_name)(data)
107106
except:
108-
dbg("problem in module {} {}".format(function_name, data))
107+
logger.warn("problem in module {} {}".format(function_name, data))
109108
else:
110109
eval("self.module."+function_name)(data)
111110
if "catch_all" in dir(self.module):
112111
try:
113112
self.module.catch_all(data)
114113
except:
115-
dbg("problem in catch all")
114+
logger.error("problem in catch all")
116115
def do_jobs(self):
117116
for job in self.jobs:
118117
job.check()
@@ -121,7 +120,7 @@ def do_output(self):
121120
while True:
122121
if 'outputs' in dir(self.module):
123122
if len(self.module.outputs) > 0:
124-
logging.info("output from {}".format(self.module))
123+
logger.debug("output from {}".format(self.module))
125124
output.append(self.module.outputs.pop(0))
126125
else:
127126
break
@@ -144,7 +143,7 @@ def check(self):
144143
try:
145144
self.function()
146145
except:
147-
dbg("problem")
146+
logger.error("problem")
148147
else:
149148
self.function()
150149
self.lastrun = time.time()
@@ -156,14 +155,20 @@ class UnknownChannel(Exception):
156155

157156
def main_loop():
158157
if "LOGFILE" in config:
159-
logging.basicConfig(filename=config["LOGFILE"], level=logging.INFO, format='%(asctime)s %(message)s')
160-
logging.info(directory)
158+
logging.basicConfig(
159+
filename=config["LOGFILE"],
160+
level=logging.INFO,
161+
format='%(asctime)s %(levelname)s %(name)s: %(message)s'
162+
)
163+
logger.info(directory)
161164
try:
162165
bot.start()
163166
except KeyboardInterrupt:
164167
sys.exit(0)
168+
except SystemExit:
169+
pass
165170
except:
166-
logging.exception('OOPS')
171+
logger.exception('OOPS')
167172

168173

169174
def parse_args():

0 commit comments

Comments
 (0)