Skip to content

Commit

Permalink
Changed how import works.
Browse files Browse the repository at this point in the history
Cleanup code for release
  • Loading branch information
DeForce committed Sep 7, 2016
1 parent c944000 commit 13ea45f
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 112 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# multichat_python

Thanks to ftpud for fixing IE compatibility
43 changes: 0 additions & 43 deletions gui.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
from pprint import pprint
import threading
import wx
import os
import ConfigParser
import re
from modules.helpers.parser import FlagConfigParser
from wx import html2
# import signal
# import thread
# ToDO: Support customization of borders/spacings
# ToDO: Exit by cancel button

Expand Down Expand Up @@ -51,14 +47,12 @@ def load_translations(settings, language):
conf_file = 'translations.cfg'
config = FlagConfigParser(allow_no_value=True)
config.read(os.path.join(settings['conf'], conf_file))
# print config

if not config.has_section(language):
print "Warning, have not found language ", language
language = 'english'

for param, value in config.items(language):
# print param, value
translations[param] = value


Expand Down Expand Up @@ -154,8 +148,6 @@ def __init__(self, parent, configs, on_top=False, title=translate_language("menu
self.configs = configs
self.gui_settings = {}

# pprint(self.main_class.modules_configs)

self.main_grid = wx.BoxSizer(wx.VERTICAL)

if on_top:
Expand All @@ -165,9 +157,6 @@ def __init__(self, parent, configs, on_top=False, title=translate_language("menu

self.SetSizer(self.main_grid)
self.Layout()
# self.main_grid.Fit(self)
# print config_size_max
# self.SetSize(config_size_max)
self.Show(True)

def on_exit(self, event):
Expand Down Expand Up @@ -230,11 +219,9 @@ def button_clicked(self, event):
print ids[event.GetId()]
module_groups = ids[event.GetId()].split('.')
if module_groups[1] == 'apply_button':
# print "Got Apply Event"
self.write_config(module_groups)
self.on_close(event)
elif module_groups[1] == 'cancel_button':
# print "Got Cancel Event"
event.Skip()
elif 'list_add' in module_groups:
self.add_list_item(module_groups)
Expand Down Expand Up @@ -279,7 +266,6 @@ def write_config(self, module_groups):
if isinstance(window, wx.CheckBox):
parser.set(section, param, str(window.IsChecked()).lower())
elif isinstance(window, wx.TextCtrl):
# print "Got TextCtrl, YAY", str(window.GetValue())
parser.set(section, param, window.GetValue().encode('utf-8'))

with open(conf_file, 'w') as config_file:
Expand All @@ -290,27 +276,20 @@ def create_and_load_config(self, main_panel):
notebook_text = translate_language('config')
notebook = wx.Notebook(main_panel, id=notebook_id, style=wx.NB_TOP, name=notebook_text)

# self.main_class.modules_configs = [self.main_class.modules_configs[0]]

for config in self.main_class.modules_configs:
panel_id = wx.Window_NewControlId()
config_name = config.keys()[0]
panel_name = translate_language(config_name)
panel = wx.Panel(notebook, id=panel_id)
# panel.SetScrollbars(5, 5, 10, 10)

# print config[config_name]
panel_sizer = self.load_config(config_params=config[config_name], panel=panel)
panel.SetSizer(panel_sizer)
# panel.Fit()

notebook.AddPage(panel, panel_name)

return notebook

def load_config(self, config_params=None, panel=None, **kwargs):
conf_params = config_params
# print conf_params
sizer = wx.BoxSizer(wx.VERTICAL)

config = FlagConfigParser(allow_no_value=True)
Expand All @@ -324,16 +303,13 @@ def load_config(self, config_params=None, panel=None, **kwargs):
module_prefix = conf_params['filename']

sections = config._sections
# print sections
for item in sections:
# Check if it is GUI settings
item_split = item.split('_')
if len(item_split) > 1 and item_split[-1] == 'gui':
# print "found GUI settings ", '.'.join([module_prefix, '_'.join(item_split[:-1])])
for gui_item in config.get(item, 'for').split(','):
gui_item = gui_item.strip()
gui_item = self.module_key.join([module_prefix, gui_item])
# print gui_item
self.gui_settings[gui_item] = {}
for param, value in config.get_items(item):
if param != 'for':
Expand All @@ -343,8 +319,6 @@ def load_config(self, config_params=None, panel=None, **kwargs):
view = None
gui_module = self.module_key.join([module_prefix, item])
if gui_module in self.gui_settings:
# print "Found Settings for this section", item
# print self.gui_settings[item]
view = self.gui_settings[gui_module].get('view', None)

# Create header for section
Expand Down Expand Up @@ -383,7 +357,6 @@ def load_config(self, config_params=None, panel=None, **kwargs):
list_box_name = '.'.join([module_prefix, item, 'list_box'])
list_box_id = id_change_to_new(list_box_name)
ids[list_box_id] = list_box_name
# list_box_text = translate_language(ids[list_box_id])
list_box = wx.ListBox(f_panel, id=list_box_id, style=wx.LB_EXTENDED)

values = []
Expand All @@ -402,14 +375,12 @@ def load_config(self, config_params=None, panel=None, **kwargs):
for param, value, flags in config.items_with_flags(item):
item_sizer = wx.BoxSizer(wx.HORIZONTAL)
rotate = False
# print param, flags
# Creating item settings
if flags:
module_name = self.keyword.join(['.'.join([module_prefix, item, param]),
self.flag_keyword.join(flags)])
else:
module_name = '.'.join([module_prefix, item, param])
# print module_name

if 'hidden' in flags:
if self.show_hidden:
Expand All @@ -424,8 +395,6 @@ def load_config(self, config_params=None, panel=None, **kwargs):
text_text = translate_language(module_name)
text = wx.StaticText(f_panel, wx.ID_ANY, text_text,
style=wx.ALIGN_RIGHT)
# text.SetBackgroundColour('red')
# text.SetMinSize(fix_sizes(text.GetSize(), self.labelMinSize))

# Creating item objects
if value is not None:
Expand All @@ -435,15 +404,13 @@ def load_config(self, config_params=None, panel=None, **kwargs):
if module_name == 'config.gui.show_hidden' and value == 'true':
self.show_hidden = True
box = wx.CheckBox(f_panel, id=module_id)
# self.main_class.Bind(wx.EVT_CHECKBOX, self.check_box_clicked, id=module_id)
if value == 'true':
box.SetValue(True)
else:
box.SetValue(False)
# If not - it's text control
else:
box = wx.TextCtrl(f_panel, id=module_id, value=value.decode('utf-8'))
# box.AppendText(value)

# If item doesn't have any values it's a button
else:
Expand Down Expand Up @@ -475,8 +442,6 @@ def load_config(self, config_params=None, panel=None, **kwargs):
self.main_class.Bind(wx.EVT_BUTTON, self.button_clicked, id=button_id)
buttons_sizer.Add(apply_button, 0, wx.ALIGN_RIGHT)

# buttons_sizer.AddSpacer(self.flex_horizontal_gap*2)

button_name = '.'.join([module_prefix, 'cancel_button'])
button_id = id_change_to_new(button_name)
ids[button_id] = button_name
Expand Down Expand Up @@ -505,25 +470,19 @@ def __init__(self, parent, title, url, **kwds):
print "Application is on top"
styles = styles | wx.STAY_ON_TOP

# print styles
self.SetWindowStyle(styles)

vbox = wx.BoxSizer(wx.VERTICAL)

toolbar = MainMenuToolBar(self, main_class=self)
self.main_window = html2.WebView.New(parent=self, url=url, name='LalkaWebViewGui')
# self.main_window
# self.main_window = html2.WebView.New(parent=self, url=url, name='LalkaWebViewGui', size=(300, 600))

vbox.Add(toolbar, 0, wx.EXPAND)
vbox.Add(self.main_window, 1, wx.EXPAND)

self.Bind(wx.EVT_LEFT_DOWN, self.on_right_down, self.main_window)
self.Bind(wx.EVT_CLOSE, self.on_exit)
# self.Bind(wx.EVT_TOOL, self.on_settings, id=get_id_from_name('settings'))
# self.Bind(wx.EVT_TOOL, self.on_about, id=ids['about'])

# vbox.Fit(self)
self.SetSizer(vbox)
self.Show(True)

Expand All @@ -538,8 +497,6 @@ def on_right_down(self, event):
print "RClick"

def on_settings(self, event):
# print event
# print wx.STAY_ON_TOP, self.GetWindowStyle()
if not self.settingWindow:
if wx.STAY_ON_TOP & self.GetWindowStyle() == wx.STAY_ON_TOP:
self.settingWindow = SettingsWindow(self, self.main_config, on_top=True,
Expand Down
12 changes: 7 additions & 5 deletions http/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<head>
<title>Lalkachat</title>
<meta charset="UTF-8">
<script src="js/socket.js"></script>
<link rel="stylesheet" type="text/css" href="css/czt.css">
<title>LalkaChat</title>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />

<script src="js/socket.js"></script>
<link rel="stylesheet" type="text/css" href="css/czt.css">
</head>

<body>
<div id='ChatContainer'></div>
</body>
</body>
12 changes: 5 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import ConfigParser
import importlib
import imp
import Queue
import messaging
import gui
Expand All @@ -14,7 +14,7 @@ def init():
loaded_modules = {}
gui_settings = {}

python_folder = os.path.dirname(os.path.abspath(__file__))
python_folder = os.path.dirname(os.path.abspath('__file__'))
conf_folder = os.path.join(python_folder, "conf")
module_folder = os.path.join(python_folder, "modules")

Expand Down Expand Up @@ -79,8 +79,6 @@ def init():

loaded_module_config += msg.modules_configs

# print loaded_module_config

print "Loading Chats Configuration File"
module_tag = "chats"
module_import_folder = "modules"
Expand All @@ -99,7 +97,9 @@ def init():
# Class should be named as in config
# Also passing core folder to module so it can load it's own
# configuration correctly
tmp = importlib.import_module(module_import_folder + '.' + module[0])
file_path = os.path.join(main_config['python'], module_import_folder, '{0}.py'.format(module[0]))

tmp = imp.load_source(module[0], file_path)
class_name = getattr(tmp, module[0])
loaded_modules[module[0]] = class_name(queue, python_folder)
loaded_module_config.insert(module_id, {module[0]: loaded_modules[module[0]].conf_params})
Expand All @@ -114,14 +114,12 @@ def init():
print console
if console == "exit":
print "Exiting now!"
# exit()
thread.interrupt_main()
else:
print "Incorrect Command"
except (KeyboardInterrupt, SystemExit):
print "Exiting now!"
thread.interrupt_main()
# exit()
except Exception as exc:
print exc

Expand Down
14 changes: 5 additions & 9 deletions messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import ConfigParser
import threading
import importlib
import imp
import codecs
import sys

Expand All @@ -15,7 +15,6 @@ def __init__(self, queue):
self.modules = []
self.modules_configs = []
self.daemon = True
# self.modules = {}
self.msg_counter = 0

print "Loading configuration file for messaging"
Expand All @@ -33,9 +32,11 @@ def __init__(self, queue):
# We load the module, and then we initalize it.
# When writing your modules you should have class with the
# same name as module name
tmp = importlib.import_module(module_tag + '.' + module[0])
join_path = [python_folder] + module_tag.split('.') + ['{0}.py'.format(module[0])]
file_path = os.path.join(*join_path)

tmp = imp.load_source(module[0], file_path)
init = getattr(tmp, module[0])
# self.modules[module[0]] = init(conf_folder)
class_module = init(conf_folder)
self.modules.append(class_module)
self.modules_configs.append({module[0]: class_module.conf_params})
Expand All @@ -61,11 +62,6 @@ def msg_process(self, message):
except Exception as exc:
print exc

# if message is not None:
# print type(message['text'])
# print type(message['text'].encode('utf-8').decode('utf-8'))
# print "[%s] %s: %s" % (message['source'], message['user'], message['text'])

def run(self):
while True:
UTF8Writer = codecs.getwriter('utf8')
Expand Down
10 changes: 2 additions & 8 deletions modules/goodgame.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import threading
import os
import ConfigParser
import requests
import Queue
import re
Expand Down Expand Up @@ -133,7 +132,6 @@ def __init__(self, queue, address, id, nick):
self.smiles = []

def run(self):
# Get the fucking smiles
try:
smile_request = requests.get("http://api2.goodgame.ru/smiles")
next_page = smile_request.json()['_links']['first']['href']
Expand Down Expand Up @@ -188,26 +186,22 @@ def __init__(self, queue, python_folder):
try:
request = requests.get("http://api2.goodgame.ru/streams/"+ch_id)
if request.status_code == 200:
# print type(request.json())
channel_name = request.json()['channel']['key']
# print request.json()
except:
print "Issue with goodgame"

if channel_name:
try:
request = requests.get("http://api2.goodgame.ru/streams/"+channel_name)
if request.status_code == 200:
# print type(request.json())
ch_id = request.json()['channel']['id']
# print request.json()
except:
print "Issue with goodgame"
# If any of the value are non-existent then exit the programm with error.
# If any of the value are non-existent then exit the program with error.
if (address is None) or (ch_id is None):
print "Config for goodgame is not correct!"
exit()

# Creating new thread with queue in place for messaging tranfers
# Creating new thread with queue in place for messaging transfers
gg = ggThread(queue, address, ch_id, channel_name)
gg.start()
1 change: 0 additions & 1 deletion modules/helpers/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def items_with_flags(self, section):
items = self.items(section)
for param, value in items:
split_param = param.split(self.keyword)
# print split_param
if len(split_param) > 1:
yield split_param[0], value, split_param[1].split(self.flag_keyword)
else:
Expand Down
1 change: 0 additions & 1 deletion modules/messaging/blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def __init__(self, conf_folder):

def get_message(self, message, queue):
if message is None:
# print "Blackist recieved no message"
return
else:
user = self.process_user(message)
Expand Down
Loading

0 comments on commit 13ea45f

Please sign in to comment.