commit c0c04fffbc06a697c45dd13ba7f204d63ccd0fe0
parent 8f08e661f46a69042207f58f04c974e2c3a82268
Author: Marcel <mtrnord1@gmail.com>
Date: Sun, 29 Jan 2017 12:34:22 +0100
prepare command registering
Diffstat:
7 files changed, 67 insertions(+), 44 deletions(-)
diff --git a/bot.py b/bot.py
@@ -1,52 +1,19 @@
#Import external Modules that are needed
-import telegram.ext
-from telegram.ext import Updater, Filters
-from slackclient import SlackClient
import os.path, sys, traceback, shutil, logging, logging.config, argparse
from multiprocessing import Pool
logger = logging.getLogger(__name__)
#Import internal Modules that are needed
-from bot import config
from bot import Htelegram
from bot import Hslack
+from bot.helper import config_class
class Core:
- def __init__(self):
- try:
- self.config = config.Config(os.path.join('config', 'config.json'))
- except ValueError:
- logging.exception("failed to load config, malformed json")
- sys.exit()
-
- try:
- self.memory = config.Memory(os.path.join('config', 'memory.json'))
- except ValueError:
- logging.exception("failed to load config, malformed json")
- sys.exit()
-
- try:
- self.sl_api_key = self.config.get_by_path(['SLACK_API_KEY'])
- self.sc = SlackClient(self.sl_api_key)
- except Exception as e:
- traceback.print_exc()
-
- try:
- self.tg_api_key = self.config.get_by_path(['TELEGRAM_API_KEY'])
- if not self.memory.exists(['tg_sl-sync']):
- logger.warn('tg_sl-sync missing...')
- self.memory.set_by_path(['tg_sl-sync'], {'sl2tg':{}, 'tg2sl': {}})
- self.memory.save()
- self.tg_bot = telegram.Bot(self.tg_api_key)
- self.updater = Updater(self.tg_api_key)
- except Exception as e:
- traceback.print_exc()
-
def run(self):
pool = Pool(2)
- telegram_class = Htelegram.Telegram(self.sc, self.updater, self.memory)
- slack_class = Hslack.Slack(self.sc, self.tg_bot, self.memory)
+ telegram_class = Htelegram.Telegram()
+ slack_class = Hslack.Slack()
try:
Ttelegram = pool.apply_async(telegram_class.telegram_init(), [])
Tslack = pool.apply_async(slack_class.slack_init(), [])
@@ -113,9 +80,8 @@ def configure_logging(args):
# logging before bringing anything else up. There is no race internally,
# if logging() is called before configured, it outputs to stderr, and
# we will configure it soon enough
- bootcfg = config.Config(os.path.join('config', 'config.json'))
- if bootcfg.exists(["logging.system"]):
- logging_config = bootcfg["logging.system"]
+ if config_class.exists(["logging.system"]):
+ logging_config = config_class["logging.system"]
if "extras.setattr" in logging_config:
for class_attr, value in logging_config["extras.setattr"].items():
diff --git a/bot/Hcommand.py b/bot/Hcommand.py
@@ -1,2 +1,18 @@
+from functools import wraps
+
class Command:
- print("WIP")
+ def __init__():
+ print("WIP")
+
+ def register(count):
+ def true_register(f):
+ @wraps(f)
+ def wrapped(*args, **kwargs):
+ for i in range(count):
+ print "Before decorated function"
+ r = f(*args, **kwargs)
+ for i in range(count):
+ print "After decorated function"
+ return r
+ return wrapped
+ return true_register
diff --git a/bot/Hslack.py b/bot/Hslack.py
@@ -2,11 +2,13 @@ import logging, time
logger = logging.getLogger(__name__)
+from bot.helper import sc, tg_bot, memory_class
+
class Slack:
- def __init__(self, sc, tg_bot, memory):
+ def __init__(self):
self.sc = sc
self.tg_bot = tg_bot
- self.memory = memory
+ self.memory = memory_class
def slack_init(self):
try:
diff --git a/bot/Htelegram.py b/bot/Htelegram.py
@@ -4,11 +4,13 @@ from telegram.ext import Updater, Filters
logger = logging.getLogger(__name__)
+from bot.helper import sc, updater, memory_class
+
class Telegram:
- def __init__(self, sc, updater, memory):
+ def __init__(self):
self.sc = sc
self.updater = updater
- self.memory = memory
+ self.memory = memory_class
def Hsyc(self, bot, update):
try:
diff --git a/bot/__pycache__/Hslack.cpython-35.pyc b/bot/__pycache__/Hslack.cpython-35.pyc
Binary files differ.
diff --git a/bot/__pycache__/Htelegram.cpython-35.pyc b/bot/__pycache__/Htelegram.cpython-35.pyc
Binary files differ.
diff --git a/bot/helper.py b/bot/helper.py
@@ -0,0 +1,37 @@
+import logging, os.path, traceback
+import telegram.ext
+from telegram.ext import Updater, Filters
+from slackclient import SlackClient
+
+logger = logging.getLogger(__name__)
+
+from bot import config
+
+try:
+ config_class = config.Config(os.path.join('config', 'config.json'))
+except ValueError:
+ logging.exception("failed to load config, malformed json")
+ sys.exit()
+
+try:
+ memory_class = config.Memory(os.path.join('config', 'memory.json'))
+except ValueError:
+ logging.exception("failed to load config, malformed json")
+ sys.exit()
+
+try:
+ sl_api_key = config_class.get_by_path(['SLACK_API_KEY'])
+ sc = SlackClient(sl_api_key)
+except Exception as e:
+ traceback.print_exc()
+
+try:
+ tg_api_key = config_class.get_by_path(['TELEGRAM_API_KEY'])
+ if not memory_class.exists(['tg_sl-sync']):
+ logger.warn('tg_sl-sync missing...')
+ memory_class.set_by_path(['tg_sl-sync'], {'sl2tg':{}, 'tg2sl': {}})
+ memory_class.save()
+ tg_bot = telegram.Bot(tg_api_key)
+ updater = Updater(tg_api_key)
+except Exception as e:
+ traceback.print_exc()