-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
273 lines (246 loc) · 10.7 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# This Python file uses the following encoding: utf-8
# -*- coding: utf-8 -*-
# Copyright (C) 2016 CzT/Vladislav Ivanov
import Queue
import logging
import logging.config
import logging.handlers
import os
from collections import OrderedDict
from time import sleep
import semantic_version
import messaging
from modules.helper.functions import get_class_from_iname
from modules.helper.module import BaseModule
from modules.helper.parser import load_from_config_file
from modules.helper.system import load_translations_keys, PYTHON_FOLDER, CONF_FOLDER, MAIN_CONF_FILE, MODULE_FOLDER, \
LOG_FOLDER, GUI_TAG, TRANSLATION_FOLDER, LOG_FILE, LOG_FORMAT, get_language, get_update, ModuleLoadException
from modules.interface.types import LCStaticBox, LCText, LCBool, LCButton, LCPanel, LCSpin, LCSlider, LCChooseMultiple
VERSION = '0.4.0'
SEM_VERSION = semantic_version.Version(VERSION)
LOG_FILES_COUNT = 5
def button_test(event):
log.info('HelloWorld')
def init():
def close():
for l_module, l_module_dict in loaded_modules.iteritems():
l_module_dict['class'].apply_settings(system_exit=True)
if window:
window.gui.on_close('Closing Program from console')
else:
os._exit(0)
# For system compatibility, loading chats
loaded_modules = OrderedDict()
gui_settings = {}
window = None
# Creating dict with folder settings
base_config = {'root_folder': PYTHON_FOLDER,
'conf_folder': CONF_FOLDER,
'main_conf_file': MAIN_CONF_FILE,
'main_conf_file_loc': MAIN_CONF_FILE,
'main_conf_file_name': ''.join(os.path.basename(MAIN_CONF_FILE).split('.')[:-1]),
'update': False}
if not os.path.isdir(MODULE_FOLDER):
logging.error("Was not able to find modules folder, check you installation")
exit()
# Trying to load config file.
# Create folder if doesn't exist
if not os.path.isdir(CONF_FOLDER):
log.error("Could not find %s folder", CONF_FOLDER)
try:
os.mkdir(CONF_FOLDER)
except Exception as exc:
log.debug("Exception: %s", exc)
log.error("Was unable to create %s folder.", CONF_FOLDER)
exit()
log.info("Loading basic configuration")
main_config_dict = LCPanel()
main_config_dict['gui_information'] = LCStaticBox()
main_config_dict['gui_information']['width'] = LCText('450')
main_config_dict['gui_information']['height'] = LCText('500')
main_config_dict['gui_information']['pos_x'] = LCText('10')
main_config_dict['gui_information']['pos_y'] = LCText('10')
main_config_dict['system'] = LCStaticBox()
main_config_dict['system']['log_level'] = LCText('INFO')
main_config_dict['system']['testing_mode'] = LCBool(False)
main_config_dict['gui'] = LCStaticBox()
main_config_dict['gui']['cli'] = LCBool(False)
main_config_dict['gui']['show_icons'] = LCBool(False)
main_config_dict['gui']['show_hidden'] = LCBool(False)
main_config_dict['gui']['gui'] = LCBool(True)
main_config_dict['gui']['on_top'] = LCBool(True)
main_config_dict['gui']['show_browser'] = LCBool(True)
main_config_dict['gui']['show_counters'] = LCBool(True)
main_config_dict['gui']['transparency'] = LCSlider(100, min_v=0, max_v=100)
main_config_dict['gui']['borderless'] = LCBool(False)
main_config_dict['gui']['reload'] = LCButton(button_test)
main_config_dict['language'] = LCText(get_language())
main_config_gui = {
'language': {
'view': 'choose_single',
'check_type': 'dir',
'check': 'translations'
},
'system': {
'hidden': ['log_level', 'testing_mode'],
},
'gui': {
'hidden': ['cli'],
'transparency': {
'view': 'slider',
'min': 10,
'max': 100
}
},
'ignored_sections': ['gui.reload'],
'non_dynamic': [
'language.list_box',
'gui.borderless',
'gui.cli',
'gui.gui',
'gui.show_browser',
'gui.show_hidden',
'gui.show_icons',
'gui.transparency',
'system.*'
]
}
# Adding config for main module
main_class = BaseModule(
conf_params={
'root_folder': base_config['root_folder'],
'logs_folder': LOG_FOLDER,
},
config=main_config_dict,
gui=main_config_gui,
conf_file_name='config.cfg',
category='main'
)
loaded_modules['main'] = main_class.conf_params()
main_config = main_class.conf_params()['config']
root_logger.setLevel(level=logging.getLevelName(main_config['system'].get('log_level', 'INFO')))
gui_settings['gui'] = main_config[GUI_TAG].get('gui')
gui_settings['on_top'] = main_config[GUI_TAG].get('on_top')
gui_settings['transparency'] = main_config[GUI_TAG].get('transparency')
gui_settings['borderless'] = main_config[GUI_TAG].get('borderless')
gui_settings['language'] = main_config.get('language')
gui_settings['show_hidden'] = main_config[GUI_TAG].get('show_hidden')
gui_settings['size'] = (int(main_config['gui_information'].get('width')),
int(main_config['gui_information'].get('height')))
gui_settings['position'] = (int(main_config['gui_information'].get('pos_x')),
int(main_config['gui_information'].get('pos_y')))
gui_settings['show_browser'] = main_config['gui'].get('show_browser')
# Checking updates
log.info("Checking for updates")
loaded_modules['main']['update'], loaded_modules['main']['update_url'] = get_update(SEM_VERSION)
if loaded_modules['main']['update']:
log.info("There is new update, please update!")
# Starting modules
log.info("Loading Messaging Handler")
log.info("Loading Queue for message handling")
try:
load_translations_keys(TRANSLATION_FOLDER, gui_settings['language'])
except Exception as exc:
log.debug("Exception: %s", exc)
log.exception("Failed loading translations")
# Creating queues for messaging transfer between chat threads
queue = Queue.Queue()
# Loading module for message processing...
msg = messaging.Message(queue)
loaded_modules.update(msg.load_modules(base_config, loaded_modules['main']))
msg.start()
log.info("Loading Chats")
# Trying to dynamically load chats that are in config file.
chat_modules_file = os.path.join(CONF_FOLDER, "chat_modules.cfg")
chat_location = os.path.join(MODULE_FOLDER, "chat")
chat_conf_dict = OrderedDict()
chat_conf_dict['chats'] = LCChooseMultiple(
[],
check_type='files',
folder=os.path.sep.join(['modules', 'chat']),
keep_extension=False
)
chat_conf_gui = {
'non_dynamic': ['chats.list_box']
}
chat_init_module = BaseModule(
conf_params={
'config': load_from_config_file(chat_modules_file, chat_conf_dict),
'gui': chat_conf_gui
},
conf_file_name='chat_modules.cfg',
category='chat'
)
loaded_modules['chat'] = chat_init_module.conf_params()
for chat_module_name in chat_conf_dict['chats'].simple():
log.info("Loading chat module: {0}".format(chat_module_name))
module_location = os.path.join(chat_location, chat_module_name + ".py")
if os.path.isfile(module_location):
log.info("found {0}".format(chat_module_name))
# After module is find, we are initializing it.
# Class should be named same as module(Case insensitive)
# Also passing core folder to module so it can load it's own
# configuration correctly
chat_init = get_class_from_iname(module_location, chat_module_name)
class_module = chat_init(queue=queue,
conf_folder=CONF_FOLDER,
conf_file=os.path.join(CONF_FOLDER, '{0}.cfg'.format(chat_module_name)),
testing=main_config_dict['system']['testing_mode'])
loaded_modules[chat_module_name.lower()] = 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:
try:
f_config['class'].load_module(main_settings=base_config, loaded_modules=loaded_modules)
log.debug('loaded module {}'.format(f_module))
except ModuleLoadException:
msg.modules.remove(loaded_modules[f_module]['class'])
loaded_modules.pop(f_module)
log.info('LalkaChat loaded successfully')
if gui_settings['gui']:
from modules import gui
log.info("Loading GUI Interface")
window = gui.GuiThread(gui_settings=gui_settings,
main_config=loaded_modules['main'],
loaded_modules=loaded_modules,
queue=queue)
loaded_modules['gui'] = window.conf_params()
window.run()
else:
if main_config_dict['gui']['cli']:
try:
while True:
console = raw_input("> ")
log.info(console)
if console == "exit":
log.info("Exiting now!")
close()
else:
log.info("Incorrect Command")
except (KeyboardInterrupt, SystemExit):
log.info("Exiting now")
close()
except Exception as exc:
log.info(exc)
else:
try:
while True:
sleep(1)
except (KeyboardInterrupt, SystemExit):
log.info("Exiting now")
close()
if __name__ == '__main__':
root_logger = logging.getLogger()
# Logging level
file_handler = logging.handlers.RotatingFileHandler(
LOG_FILE, maxBytes=1000*1024, backupCount=LOG_FILES_COUNT)
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()