Skip to content

Commit

Permalink
Merge pull request #127 from DeForce/LC-126
Browse files Browse the repository at this point in the history
LC-126 Rewrite messaging module initialization
  • Loading branch information
DeForce authored Oct 30, 2016
2 parents 6f7bfef + f3ff31a commit 4418db2
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 91 deletions.
2 changes: 1 addition & 1 deletion gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
57 changes: 20 additions & 37 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
1 change: 1 addition & 0 deletions messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
22 changes: 10 additions & 12 deletions modules/chat/goodgame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
28 changes: 12 additions & 16 deletions modules/chat/sc2tv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
27 changes: 13 additions & 14 deletions modules/chat/twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
3 changes: 3 additions & 0 deletions modules/helper/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 22 additions & 3 deletions modules/messaging/levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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'))
Expand Down
35 changes: 27 additions & 8 deletions modules/messaging/webchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -177,27 +179,44 @@ 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]),
'parser': config,
'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()
Expand Down

0 comments on commit 4418db2

Please sign in to comment.