forked from DeForce/LalkaChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
241 lines (214 loc) · 9.07 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
# This Python file uses the following encoding: utf-8
# -*- coding: utf-8 -*-
import os
import imp
import Queue
import messaging
import gui
import sys
import logging
import logging.config
import requests
import semantic_version
import locale
from collections import OrderedDict
from modules.helper.parser import self_heal
from modules.helper.system import load_translations_keys
VERSION = '0.3.0'
SEM_VERSION = semantic_version.Version(VERSION)
if hasattr(sys, 'frozen'):
PYTHON_FOLDER = os.path.dirname(sys.executable)
else:
PYTHON_FOLDER = os.path.dirname(os.path.abspath('__file__'))
TRANSLATION_FOLDER = os.path.join(PYTHON_FOLDER, "translations")
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")
GUI_TAG = 'gui'
LOG_FOLDER = os.path.join(PYTHON_FOLDER, "logs")
if not os.path.exists(LOG_FOLDER):
os.makedirs(LOG_FOLDER)
LOG_FILE = os.path.join(LOG_FOLDER, 'chat_log.log')
LOG_FORMAT = logging.Formatter("%(asctime)s [%(name)s] [%(levelname)s] %(message)s")
LANGUAGE_DICT = {
'en_US': 'en',
'en_GB': 'en',
'ru_RU': 'ru'
}
def get_update():
github_url = "https://api.github.com/repos/DeForce/LalkaChat/releases"
try:
update_json = requests.get(github_url)
if update_json.status_code == 200:
update = False
update_url = None
update_list = update_json.json()
for update_item in update_list:
if semantic_version.Version.coerce(update_item['tag_name'].lstrip('v')) > SEM_VERSION:
update = True
update_url = update_item['html_url']
return update, update_url
except Exception as exc:
log.info("Got exception: {0}".format(exc))
return False, None
def get_language():
local_name, local_encoding = locale.getdefaultlocale()
return LANGUAGE_DICT.get(local_name, 'en')
def init():
def close():
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
main_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 {0} folder".format(CONF_FOLDER))
try:
os.mkdir(CONF_FOLDER)
except:
log.error("Was unable to create {0} folder.".format(CONF_FOLDER))
exit()
log.info("Loading basic configuration")
main_config_dict = OrderedDict()
main_config_dict['gui_information'] = OrderedDict()
main_config_dict['gui_information']['category'] = 'main'
main_config_dict['gui_information']['width'] = 450
main_config_dict['gui_information']['height'] = 500
main_config_dict['gui'] = OrderedDict()
main_config_dict['gui']['show_hidden'] = False
main_config_dict['gui']['gui'] = True
main_config_dict['gui']['on_top'] = True
main_config_dict['gui']['reload'] = None
main_config_dict['language'] = get_language()
main_config_gui = {
'language': {
'view': 'choose_single',
'check_type': 'dir',
'check': 'translations'
},
'non_dynamic': ['language.list_box', 'gui.*']
}
config = self_heal(MAIN_CONF_FILE, main_config_dict)
# Adding config for main module
loaded_modules['main'] = {'folder': CONF_FOLDER,
'file': main_config['main_conf_file_loc'],
'filename': main_config['main_conf_file_name'],
'parser': config,
'root_folder': main_config['root_folder'],
'logs_folder': LOG_FOLDER,
'config': main_config_dict,
'gui': main_config_gui}
gui_settings['gui'] = main_config_dict[GUI_TAG].get('gui')
gui_settings['on_top'] = main_config_dict[GUI_TAG].get('on_top')
gui_settings['language'] = main_config_dict.get('language')
gui_settings['show_hidden'] = main_config_dict[GUI_TAG].get('show_hidden')
gui_settings['size'] = (main_config_dict['gui_information'].get('width'),
main_config_dict['gui_information'].get('height'))
# Checking updates
log.info("Checking for updates")
loaded_modules['main']['update'], loaded_modules['main']['update_url'] = get_update()
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")
# 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(main_config, loaded_modules['main']))
msg.start()
log.info("Loading Chats")
# Trying to dynamically load chats that are in config file.
chat_modules = os.path.join(CONF_FOLDER, "chat_modules.cfg")
chat_tag = "chats"
chat_location = os.path.join(MODULE_FOLDER, "chat")
chat_conf_dict = OrderedDict()
chat_conf_dict['gui_information'] = {'category': 'chat'}
chat_conf_dict['chats'] = {}
chat_conf_gui = {
'chats': {
'view': 'choose_multiple',
'check_type': 'files',
'check': os.path.sep.join(['modules', 'chat']),
'file_extension': False},
'non_dynamic': ['chats.list_box']}
chat_config = self_heal(chat_modules, chat_conf_dict)
loaded_modules['chat'] = {'folder': CONF_FOLDER, 'file': chat_modules,
'filename': ''.join(os.path.basename(chat_modules).split('.')[:-1]),
'parser': chat_config,
'config': chat_conf_dict,
'gui': chat_conf_gui}
for module, settings in chat_config.items(chat_tag):
log.info("Loading chat module: {0}".format(module))
module_location = os.path.join(chat_location, module + ".py")
if os.path.isfile(module_location):
log.info("found {0}".format(module))
# After module is find, we are initializing it.
# Class should be named as in config
# Also passing core folder to module so it can load it's own
# configuration correctly
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()
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,
queue=queue)
try:
load_translations_keys(TRANSLATION_FOLDER, gui_settings['language'])
except:
log.exception("Failed loading translations")
if gui_settings['gui']:
log.info("Loading GUI Interface")
window = gui.GuiThread(gui_settings=gui_settings,
main_config=loaded_modules['main'],
loaded_modules=loaded_modules,
queue=queue)
window.start()
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)
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()