forked from DeForce/LalkaChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messaging.py
118 lines (97 loc) · 4.62 KB
/
messaging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# This Python file uses the following encoding: utf-8
# -*- coding: utf-8 -*-
import os
import threading
import imp
import operator
import logging
from collections import OrderedDict
from modules.helper.system import ModuleLoadException, THREADS
from modules.helper.parser import self_heal
log = logging.getLogger('messaging')
MODULE_PRI_DEFAULT = '100'
class MessageHandler(threading.Thread):
def __init__(self, queue, process):
self.queue = queue
self.process = process
threading.Thread.__init__(self)
def run(self):
while True:
self.process(self.queue.get())
class Message(threading.Thread):
def __init__(self, queue):
super(self.__class__, self).__init__()
# Creating dict for dynamic modules
self.modules = []
self.daemon = True
self.msg_counter = 0
self.queue = queue
self.module_tag = "modules.messaging"
self.threads = []
def load_modules(self, main_config, settings):
log.info("Loading configuration file for messaging")
modules_list = OrderedDict()
conf_file = os.path.join(main_config['conf_folder'], "messaging_modules.cfg")
conf_dict = OrderedDict()
conf_dict['gui_information'] = {'category': 'messaging'}
conf_dict['messaging'] = {'webchat': None}
conf_gui = {
'messaging': {'check': 'modules/messaging',
'check_type': 'files',
'file_extension': False,
'view': 'choose_multiple',
'description': True},
'non_dynamic': ['messaging.*']}
config = self_heal(conf_file, conf_dict)
modules_list['messaging'] = {'folder': main_config['conf_folder'], 'file': conf_file,
'filename': ''.join(os.path.basename(conf_file).split('.')[:-1]),
'parser': config,
'config': conf_dict,
'gui': conf_gui}
modules = {}
# Loading modules from cfg.
if config.items("messaging") > 0:
for module, item in config.items("messaging"):
log.info("Loading %s" % module)
# We load the module, and then we initalize it.
# When writing your modules you should have class with the
# same name as module name
join_path = [main_config['root_folder']] + self.module_tag.split('.') + ['{0}.py'.format(module)]
file_path = os.path.join(*join_path)
try:
tmp = imp.load_source(module, file_path)
class_init = getattr(tmp, module)
class_module = class_init(main_config['conf_folder'], root_folder=main_config['root_folder'],
main_settings=settings)
params = class_module.conf_params()
if 'id' in params:
priority = params['id']
else:
priority = MODULE_PRI_DEFAULT
if int(priority) in modules:
modules[int(priority)].append(class_module)
else:
modules[int(priority)] = [class_module]
modules_list[module] = params
except ModuleLoadException as exc:
log.error("Unable to load module {0}".format(module))
sorted_module = sorted(modules.items(), key=operator.itemgetter(0))
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):
if ('to' in message) and (message['to'] is not None):
message['text'] = ', '.join([message['to'], message['text']])
if 'id' not in message:
message['id'] = self.msg_counter
self.msg_counter += 1
# When we receive message we pass it via all loaded modules
# All modules should return the message with modified/not modified
# content so it can be passed to new module, or to pass to CLI
for module in self.modules:
message = module.process_message(message, self.queue)
def run(self):
for thread in range(THREADS):
self.threads.append(MessageHandler(self.queue, self.msg_process))
self.threads[thread].start()