running pycharm code instpector and pycharm reformat code

This commit is contained in:
Quentin Nerden 2015-08-10 12:00:00 +02:00 committed by Jeff Ammons
parent 4441a56011
commit 05ca843d31

View file

@ -1,11 +1,11 @@
#!/usr/bin/env python
import sys
sys.dont_write_bytecode = True
import glob
import yaml
import json
import os
import sys
import time
@ -14,20 +14,24 @@ from argparse import ArgumentParser
from slackclient import SlackClient
def dbg(debug_string):
if debug:
logging.info(debug_string)
class RtmBot(object):
def __init__(self, token):
self.last_ping = 0
self.token = token
self.bot_plugins = []
self.slack_client = None
def connect(self):
"""Convenience method that creates Server instance"""
self.slack_client = SlackClient(self.token)
self.slack_client.rtm_connect()
def start(self):
self.connect()
self.load_plugins()
@ -38,12 +42,14 @@ class RtmBot(object):
self.output()
self.autoping()
time.sleep(.1)
def autoping(self):
# hardcode the interval to 3 seconds
now = int(time.time())
if now > self.last_ping + 3:
self.slack_client.server.ping()
self.last_ping = now
def input(self, data):
if "type" in data:
function_name = "process_" + data["type"]
@ -51,21 +57,25 @@ class RtmBot(object):
for plugin in self.bot_plugins:
plugin.register_jobs()
plugin.do(function_name, data)
def output(self):
for plugin in self.bot_plugins:
limiter = False
for output in plugin.do_output():
channel = self.slack_client.server.channels.find(output[0])
if channel != None and output[1] != None:
if limiter == True:
if channel is not None and output[1] is not None:
if limiter:
time.sleep(.1)
limiter = False
message = output[1].encode('ascii','ignore')
channel.send_message("{}".format(message))
limiter = True
limiter = True # TODO: check goal: no sleep for 1st channel, sleep of all after ?
# TODO: find out how to safely encode stuff if needed :(
# message = output[1].encode('utf-8','ignore')
channel.send_message(output[1]) # message
def crons(self):
for plugin in self.bot_plugins:
plugin.do_jobs()
def load_plugins(self):
for plugin in glob.glob(directory + '/plugins/*'):
sys.path.insert(0, plugin)
@ -79,7 +89,7 @@ class RtmBot(object):
# print "error loading plugin %s" % name
class Plugin(object):
def __init__(self, name, plugin_config={}):
def __init__(self, name):
self.name = name
self.jobs = []
self.module = __import__(name)
@ -90,6 +100,7 @@ class Plugin(object):
self.module.config = config[name]
if 'setup' in dir(self.module):
self.module.setup()
def register_jobs(self):
if 'crontable' in dir(self.module):
for interval, function in self.module.crontable:
@ -98,24 +109,27 @@ class Plugin(object):
self.module.crontable = []
else:
self.module.crontable = []
def do(self, function_name, data):
if function_name in dir(self.module):
# this makes the plugin fail with stack trace in debug mode
if not debug:
try:
eval("self.module."+function_name)(data)
eval("self.module." + function_name)(data, bot, config)
except:
dbg("problem in module {} {}".format(function_name, data))
else:
eval("self.module."+function_name)(data)
eval("self.module." + function_name)(data, bot, config)
if "catch_all" in dir(self.module):
try:
self.module.catch_all(data)
except:
dbg("problem in catch all")
def do_jobs(self):
for job in self.jobs:
job.check()
def do_output(self):
output = []
while True:
@ -129,15 +143,19 @@ class Plugin(object):
self.module.outputs = []
return output
class Job(object):
def __init__(self, interval, function):
self.function = function
self.interval = interval
self.lastrun = 0
def __str__(self):
return "{} {} {}".format(self.function, self.interval, self.lastrun)
def __repr__(self):
return self.__str__()
def check(self):
if self.lastrun + self.interval < time.time():
if not debug:
@ -150,6 +168,7 @@ class Job(object):
self.lastrun = time.time()
pass
class UnknownChannel(Exception):
pass
@ -195,7 +214,7 @@ if __name__ == "__main__":
if config.has_key("DAEMON"):
if config["DAEMON"]:
import daemon
with daemon.DaemonContext():
main_loop()
main_loop()