Implement asynchronous api skill fetching

This commit is contained in:
blitzmann
2017-04-14 23:24:37 -04:00
parent 53cab4b1ab
commit 8c83ac120f
3 changed files with 70 additions and 33 deletions

View File

@@ -657,14 +657,19 @@ class APIView(wx.Panel):
def fetchSkills(self, event):
charName = self.charChoice.GetString(self.charChoice.GetSelection())
if charName:
try:
sChar = Character.getInstance()
activeChar = self.charEditor.entityEditor.getActiveEntity()
sChar.apiFetch(activeChar.ID, charName)
self.stStatus.SetLabel("Successfully fetched %s\'s skills from EVE API." % charName)
except Exception, e:
pyfalog.error("Unable to retrieve {0}\'s skills. Error message:\n{1}", charName, e)
self.stStatus.SetLabel("Unable to retrieve %s\'s skills. Error message:\n%s" % (charName, e))
sChar = Character.getInstance()
activeChar = self.charEditor.entityEditor.getActiveEntity()
sChar.apiFetch(activeChar.ID, charName, self.__fetchCallback)
self.stStatus.SetLabel("Getting skills for {}".format(charName))
def __fetchCallback(self, e=None):
charName = self.charChoice.GetString(self.charChoice.GetSelection())
if e is None:
self.stStatus.SetLabel("Successfully fetched {}\'s skills from EVE API.".format(charName))
else:
exc_type, exc_obj, exc_trace = e
pyfalog.error("Unable to retrieve {0}\'s skills. Error message:\n{1}".format(charName, exc_obj))
self.stStatus.SetLabel("Unable to retrieve {}\'s skills. Error message:\n{}".format(charName, exc_obj))
class SaveCharacterAs(wx.Dialog):

View File

@@ -25,6 +25,7 @@ import gui.mainFrame
from service.character import Character
from service.fit import Fit
from logbook import Logger
pyfalog = Logger(__name__)
@@ -109,16 +110,24 @@ class CharacterSelection(wx.Panel):
event.Skip()
def refreshApi(self, event):
self.btnRefresh.Enable(False)
sChar = Character.getInstance()
ID, key, charName, chars = sChar.getApiDetails(self.getActiveCharacter())
if charName:
try:
sChar.apiFetch(self.getActiveCharacter(), charName)
except Exception as e:
# can we do a popup, notifying user of API error?
pyfalog.error("API fetch error")
pyfalog.error(e)
self.refreshCharacterList()
sChar.apiFetch(self.getActiveCharacter(), charName, self.refreshAPICallback)
def refreshAPICallback(self, e=None):
self.btnRefresh.Enable(True)
if e is None:
self.refreshCharacterList()
else:
exc_type, exc_obj, exc_trace = e
pyfalog.warn("Error fetching API information for character")
pyfalog.warn(exc_obj)
wx.MessageBox(
"Error fetching API information, please check your API details in the character editor and try again later",
"Error", wx.ICON_ERROR | wx.STAY_ON_TOP)
def charChanged(self, event):
fitID = self.mainFrame.getActiveFit()

View File

@@ -17,9 +17,11 @@
# along with pyfa. If not, see <http://www.gnu.org/licenses/>.
# =============================================================================
import sys
import copy
import itertools
import json
from logbook import Logger
import threading
from codecs import open
@@ -350,26 +352,13 @@ class Character(object):
char.chars = json.dumps(charList)
return charList
@staticmethod
def apiFetch(charID, charName):
dbChar = eos.db.getCharacter(charID)
dbChar.defaultChar = charName
def apiFetch(self, charID, charName, callback):
thread = UpdateAPIThread(charID, charName, (self.apiFetchCallback, callback))
thread.start()
api = EVEAPIConnection()
auth = api.auth(keyID=dbChar.apiID, vCode=dbChar.apiKey)
apiResult = auth.account.Characters()
charID = None
for char in apiResult.characters:
if char.name == charName:
charID = char.characterID
if charID is None:
return
sheet = auth.character(charID).CharacterSheet()
dbChar.apiUpdateCharSheet(sheet.skills)
def apiFetchCallback(self, guiCallback, e=None):
eos.db.commit()
wx.CallAfter(guiCallback, e)
@staticmethod
def apiUpdateCharSheet(charID, skills):
@@ -458,3 +447,37 @@ class Character(object):
self._checkRequirements(fit, char, req, subs)
return reqs
class UpdateAPIThread(threading.Thread):
def __init__(self, charID, charName, callback):
threading.Thread.__init__(self)
self.name = "CheckUpdate"
self.callback = callback
self.charID = charID
self.charName = charName
def run(self):
print "in thread"
try:
dbChar = eos.db.getCharacter(self.charID)
dbChar.defaultChar = self.charName
api = EVEAPIConnection()
auth = api.auth(keyID=dbChar.apiID, vCode=dbChar.apiKey)
apiResult = auth.account.Characters()
charID = None
for char in apiResult.characters:
if char.name == self.charName:
charID = char.characterID
if charID is None:
return
sheet = auth.character(charID).CharacterSheet()
dbChar.apiUpdateCharSheet(sheet.skills)
self.callback[0](self.callback[1])
except Exception:
self.callback[0](self.callback[1], sys.exc_info())