diff --git a/gui.py b/gui.py index fa22f97..762753e 100644 --- a/gui.py +++ b/gui.py @@ -775,7 +775,7 @@ def __init__(self, **kwargs): def run(self): chromectrl.Initialize() - url = ':'.join([self.url, self.port]) + url = ':'.join([self.url, str(self.port)]) app = wx.App(False) # Create a new app, don't redirect stdout/stderr to a window. self.gui = ChatGui(None, "LalkaChat", url, **self.kwargs) # A Frame is a top-level window. app.MainLoop() diff --git a/main.py b/main.py index 0dd03f4..e36c9db 100644 --- a/main.py +++ b/main.py @@ -25,7 +25,6 @@ CONF_FOLDER = os.path.join(PYTHON_FOLDER, "conf") MODULE_FOLDER = os.path.join(PYTHON_FOLDER, "modules") MAIN_CONF_FILE = os.path.join(CONF_FOLDER, "config.cfg") -HTTP_FOLDER = os.path.join(PYTHON_FOLDER, "http") GUI_TAG = 'gui' LOG_FOLDER = os.path.join(PYTHON_FOLDER, "logs") @@ -40,21 +39,6 @@ 'ru_RU': 'ru' } -root_logger = logging.getLogger() -# Logging level -root_logger.setLevel(level=logging.INFO) -file_handler = logging.FileHandler(LOG_FILE) -file_handler.setFormatter(LOG_FORMAT) -root_logger.addHandler(file_handler) - -console_handler = logging.StreamHandler() -console_handler.setFormatter(LOG_FORMAT) -root_logger.addHandler(console_handler) -logging.getLogger('requests').setLevel(logging.ERROR) - -log = logging.getLogger('main') - - def get_update(): github_url = "https://api.github.com/repos/DeForce/LalkaChat/releases" try: @@ -122,20 +106,15 @@ def close(): main_config_dict['gui']['gui'] = True main_config_dict['gui']['on_top'] = True main_config_dict['gui']['reload'] = None - main_config_dict['style'] = 'czt' main_config_dict['language'] = get_language() main_config_gui = { - 'style': { - 'check': 'http', - 'check_type': 'dir', - 'view': 'choose_single'}, 'language': { 'view': 'choose_single', 'check_type': 'dir', 'check': 'translations' }, - 'non_dynamic': ['style.list_box', 'language.list_box', 'gui.*'] + 'non_dynamic': ['language.list_box', 'gui.*'] } config = self_heal(MAIN_CONF_FILE, main_config_dict) # Adding config for main module @@ -155,19 +134,6 @@ def close(): gui_settings['size'] = (main_config_dict['gui_information'].get('width'), main_config_dict['gui_information'].get('height')) - # Fallback if style folder not found - fallback_style = 'czt' - if len(config.items('style')) > 0: - style, null_element = config.items('style')[0] - path = os.path.abspath(os.path.join(HTTP_FOLDER, style)) - if os.path.exists(path): - gui_settings['style'] = style - else: - gui_settings['style'] = fallback_style - else: - gui_settings['style'] = fallback_style - loaded_modules['main']['http_folder'] = os.path.join(HTTP_FOLDER, gui_settings['style']) - # Checking updates log.info("Checking for updates") loaded_modules['main']['update'], loaded_modules['main']['update_url'] = get_update() @@ -221,10 +187,14 @@ def close(): tmp = imp.load_source(module, module_location) chat_init = getattr(tmp, module) class_module = chat_init(queue, PYTHON_FOLDER) - loaded_modules[module] = class_module.conf_params - loaded_modules[module]['class'] = class_module + loaded_modules[module] = class_module.conf_params() else: log.error("Unable to find {0} module") + + # Actually loading modules + for f_module, f_config in loaded_modules.iteritems(): + if 'class' in f_config: + f_config['class'].load_module(main_settings=main_config, loaded_modules=loaded_modules) try: load_translations_keys(TRANSLATION_FOLDER, gui_settings['language']) except: @@ -253,4 +223,17 @@ def close(): log.info(exc) if __name__ == '__main__': + root_logger = logging.getLogger() + # Logging level + root_logger.setLevel(level=logging.INFO) + file_handler = logging.FileHandler(LOG_FILE) + file_handler.setFormatter(LOG_FORMAT) + root_logger.addHandler(file_handler) + + console_handler = logging.StreamHandler() + console_handler.setFormatter(LOG_FORMAT) + root_logger.addHandler(console_handler) + logging.getLogger('requests').setLevel(logging.ERROR) + + log = logging.getLogger('main') init() diff --git a/messaging.py b/messaging.py index e48de3b..f1cc88b 100644 --- a/messaging.py +++ b/messaging.py @@ -94,6 +94,7 @@ def load_modules(self, main_config, settings): for sorted_priority, sorted_list in sorted_module: for sorted_list_item in sorted_list: self.modules.append(sorted_list_item) + return modules_list def msg_process(self, message): diff --git a/modules/chat/goodgame.py b/modules/chat/goodgame.py index c2d0ade..270aa0d 100644 --- a/modules/chat/goodgame.py +++ b/modules/chat/goodgame.py @@ -224,18 +224,16 @@ def __init__(self, queue, python_folder, **kwargs): log.info("Initializing goodgame chat") conf_file = os.path.join(conf_folder, "goodgame.cfg") config = self_heal(conf_file, CONF_DICT) - self.conf_params = {'folder': conf_folder, 'file': conf_file, - 'filename': ''.join(os.path.basename(conf_file).split('.')[:-1]), - 'parser': config, - 'config': CONF_DICT, - 'gui': CONF_GUI} - - # Checking config file for needed variables - conf_tag = 'config' - address = config.get(conf_tag, 'socket') - channel_name = config.get(conf_tag, 'channel_name') - # ch_id + self._conf_params = {'folder': conf_folder, 'file': conf_file, + 'filename': ''.join(os.path.basename(conf_file).split('.')[:-1]), + 'parser': config, + 'config': CONF_DICT, + 'gui': CONF_GUI} + self.queue = queue + self.host = CONF_DICT['config']['socket'] + self.channel_name = CONF_DICT['config']['channel_name'] + def load_module(self, *args, **kwargs): # Creating new thread with queue in place for messaging transfers - gg = GGThread(queue, address, channel_name) + gg = GGThread(self.queue, self.host, self.channel_name) gg.start() diff --git a/modules/chat/sc2tv.py b/modules/chat/sc2tv.py index baf8b1d..0d75f76 100644 --- a/modules/chat/sc2tv.py +++ b/modules/chat/sc2tv.py @@ -260,20 +260,16 @@ def __init__(self, queue, python_folder, **kwargs): conf_folder = os.path.join(python_folder, "conf") conf_file = os.path.join(conf_folder, "sc2tv.cfg") config = self_heal(conf_file, CONF_DICT) - self.conf_params = {'folder': conf_folder, 'file': conf_file, - 'filename': ''.join(os.path.basename(conf_file).split('.')[:-1]), - 'parser': config, - 'config': CONF_DICT, - 'gui': CONF_GUI} - # Checking config file for needed variables - config_tag = 'config' - socket = config.get(config_tag, 'socket') - channel_name = config.get(config_tag, 'channel_name') - - # If any of the value are non-existent then exit the programm with error. - if (socket is None) or (channel_name is None): - log.critical("Config for funstream is not correct!") - - # Creating new thread with queue in place for messaging tranfers - fs = FsThread(queue, socket, channel_name) + self._conf_params = {'folder': conf_folder, 'file': conf_file, + 'filename': ''.join(os.path.basename(conf_file).split('.')[:-1]), + 'parser': config, + 'config': CONF_DICT, + 'gui': CONF_GUI} + self.queue = queue + self.socket = CONF_DICT['config']['socket'] + self.channel_name = CONF_DICT['config']['channel_name'] + + def load_module(self, *args, **kwargs): + # Creating new thread with queue in place for messaging transfers + fs = FsThread(self.queue, self.socket, self.channel_name) fs.start() diff --git a/modules/chat/twitch.py b/modules/chat/twitch.py index 61c875d..90eca23 100644 --- a/modules/chat/twitch.py +++ b/modules/chat/twitch.py @@ -324,20 +324,19 @@ def __init__(self, queue, python_folder, **kwargs): conf_file = os.path.join(conf_folder, "twitch.cfg") config = self_heal(conf_file, CONF_DICT) - self.conf_params = {'folder': conf_folder, 'file': conf_file, - 'filename': ''.join(os.path.basename(conf_file).split('.')[:-1]), - 'parser': config, - 'config': CONF_DICT, - 'gui': CONF_GUI} - - config.read(conf_file) - # Checking config file for needed variables - config_tag = 'config' - host = config.get(config_tag, 'host') - port = int(config.get(config_tag, 'port')) - channel = config.get(config_tag, 'channel') - bttv_smiles = config.get(config_tag, 'bttv') + self._conf_params = {'folder': conf_folder, 'file': conf_file, + 'filename': ''.join(os.path.basename(conf_file).split('.')[:-1]), + 'parser': config, + 'config': CONF_DICT, + 'gui': CONF_GUI} + self.queue = queue + self.host = CONF_DICT['config']['host'] + self.port = CONF_DICT['config']['port'] + self.channel = CONF_DICT['config']['channel'] + self.bttv = CONF_DICT['config']['bttv'] + + def load_module(self, *args, **kwargs): # Creating new thread with queue in place for messaging transfers - tw = twThread(queue, host, port, channel, bttv_smiles) + tw = twThread(self.queue, self.host, self.port, self.channel, self.bttv) tw.start() diff --git a/modules/helper/modules.py b/modules/helper/modules.py index 5bac842..8aaeca5 100644 --- a/modules/helper/modules.py +++ b/modules/helper/modules.py @@ -9,6 +9,9 @@ def conf_params(self): params['class'] = self return params + def load_module(self, *args, **kwargs): + pass + def gui_button_press(self, *args): pass diff --git a/modules/messaging/levels.py b/modules/messaging/levels.py index 23a1d18..dd3972c 100644 --- a/modules/messaging/levels.py +++ b/modules/messaging/levels.py @@ -30,8 +30,6 @@ def create_db(db_location): def __init__(self, conf_folder, **kwargs): MessagingModule.__init__(self) - # Creating filter and replace strings. - main_settings = kwargs.get('main_settings') conf_file = os.path.join(conf_folder, "levels.cfg") conf_dict = OrderedDict() @@ -56,11 +54,32 @@ def __init__(self, conf_folder, **kwargs): 'config': conf_dict, 'gui': conf_gui} + self.conf_folder = None + self.experience = None + self.exp_for_level = None + self.exp_for_message = None + self.filename = None + self.levels = None + self.special_levels = None + self.db_location = None + self.message = None + self.decrease_window = None + self.threshold_users = None + + def load_module(self, *args, **kwargs): + main_settings = kwargs.get('main_settings') + loaded_modules = kwargs.get('loaded_modules') + if 'webchat' not in loaded_modules: + raise ModuleLoadException("Unable to find webchat module that is needed for level module") + + conf_folder = self._conf_params['folder'] + conf_dict = self._conf_params['config'] + self.conf_folder = conf_folder self.experience = conf_dict['config'].get('experience') self.exp_for_level = float(conf_dict['config'].get('exp_for_level')) self.exp_for_message = float(conf_dict['config'].get('exp_for_message')) - self.filename = os.path.abspath(os.path.join(main_settings['http_folder'], 'levels.xml')) + self.filename = os.path.abspath(os.path.join(loaded_modules['webchat']['style_location'], 'levels.xml')) self.levels = [] self.special_levels = {} self.db_location = os.path.join(conf_dict['config'].get('db')) diff --git a/modules/messaging/webchat.py b/modules/messaging/webchat.py index aa50a68..d1ae358 100644 --- a/modules/messaging/webchat.py +++ b/modules/messaging/webchat.py @@ -15,9 +15,11 @@ from modules.helper.system import THREADS from modules.helper.modules import MessagingModule from gui import MODULE_KEY +from main import PYTHON_FOLDER, CONF_FOLDER DEFAULT_PRIORITY = 9001 HISTORY_SIZE = 20 +HTTP_FOLDER = os.path.join(PYTHON_FOLDER, "http") s_queue = Queue.Queue() logging.getLogger('ws4py').setLevel(logging.ERROR) log = logging.getLogger('webchat') @@ -177,14 +179,22 @@ def __init__(self, conf_folder, **kwargs): conf_dict['server'] = OrderedDict() conf_dict['server']['host'] = '127.0.0.1' conf_dict['server']['port'] = '8080' - conf_gui = {'non_dynamic': ['server.*']} + conf_dict['style'] = 'czt' + conf_gui = { + 'style': { + 'check': 'http', + 'check_type': 'dir', + 'view': 'choose_single'}, + 'non_dynamic': ['server.*']} config = self_heal(conf_file, conf_dict) - tag_server = 'server' - host = config.get(tag_server, 'host') - port = config.get(tag_server, 'port') - style = main_settings['http_folder'] + fallback_style = 'czt' + path = os.path.abspath(os.path.join(HTTP_FOLDER, conf_dict['style'])) + if os.path.exists(path): + style_location = path + else: + style_location = os.path.join(HTTP_FOLDER, fallback_style) self._conf_params = {'folder': conf_folder, 'file': conf_file, 'filename': ''.join(os.path.basename(conf_file).split('.')[:-1]), @@ -192,12 +202,21 @@ def __init__(self, conf_folder, **kwargs): 'id': config.get('gui_information', 'id'), 'config': conf_dict, 'gui': conf_gui, - 'port': port} + 'host': conf_dict['server']['host'], + 'port': conf_dict['server']['port'], + 'style_location': style_location} + + self.message_threads = [] + + def load_module(self, *args, **kwargs): + conf_dict = self._conf_params + host = conf_dict['host'] + port = conf_dict['port'] + if socket_open(host, port): - s_thread = SocketThread(host, port, conf_folder, style=style) + s_thread = SocketThread(host, port, CONF_FOLDER, style=self._conf_params['style_location']) s_thread.start() - self.message_threads = [] for thread in range(THREADS+5): self.message_threads.append(MessagingThread()) self.message_threads[thread].start()