From 3ae312db377fc5b37465e42e8a6db0af6314d35c Mon Sep 17 00:00:00 2001 From: blitzmann Date: Mon, 21 Mar 2016 23:43:45 -0400 Subject: [PATCH] Import / export implants sets --- eos/saveddata/implantSet.py | 21 ++++++++++------ gui/setEditor.py | 18 ++++++++------ service/implantSet.py | 49 +++++++++++++++++++++++++++++++++++++ service/market.py | 36 ++++++++++++++++----------- 4 files changed, 95 insertions(+), 29 deletions(-) diff --git a/eos/saveddata/implantSet.py b/eos/saveddata/implantSet.py index 34b4de6cf..8e495c5ea 100644 --- a/eos/saveddata/implantSet.py +++ b/eos/saveddata/implantSet.py @@ -29,15 +29,20 @@ class ImplantSet(object): def implants(self): return self.__implants - - EXPORT_FORMAT = "ImplantSet = %s,%d,%d,%d,%d\n" @classmethod - def exportPatterns(cls, *patterns): - out = "# Exported from pyfa\n#\n" - out += "# Values are in following format:\n" - out += "# DamageProfile = [name],[EM amount],[Thermal amount],[Kinetic amount],[Explosive amount]\n\n" - for dp in patterns: - out += cls.EXPORT_FORMAT % (dp.name, dp.emAmount, dp.thermalAmount, dp.kineticAmount, dp.explosiveAmount) + def exportSets(cls, *sets): + out = "# Exported from pyfa\n#\n" \ + "# Values are in following format:\n" \ + "# [Implant Set name]\n" \ + "# [Implant name]\n" \ + "# [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() diff --git a/gui/setEditor.py b/gui/setEditor.py index e9a0b8ce1..eed2afa1e 100644 --- a/gui/setEditor.py +++ b/gui/setEditor.py @@ -22,7 +22,10 @@ from gui.bitmapLoader import BitmapLoader from gui.builtinViews.implantEditor import BaseImplantEditorView import service 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): def __init__(self, parent): @@ -326,14 +329,15 @@ class ImplantSetEditorDlg(wx.Dialog): text = fromClipboard() if text: - sTR = service.TargetResists.getInstance() + sIS = service.ImplantSets.getInstance() try: - sTR.importPatterns(text) + sIS.importSets(text) self.stNotice.SetLabel("Patterns successfully imported from clipboard") self.showInput(False) - except service.targetResists.ImportError, e: + except ImportError, e: self.stNotice.SetLabel(str(e)) except Exception, e: + logging.exception("Unhandled Exception") self.stNotice.SetLabel("Could not import from clipboard: unknown errors") finally: self.updateChoices() @@ -343,6 +347,6 @@ class ImplantSetEditorDlg(wx.Dialog): def exportPatterns(self, event): "Event fired when export to clipboard button is clicked" - sTR = service.TargetResists.getInstance() - toClipboard( sTR.exportPatterns() ) - self.stNotice.SetLabel("Patterns exported to clipboard") + sIS = service.ImplantSets.getInstance() + toClipboard(sIS.exportSets()) + self.stNotice.SetLabel("Sets exported to clipboard") diff --git a/service/implantSet.py b/service/implantSet.py index 1b1b500a5..4a517f0ca 100644 --- a/service/implantSet.py +++ b/service/implantSet.py @@ -20,6 +20,7 @@ import eos.db import eos.types import copy +import service.market class ImportError(Exception): pass @@ -73,3 +74,51 @@ class ImplantSets(): def saveChanges(self, 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) + diff --git a/service/market.py b/service/market.py index b8c0ab341..8d8fe3810 100644 --- a/service/market.py +++ b/service/market.py @@ -29,12 +29,15 @@ import eos.types from service.settings import SettingsProvider, NetworkSettings import service import service.conversions as conversions +import logging try: from collections import OrderedDict except ImportError: from utils.compat import OrderedDict +logger = logging.getLogger(__name__) + # Event which tells threads dependent on Market that it's initialized mktRdy = threading.Event() @@ -349,20 +352,25 @@ class Market(): def getItem(self, identity, *args, **kwargs): """Get item by its ID or name""" - if isinstance(identity, eos.types.Item): - item = identity - elif isinstance(identity, int): - item = eos.db.getItem(identity, *args, **kwargs) - elif isinstance(identity, basestring): - # We normally lookup with string when we are using import/export - # features. Check against overrides - identity = conversions.all.get(identity, identity) - item = eos.db.getItem(identity, *args, **kwargs) - elif isinstance(identity, float): - id = int(identity) - item = eos.db.getItem(id, *args, **kwargs) - else: - raise TypeError("Need Item object, integer, float or string as argument") + try: + if isinstance(identity, eos.types.Item): + item = identity + elif isinstance(identity, int): + item = eos.db.getItem(identity, *args, **kwargs) + elif isinstance(identity, basestring): + # We normally lookup with string when we are using import/export + # features. Check against overrides + identity = conversions.all.get(identity, identity) + item = eos.db.getItem(identity, *args, **kwargs) + elif isinstance(identity, float): + id = int(identity) + item = eos.db.getItem(id, *args, **kwargs) + else: + raise TypeError("Need Item object, integer, float or string as argument") + except: + logger.error("Could not get item: %s", identity) + raise + return item def getGroup(self, identity, *args, **kwargs):