From 83adadd71f1acc22657d2b159071397139e168f3 Mon Sep 17 00:00:00 2001 From: blitzmann Date: Sun, 8 Nov 2015 11:07:01 -0500 Subject: [PATCH] Convert pubsub to wx events --- gui/crestFittings.py | 68 +++++++++++++++++++++++++++++++++----------- gui/globalEvents.py | 3 ++ gui/mainFrame.py | 51 ++++++++++++++++++++++++++------- gui/mainMenuBar.py | 27 ------------------ service/crest.py | 43 +++++++++++++++++++--------- service/server.py | 10 +++---- 6 files changed, 129 insertions(+), 73 deletions(-) diff --git a/gui/crestFittings.py b/gui/crestFittings.py index c4a342f62..8e57438f9 100644 --- a/gui/crestFittings.py +++ b/gui/crestFittings.py @@ -3,15 +3,16 @@ import webbrowser import json import wx -from wx.lib.pubsub import setupkwargs -from wx.lib.pubsub import pub - import service from service.crest import CrestModes -import gui.display as d + from eos.types import Cargo from eos.db import getItem +import gui.display as d +import gui.globalEvents as GE + + class CrestFittings(wx.Frame): def __init__(self, parent): @@ -67,7 +68,9 @@ class CrestFittings(wx.Frame): self.importBtn.Bind(wx.EVT_BUTTON, self.importFitting) self.deleteBtn.Bind(wx.EVT_BUTTON, self.deleteFitting) - pub.subscribe(self.ssoLogout, 'logout_success') + self.mainFrame.Bind(GE.EVT_SSO_LOGOUT, self.ssoLogout) + self.mainFrame.Bind(GE.EVT_SSO_LOGIN, self.ssoLogin) + self.Bind(wx.EVT_CLOSE, self.OnClose) self.statusbar = wx.StatusBar(self) self.statusbar.SetFieldsCount() @@ -81,6 +84,10 @@ class CrestFittings(wx.Frame): self.Centre(wx.BOTH) + def ssoLogin(self, event): + self.updateCharList() + event.Skip() + def updateCharList(self): sCrest = service.Crest.getInstance() chars = sCrest.getCrestCharacters() @@ -88,6 +95,7 @@ class CrestFittings(wx.Frame): if len(chars) == 0: self.Close() + self.charChoice.Clear() for char in chars: self.charChoice.Append(char.name, char.ID) @@ -102,8 +110,17 @@ class CrestFittings(wx.Frame): sTime = time.strftime("%H:%M:%S", t) self.statusbar.SetStatusText("Cached for %s"%sTime, 0) - def ssoLogout(self, message): - self.Close() + def ssoLogout(self, event): + if event.type == CrestModes.IMPLICIT: + self.Close() + else: + self.updateCharList() + event.Skip() # continue event + + def OnClose(self, event): + self.mainFrame.Unbind(GE.EVT_SSO_LOGOUT, handler=self.ssoLogout) + self.mainFrame.Unbind(GE.EVT_SSO_LOGIN, handler=self.ssoLogin) + event.Skip() def getActiveCharacter(self): sCrest = service.Crest.getInstance() @@ -182,7 +199,9 @@ class ExportToEve(wx.Frame): self.statusbar.SetFieldsCount(2) self.statusbar.SetStatusWidths([100, -1]) - pub.subscribe(self.ssoLogout, 'logout_success') + self.mainFrame.Bind(GE.EVT_SSO_LOGOUT, self.ssoLogout) + self.mainFrame.Bind(GE.EVT_SSO_LOGIN, self.ssoLogin) + self.Bind(wx.EVT_CLOSE, self.OnClose) self.SetSizer(hSizer) self.SetStatusBar(self.statusbar) @@ -197,13 +216,26 @@ class ExportToEve(wx.Frame): if len(chars) == 0: self.Close() + self.charChoice.Clear() for char in chars: self.charChoice.Append(char.name, char.ID) self.charChoice.SetSelection(0) - def ssoLogout(self, message): - self.Close() + def ssoLogin(self, event): + self.updateCharList() + event.Skip() + + def ssoLogout(self, event): + if event.type == CrestModes.IMPLICIT: + self.Close() + else: + self.updateCharList() + event.Skip() # continue event + + def OnClose(self, event): + self.mainFrame.Unbind(GE.EVT_SSO_LOGOUT, handler=self.ssoLogout) + event.Skip() def getActiveCharacter(self): sCrest = service.Crest.getInstance() @@ -235,7 +267,7 @@ class CrestMgmt(wx.Dialog): def __init__( self, parent ): wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = "CREST Character Management", pos = wx.DefaultPosition, size = wx.Size( 550,250 ), style = wx.DEFAULT_DIALOG_STYLE ) - + self.mainFrame = parent mainSizer = wx.BoxSizer( wx.HORIZONTAL ) self.lcCharacters = wx.ListCtrl( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LC_REPORT) @@ -260,15 +292,16 @@ class CrestMgmt(wx.Dialog): self.addBtn.Bind(wx.EVT_BUTTON, self.addChar) self.deleteBtn.Bind(wx.EVT_BUTTON, self.delChar) - pub.subscribe(self.ssoLogin, 'login_success') + self.mainFrame.Bind(GE.EVT_SSO_LOGIN, self.ssoLogin) self.SetSizer( mainSizer ) self.Layout() self.Centre( wx.BOTH ) - def ssoLogin(self, type): + def ssoLogin(self, event): self.popCharList() + event.Skip() def popCharList(self): sCrest = service.Crest.getInstance() @@ -291,10 +324,11 @@ class CrestMgmt(wx.Dialog): def delChar(self, event): item = self.lcCharacters.GetFirstSelected() - charID = self.lcCharacters.GetItemData(item) - sCrest = service.Crest.getInstance() - sCrest.delCrestCharacter(charID) - self.popCharList() + if item > -1: + charID = self.lcCharacters.GetItemData(item) + sCrest = service.Crest.getInstance() + sCrest.delCrestCharacter(charID) + self.popCharList() class FittingsTreeView(wx.Panel): diff --git a/gui/globalEvents.py b/gui/globalEvents.py index afb2d7bb3..18f91d348 100644 --- a/gui/globalEvents.py +++ b/gui/globalEvents.py @@ -3,3 +3,6 @@ import wx.lib.newevent FitChanged, FIT_CHANGED = wx.lib.newevent.NewEvent() CharListUpdated, CHAR_LIST_UPDATED = wx.lib.newevent.NewEvent() CharChanged, CHAR_CHANGED = wx.lib.newevent.NewEvent() + +SsoLogin, EVT_SSO_LOGIN = wx.lib.newevent.NewEvent() +SsoLogout, EVT_SSO_LOGOUT = wx.lib.newevent.NewEvent() diff --git a/gui/mainFrame.py b/gui/mainFrame.py index d86bce947..592e729ff 100644 --- a/gui/mainFrame.py +++ b/gui/mainFrame.py @@ -64,8 +64,6 @@ from time import gmtime, strftime if not 'wxMac' in wx.PlatformInfo or ('wxMac' in wx.PlatformInfo and wx.VERSION >= (3,0)): from service.crest import CrestModes from gui.crestFittings import CrestFittings, ExportToEve, CrestMgmt - from wx.lib.pubsub import setupkwargs - from wx.lib.pubsub import pub try: from gui.propertyEditor import AttributeEditor @@ -210,8 +208,8 @@ class MainFrame(wx.Frame): self.sUpdate.CheckUpdate(self.ShowUpdateBox) if not 'wxMac' in wx.PlatformInfo or ('wxMac' in wx.PlatformInfo and wx.VERSION >= (3,0)): - pub.subscribe(self.onSSOLogin, 'login_success') - pub.subscribe(self.onSSOLogout, 'logout_success') + self.Bind(GE.EVT_SSO_LOGIN, self.onSSOLogin) + self.Bind(GE.EVT_SSO_LOGOUT, self.onSSOLogout) self.titleTimer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.updateTitle, self.titleTimer) @@ -448,8 +446,8 @@ class MainFrame(wx.Frame): self.Bind(wx.EVT_MENU, self.eveFittings, id = menuBar.eveFittingsId) # Export to EVE self.Bind(wx.EVT_MENU, self.exportToEve, id = menuBar.exportToEveId) - # Login to EVE - self.Bind(wx.EVT_MENU, self.ssoLogin, id = menuBar.ssoLoginId) + # Handle SSO event (login/logout/manage characters, depending on mode and current state) + self.Bind(wx.EVT_MENU, self.ssoHandler, id = menuBar.ssoLoginId) # Open attribute editor self.Bind(wx.EVT_MENU, self.showAttrEditor, id = menuBar.attrEditorId) @@ -532,15 +530,48 @@ class MainFrame(wx.Frame): newTitle = "%s | %s - %s"%(self.title, char.name, sTime) self.SetTitle(newTitle) - def onSSOLogin(self, type): - if type == 0: + def onSSOLogin(self, event): + menu = self.GetMenuBar() + menu.Enable(menu.eveFittingsId, True) + menu.Enable(menu.exportToEveId, True) + + if event.type == CrestModes.IMPLICIT: + menu.SetLabel(menu.ssoLoginId, "Logout Character") self.titleTimer.Start(1000) - def onSSOLogout(self, message): + def onSSOLogout(self, event): self.titleTimer.Stop() self.SetTitle(self.title) - def ssoLogin(self, event): + menu = self.GetMenuBar() + if event.type == CrestModes.IMPLICIT or event.numChars == 0: + menu.Enable(menu.eveFittingsId, False) + menu.Enable(menu.exportToEveId, False) + + if event.type == CrestModes.IMPLICIT: + menu.SetLabel(menu.ssoLoginId, "Login to EVE") + + def updateCrestMenus(self, type): + # in case we are logged in when switching, change title back + self.titleTimer.Stop() + self.SetTitle(self.title) + + menu = self.GetMenuBar() + sCrest = service.Crest.getInstance() + + if type == CrestModes.IMPLICIT: + print 'impl' + menu.SetLabel(menu.ssoLoginId, "Login to EVE") + menu.Enable(menu.eveFittingsId, False) + menu.Enable(menu.exportToEveId, False) + else: + print 'user' + menu.SetLabel(menu.ssoLoginId, "Manage Characters") + enable = len(sCrest.getCrestCharacters()) == 0 + menu.Enable(menu.eveFittingsId, not enable) + menu.Enable(menu.exportToEveId, not enable) + + def ssoHandler(self, event): sCrest = service.Crest.getInstance() if sCrest.settings.get('mode') == CrestModes.IMPLICIT: if sCrest.implicitCharacter is not None: diff --git a/gui/mainMenuBar.py b/gui/mainMenuBar.py index 84f080703..0e636a3e6 100644 --- a/gui/mainMenuBar.py +++ b/gui/mainMenuBar.py @@ -28,9 +28,6 @@ import service if not 'wxMac' in wx.PlatformInfo or ('wxMac' in wx.PlatformInfo and wx.VERSION >= (3,0)): from service.crest import CrestModes -from wx.lib.pubsub import setupkwargs -from wx.lib.pubsub import pub - class MainMenuBar(wx.MenuBar): def __init__(self): self.characterEditorId = wx.NewId() @@ -138,10 +135,6 @@ class MainMenuBar(wx.MenuBar): editMenu.AppendSeparator() editMenu.Append(self.toggleOverridesId, "Turn Overrides On") - pub.subscribe(self.ssoLogin, 'login_success') - pub.subscribe(self.ssoLogout, 'logout_success') - pub.subscribe(self.updateCrest, 'crest_changed') - # Help menu helpMenu = wx.Menu() self.Append(helpMenu, "&Help") @@ -172,24 +165,4 @@ class MainMenuBar(wx.MenuBar): event.Skip() - def ssoLogin(self, type): - if self.sCrest.settings.get('mode') == CrestModes.IMPLICIT: - self.SetLabel(self.ssoLoginId, "Logout Character") - self.Enable(self.eveFittingsId, True) - self.Enable(self.exportToEveId, True) - - def ssoLogout(self, message): - if self.sCrest.settings.get('mode') == CrestModes.IMPLICIT: - self.SetLabel(self.ssoLoginId, "Login to EVE") - self.Enable(self.eveFittingsId, False) - self.Enable(self.exportToEveId, False) - - def updateCrest(self, message): - bool = self.sCrest.settings.get('mode') == CrestModes.IMPLICIT or len(self.sCrest.getCrestCharacters()) == 0 - self.Enable(self.eveFittingsId, not bool) - self.Enable(self.exportToEveId, not bool) - if self.sCrest.settings.get('mode') == CrestModes.IMPLICIT: - self.SetLabel(self.ssoLoginId, "Login to EVE") - else: - self.SetLabel(self.ssoLoginId, "Manage Characters") diff --git a/service/crest.py b/service/crest.py index e49704861..d890701bf 100644 --- a/service/crest.py +++ b/service/crest.py @@ -1,19 +1,19 @@ +import wx import thread -import config import logging import threading import copy import uuid -import wx import time -from wx.lib.pubsub import pub - import eos.db from eos.enum import Enum from eos.types import CrestChar + import service +import gui.globalEvents as GE + logger = logging.getLogger(__name__) class Servers(Enum): @@ -51,10 +51,19 @@ class Crest(): if cls._instance.httpd: cls._instance.stopServer() cls._instance = Crest() - wx.CallAfter(pub.sendMessage, 'crest_changed', message=None) + cls._instance.mainFrame.updateCrestMenus(type=cls._instance.settings.get('mode')) return cls._instance def __init__(self): + """ + A note on login/logout events: the character login events happen + whenever a characters is logged into via the SSO, regardless of mod. + However, the mode should be send as an argument. Similarily, + the Logout even happens whenever the character is deleted for either + mode. The mode is sent as an argument, as well as the umber of + characters still in the cache (if USER mode) + """ + self.settings = service.settings.CRESTSettings.getInstance() self.scopes = ['characterFittingsRead', 'characterFittingsWrite'] @@ -76,7 +85,10 @@ class Crest(): # The database cache does not seem to be working for some reason. Use # this as a temporary measure self.charCache = {} - pub.subscribe(self.handleLogin, 'sso_login') + + # need these here to post events + import gui.mainFrame # put this here to avoid loop + self.mainFrame = gui.mainFrame.MainFrame.getInstance() @property def isTestServer(self): @@ -84,19 +96,23 @@ class Crest(): def delCrestCharacter(self, charID): char = eos.db.getCrestCharacter(charID) + print self.charCache + del self.charCache[char.ID] eos.db.remove(char) - wx.CallAfter(pub.sendMessage, 'crest_delete', message=None) + wx.PostEvent(self.mainFrame, GE.SsoLogout(type=CrestModes.USER, numChars=len(self.charCache))) def delAllCharacters(self): chars = eos.db.getCrestCharacters() for char in chars: eos.db.remove(char) self.charCache = {} - wx.CallAfter(pub.sendMessage, 'crest_delete', message=None) + wx.PostEvent(self.mainFrame, GE.SsoLogout(type=CrestModes.USER, numChars=0)) def getCrestCharacters(self): chars = eos.db.getCrestCharacters() - return chars + # I really need to figure out that DB cache problem, this is ridiculous + chars2 = [self.getCrestCharacter(char.ID) for char in chars] + return chars2 def getCrestCharacter(self, charID): ''' @@ -131,9 +147,10 @@ class Crest(): return char.eve.delete('%scharacters/%d/fittings/%d/'%(char.eve._authed_endpoint, char.ID, fittingID)) def logout(self): + """Logout of implicit character""" logging.debug("Character logout") self.implicitCharacter = None - wx.CallAfter(pub.sendMessage, 'logout_success', message=None) + wx.PostEvent(self.mainFrame, GE.SsoLogout(type=self.settings.get('mode'))) def stopServer(self): logging.debug("Stopping Server") @@ -146,7 +163,7 @@ class Crest(): self.stopServer() time.sleep(1) # we need this to ensure that the previous get_request finishes, and then the socket will close self.httpd = service.StoppableHTTPServer(('', 6461), service.AuthHandler) - thread.start_new_thread(self.httpd.serve, ()) + thread.start_new_thread(self.httpd.serve, (self.handleLogin,)) self.state = str(uuid.uuid4()) return self.eve.auth_uri(scopes=self.scopes, state=self.state) @@ -179,7 +196,7 @@ class Crest(): self.implicitCharacter.eve = eve #self.implicitCharacter.fetchImage() - wx.CallAfter(pub.sendMessage, 'login_success', type=CrestModes.IMPLICIT) + wx.PostEvent(self.mainFrame, GE.SsoLogin(type=CrestModes.IMPLICIT)) elif 'code' in message: eve = copy.deepcopy(self.eve) eve.authorize(message['code'][0]) @@ -198,6 +215,6 @@ class Crest(): self.charCache[int(info['CharacterID'])] = char eos.db.save(char) - wx.CallAfter(pub.sendMessage, 'login_success', type=CrestModes.USER) + wx.PostEvent(self.mainFrame, GE.SsoLogin(type=CrestModes.USER)) self.stopServer() diff --git a/service/server.py b/service/server.py index d33117c72..8e81e171b 100644 --- a/service/server.py +++ b/service/server.py @@ -4,11 +4,7 @@ import socket import thread import wx -from wx.lib.pubsub import setupkwargs -from wx.lib.pubsub import pub - import logging -import time logger = logging.getLogger(__name__) @@ -49,7 +45,7 @@ class AuthHandler(BaseHTTPServer.BaseHTTPRequestHandler): self.end_headers() self.wfile.write(HTML) - wx.CallAfter(pub.sendMessage, 'sso_login', message=parts) + wx.CallAfter(self.server.callback, parts) def log_message(self, format, *args): return @@ -86,7 +82,9 @@ class StoppableHTTPServer(BaseHTTPServer.HTTPServer): logger.debug("Server timed out waiting for connection") self.stop() - def serve(self): + def serve(self, callback): + self.callback = callback + print callback while self.run: try: self.handle_request()