Implement ProgressDialog for fit file imports (EVE XML / EFT cfg).

To make it simpler, changed the way fits are imported and saved from a per-file basis (process file->save fits->process file->save fits) to a per-batch basis (process file->process file -> save all fits).
This commit is contained in:
blitzmann
2014-12-17 21:34:43 -05:00
parent 25e30672fe
commit 26e50f2e8a
3 changed files with 64 additions and 40 deletions

View File

@@ -17,7 +17,6 @@
# along with pyfa. If not, see <http://www.gnu.org/licenses/>.
#===============================================================================
import os.path
import locale
import copy
import threading
@@ -53,6 +52,7 @@ class FitBackupThread(threading.Thread):
backupFile.close()
wx.CallAfter(self.callback, -1)
class FitImportThread(threading.Thread):
def __init__(self, paths, callback):
threading.Thread.__init__(self)
@@ -60,14 +60,10 @@ class FitImportThread(threading.Thread):
self.callback = callback
def run(self):
importedFits = []
paths = self.paths
sFit = Fit.getInstance()
for path in paths:
pathImported = sFit.importFit(path)
if pathImported is not None:
importedFits += pathImported
wx.CallAfter(self.callback, importedFits)
sFit.importFitFromFiles(self.paths, self.callback)
wx.CallAfter(self.callback, -1)
class Fit(object):
@@ -772,26 +768,37 @@ class Fit(object):
thread = FitImportThread(paths, callback)
thread.start()
def importFit(self, path):
filename = os.path.split(path)[1]
def importFitFromFiles(self, paths, callback=None):
defcodepage = locale.getpreferredencoding()
file = open(path, "r")
srcString = file.read()
# If file had ANSI encoding, convert it to unicode using system
# default codepage, or use fallback cp1252 on any encoding errors
if isinstance(srcString, str):
try:
srcString = unicode(srcString, defcodepage)
except UnicodeDecodeError:
srcString = unicode(srcString, "cp1252")
fitImport = []
for path in paths:
if callback: # Pulse
wx.CallAfter(callback, "Processing file:\n%s"%path)
_, fits = Port.importAuto(srcString, filename)
for fit in fits:
file = open(path, "r")
srcString = file.read()
# If file had ANSI encoding, convert it to unicode using system
# default codepage, or use fallback cp1252 on any encoding errors
if isinstance(srcString, str):
try:
srcString = unicode(srcString, defcodepage)
except UnicodeDecodeError:
srcString = unicode(srcString, "cp1252")
_, fits = Port.importAuto(srcString, path, callback=callback)
fitImport += fits
IDs = []
for i, fit in enumerate(fitImport):
fit.character = self.character
fit.damagePattern = self.pattern
fit.targetResists = self.targetResists
eos.db.save(fit)
IDs.append(fit.ID)
if callback: # Pulse
wx.CallAfter(callback, "Saving fit\n%d/%d"%(i+1, len(fitImport)))
return fits
def importFitFromBuffer(self, bufferStr, activeFit=None):

View File

@@ -19,7 +19,6 @@
import re
import xml.dom
import json
from eos.types import State, Slot, Module, Cargo, Fit, Ship, Drone, Implant, Booster
import service
@@ -36,20 +35,20 @@ class Port(object):
"""Service which houses all import/export format functions"""
@classmethod
def importAuto(cls, string, sourceFileName=None, activeFit=None):
def importAuto(cls, string, sourceFileName=None, activeFit=None, callback=None):
# Get first line and strip space symbols of it to avoid possible detection errors
firstLine = re.split("[\n\r]+", string.strip(), maxsplit=1)[0]
firstLine = firstLine.strip()
# If XML-style start of tag encountered, detect as XML
if re.match("<", firstLine):
return "XML", cls.importXml(string)
return "XML", cls.importXml(string, callback)
# If we've got source file name which is used to describe ship name
# and first line contains something like [setup name], detect as eft config file
if re.match("\[.*\]", firstLine) and sourceFileName is not None:
shipName = sourceFileName.rsplit('.')[0]
return "EFT Config", cls.importEftCfg(shipName, string)
return "EFT Config", cls.importEftCfg(shipName, string, callback)
# If no file is specified and there's comma between brackets,
# consider that we have [ship, setup name] and detect like eft export format
@@ -201,7 +200,7 @@ class Port(object):
return fit
@staticmethod
def importEftCfg(shipname, contents):
def importEftCfg(shipname, contents, callback=None):
"""Handle import from EFT config store file"""
# Check if we have such ship in database, bail if we don't
@@ -347,6 +346,9 @@ class Port(object):
f.modules.append(m)
# Append fit to list of fits
fits.append(f)
if callback:
wx.CallAfter(callback, None)
# Skip fit silently if we get an exception
except Exception:
pass
@@ -354,14 +356,16 @@ class Port(object):
return fits
@staticmethod
def importXml(text):
def importXml(text, callback=None):
sMkt = service.Market.getInstance()
doc = xml.dom.minidom.parseString(text.encode("utf-8"))
fittings = doc.getElementsByTagName("fittings").item(0)
fittings = fittings.getElementsByTagName("fitting")
fits = []
for fitting in fittings:
for i, fitting in enumerate(fittings):
print "fitting"
f = Fit()
f.name = fitting.getAttribute("name")
# <localized hint="Maelstrom">Maelstrom</localized>
@@ -403,6 +407,8 @@ class Port(object):
except KeyboardInterrupt:
continue
fits.append(f)
if callback:
wx.CallAfter(callback, None)
return fits