Import / export implants sets

This commit is contained in:
blitzmann
2016-03-21 23:43:45 -04:00
parent 94f73241ea
commit 3ae312db37
4 changed files with 95 additions and 29 deletions

View File

@@ -29,15 +29,20 @@ class ImplantSet(object):
def implants(self): def implants(self):
return self.__implants return self.__implants
EXPORT_FORMAT = "ImplantSet = %s,%d,%d,%d,%d\n"
@classmethod @classmethod
def exportPatterns(cls, *patterns): def exportSets(cls, *sets):
out = "# Exported from pyfa\n#\n" out = "# Exported from pyfa\n#\n" \
out += "# Values are in following format:\n" "# Values are in following format:\n" \
out += "# DamageProfile = [name],[EM amount],[Thermal amount],[Kinetic amount],[Explosive amount]\n\n" "# [Implant Set name]\n" \
for dp in patterns: "# [Implant name]\n" \
out += cls.EXPORT_FORMAT % (dp.name, dp.emAmount, dp.thermalAmount, dp.kineticAmount, dp.explosiveAmount) "# [Implant name]\n" \
"# ...\n\n"
for set in sets:
out += "[{}]\n".format(set.name)
for implant in set.implants:
out += "{}\n".format(implant.item.name)
out += "\n"
return out.strip() return out.strip()

View File

@@ -22,7 +22,10 @@ from gui.bitmapLoader import BitmapLoader
from gui.builtinViews.implantEditor import BaseImplantEditorView from gui.builtinViews.implantEditor import BaseImplantEditorView
import service import service
from gui.utils.clipboard import toClipboard, fromClipboard from gui.utils.clipboard import toClipboard, fromClipboard
from service.targetResists import ImportError from service.implantSet import ImportError
import logging
logger = logging.getLogger(__name__)
class ImplantSetEditor(BaseImplantEditorView): class ImplantSetEditor(BaseImplantEditorView):
def __init__(self, parent): def __init__(self, parent):
@@ -326,14 +329,15 @@ class ImplantSetEditorDlg(wx.Dialog):
text = fromClipboard() text = fromClipboard()
if text: if text:
sTR = service.TargetResists.getInstance() sIS = service.ImplantSets.getInstance()
try: try:
sTR.importPatterns(text) sIS.importSets(text)
self.stNotice.SetLabel("Patterns successfully imported from clipboard") self.stNotice.SetLabel("Patterns successfully imported from clipboard")
self.showInput(False) self.showInput(False)
except service.targetResists.ImportError, e: except ImportError, e:
self.stNotice.SetLabel(str(e)) self.stNotice.SetLabel(str(e))
except Exception, e: except Exception, e:
logging.exception("Unhandled Exception")
self.stNotice.SetLabel("Could not import from clipboard: unknown errors") self.stNotice.SetLabel("Could not import from clipboard: unknown errors")
finally: finally:
self.updateChoices() self.updateChoices()
@@ -343,6 +347,6 @@ class ImplantSetEditorDlg(wx.Dialog):
def exportPatterns(self, event): def exportPatterns(self, event):
"Event fired when export to clipboard button is clicked" "Event fired when export to clipboard button is clicked"
sTR = service.TargetResists.getInstance() sIS = service.ImplantSets.getInstance()
toClipboard( sTR.exportPatterns() ) toClipboard(sIS.exportSets())
self.stNotice.SetLabel("Patterns exported to clipboard") self.stNotice.SetLabel("Sets exported to clipboard")

View File

@@ -20,6 +20,7 @@
import eos.db import eos.db
import eos.types import eos.types
import copy import copy
import service.market
class ImportError(Exception): class ImportError(Exception):
pass pass
@@ -73,3 +74,51 @@ class ImplantSets():
def saveChanges(self, s): def saveChanges(self, s):
eos.db.save(s) eos.db.save(s)
def importSets(self, text):
sMkt = service.Market.getInstance()
lines = text.splitlines()
newSets = []
errors = 0
current = None
lookup = {}
for i, line in enumerate(lines):
line = line.strip()
try:
if line == '' or line[0] == "#": # comments / empty string
continue
if line[:1] == "[" and line[-1:] == "]":
current = eos.types.ImplantSet(line[1:-1])
newSets.append(current)
else:
item = sMkt.getItem(line)
current.implants.append(eos.types.Implant(item))
except:
errors += 1
continue
for set in self.getImplantSetList():
lookup[set.name] = set
for set in newSets:
if set.name in lookup:
match = lookup[set.name]
for implant in set.implants:
match.implants.append(eos.types.Implant(implant.item))
else:
eos.db.save(set)
eos.db.commit()
lenImports = len(newSets)
if lenImports == 0:
raise ImportError("No patterns found for import")
if errors > 0:
raise ImportError("%d sets imported from clipboard; %d errors"%(lenImports, errors))
def exportSets(self):
patterns = self.getImplantSetList()
patterns.sort(key=lambda p: p.name)
return eos.types.ImplantSet.exportSets(*patterns)

View File

@@ -29,12 +29,15 @@ import eos.types
from service.settings import SettingsProvider, NetworkSettings from service.settings import SettingsProvider, NetworkSettings
import service import service
import service.conversions as conversions import service.conversions as conversions
import logging
try: try:
from collections import OrderedDict from collections import OrderedDict
except ImportError: except ImportError:
from utils.compat import OrderedDict from utils.compat import OrderedDict
logger = logging.getLogger(__name__)
# Event which tells threads dependent on Market that it's initialized # Event which tells threads dependent on Market that it's initialized
mktRdy = threading.Event() mktRdy = threading.Event()
@@ -349,20 +352,25 @@ class Market():
def getItem(self, identity, *args, **kwargs): def getItem(self, identity, *args, **kwargs):
"""Get item by its ID or name""" """Get item by its ID or name"""
if isinstance(identity, eos.types.Item): try:
item = identity if isinstance(identity, eos.types.Item):
elif isinstance(identity, int): item = identity
item = eos.db.getItem(identity, *args, **kwargs) elif isinstance(identity, int):
elif isinstance(identity, basestring): item = eos.db.getItem(identity, *args, **kwargs)
# We normally lookup with string when we are using import/export elif isinstance(identity, basestring):
# features. Check against overrides # We normally lookup with string when we are using import/export
identity = conversions.all.get(identity, identity) # features. Check against overrides
item = eos.db.getItem(identity, *args, **kwargs) identity = conversions.all.get(identity, identity)
elif isinstance(identity, float): item = eos.db.getItem(identity, *args, **kwargs)
id = int(identity) elif isinstance(identity, float):
item = eos.db.getItem(id, *args, **kwargs) id = int(identity)
else: item = eos.db.getItem(id, *args, **kwargs)
raise TypeError("Need Item object, integer, float or string as argument") else:
raise TypeError("Need Item object, integer, float or string as argument")
except:
logger.error("Could not get item: %s", identity)
raise
return item return item
def getGroup(self, identity, *args, **kwargs): def getGroup(self, identity, *args, **kwargs):