Move out mutated module parsing from EFT to separate module
This commit is contained in:
@@ -22,7 +22,7 @@ import re
|
||||
|
||||
from logbook import Logger
|
||||
|
||||
from eos.db.gamedata.queries import getAttributeInfo, getDynamicItem
|
||||
from eos.db.gamedata.queries import getDynamicItem
|
||||
from eos.saveddata.cargo import Cargo
|
||||
from eos.saveddata.citadel import Citadel
|
||||
from eos.saveddata.booster import Booster
|
||||
@@ -34,7 +34,7 @@ from eos.saveddata.ship import Ship
|
||||
from eos.saveddata.fit import Fit
|
||||
from service.fit import Fit as svcFit
|
||||
from service.market import Market
|
||||
from service.port.muta import exportMutant
|
||||
from service.port.muta import parseMutant, renderMutant
|
||||
from service.port.shared import IPortUser, fetchItem, processing_notify
|
||||
from enum import Enum
|
||||
|
||||
@@ -157,7 +157,7 @@ def exportEft(fit, options):
|
||||
if mutants and options & Options.MUTATIONS.value:
|
||||
for mutantReference in sorted(mutants):
|
||||
mutant = mutants[mutantReference]
|
||||
mutationLines.append(exportMutant(mutant, firstPrefix='[{}] '.format(mutantReference), prefix=' '))
|
||||
mutationLines.append(renderMutant(mutant, firstPrefix='[{}] '.format(mutantReference), prefix=' '))
|
||||
if mutationLines:
|
||||
sections.append('\n'.join(mutationLines))
|
||||
|
||||
@@ -497,59 +497,48 @@ def _importPrepareString(eftString):
|
||||
return lines
|
||||
|
||||
|
||||
mutantHeaderPattern = re.compile('^\[(?P<ref>\d+)\](?P<tail>.*)')
|
||||
|
||||
|
||||
def _importGetMutationData(lines):
|
||||
data = {}
|
||||
# Format: {ref: [lines]}
|
||||
mutaLinesMap = {}
|
||||
currentMutaRef = None
|
||||
currentMutaLines = []
|
||||
consumedIndices = set()
|
||||
for i in range(len(lines)):
|
||||
line = lines[i]
|
||||
m = re.match('^\[(?P<ref>\d+)\]', line)
|
||||
|
||||
def completeMutaLines():
|
||||
if currentMutaRef is not None and currentMutaLines:
|
||||
mutaLinesMap[currentMutaRef] = currentMutaLines
|
||||
|
||||
for i, line in enumerate(lines):
|
||||
m = re.match(mutantHeaderPattern, line)
|
||||
# Start and reset at header line
|
||||
if m:
|
||||
ref = int(m.group('ref'))
|
||||
# Attempt to apply mutation is useless w/o mutaplasmid, so skip it
|
||||
# altogether if we have no info on it
|
||||
try:
|
||||
mutaName = lines[i + 1]
|
||||
except IndexError:
|
||||
continue
|
||||
else:
|
||||
consumedIndices.add(i)
|
||||
consumedIndices.add(i + 1)
|
||||
# Get custom attribute values
|
||||
mutaAttrs = {}
|
||||
try:
|
||||
mutaAttrsLine = lines[i + 2]
|
||||
except IndexError:
|
||||
pass
|
||||
else:
|
||||
consumedIndices.add(i + 2)
|
||||
pairs = [p.strip() for p in mutaAttrsLine.split(',')]
|
||||
for pair in pairs:
|
||||
try:
|
||||
attrName, value = pair.split(' ')
|
||||
except ValueError:
|
||||
continue
|
||||
try:
|
||||
value = float(value)
|
||||
except (ValueError, TypeError):
|
||||
continue
|
||||
attrInfo = getAttributeInfo(attrName.strip())
|
||||
if attrInfo is None:
|
||||
continue
|
||||
mutaAttrs[attrInfo.ID] = value
|
||||
mutaItem = fetchItem(mutaName)
|
||||
if mutaItem is None:
|
||||
continue
|
||||
data[ref] = (mutaItem, mutaAttrs)
|
||||
# If we got here, we have seen at least correct reference line and
|
||||
# mutaplasmid name line
|
||||
i += 2
|
||||
# Bonus points for seeing correct attrs line. Worst case we
|
||||
# will have to scan it once again
|
||||
if mutaAttrs:
|
||||
i += 1
|
||||
# Cleanup the lines from mutaplasmid info
|
||||
completeMutaLines()
|
||||
currentMutaRef = int(m.group('ref'))
|
||||
currentMutaLines = []
|
||||
currentMutaLines.append(m.group('tail'))
|
||||
consumedIndices.add(i)
|
||||
# Reset at blank line
|
||||
elif not line:
|
||||
completeMutaLines()
|
||||
currentMutaRef = None
|
||||
currentMutaLines = []
|
||||
elif currentMutaRef is not None:
|
||||
currentMutaLines.append(line)
|
||||
consumedIndices.add(i)
|
||||
else:
|
||||
completeMutaLines()
|
||||
# Clear mutant info from source
|
||||
for i in sorted(consumedIndices, reverse=True):
|
||||
del lines[i]
|
||||
# Run parsing
|
||||
data = {}
|
||||
for ref, mutaLines in mutaLinesMap.items():
|
||||
_, mutaType, mutaAttrs = parseMutant(mutaLines)
|
||||
data[ref] = (mutaType, mutaAttrs)
|
||||
return data
|
||||
|
||||
|
||||
|
||||
@@ -18,13 +18,12 @@
|
||||
# =============================================================================
|
||||
|
||||
|
||||
from eos.db.gamedata.queries import getAttributeInfo, getDynamicItem
|
||||
from eos.saveddata.module import Module
|
||||
from eos.db.gamedata.queries import getAttributeInfo
|
||||
from gui.utils.numberFormatter import roundToPrec
|
||||
from service.port.shared import fetchItem
|
||||
|
||||
|
||||
def exportMutant(mutant, firstPrefix='', prefix=''):
|
||||
def renderMutant(mutant, firstPrefix='', prefix=''):
|
||||
exportLines = []
|
||||
mutatedAttrs = {}
|
||||
for attrID, mutator in mutant.mutators.items():
|
||||
@@ -40,28 +39,28 @@ def exportMutant(mutant, firstPrefix='', prefix=''):
|
||||
return '\n'.join(exportLines)
|
||||
|
||||
|
||||
def importMutant(lines):
|
||||
def parseMutant(lines):
|
||||
# Fetch base item type
|
||||
try:
|
||||
baseName = lines[0]
|
||||
except IndexError:
|
||||
return None
|
||||
baseType = fetchItem(baseName.strip())
|
||||
if baseType is None:
|
||||
return None, None, {}
|
||||
# Fetch mutaplasmid item type and actual item
|
||||
try:
|
||||
mutaName = lines[1]
|
||||
except IndexError:
|
||||
return _makeModule(baseType)
|
||||
mutaplasmidType = fetchItem(mutaName.strip())
|
||||
if mutaplasmidType is None:
|
||||
return _makeModule(baseType)
|
||||
mutaplasmid = getDynamicItem(mutaplasmidType.ID)
|
||||
module = _makeModule(mutaplasmid.resultingItem, baseType, mutaplasmid)
|
||||
return baseType, None, {}
|
||||
mutaType = fetchItem(mutaName.strip())
|
||||
if mutaType is None:
|
||||
return baseType, None, {}
|
||||
# Process mutated attribute values
|
||||
try:
|
||||
mutaAttrsLine = lines[2]
|
||||
except IndexError:
|
||||
return module
|
||||
return baseType, mutaType, {}
|
||||
mutaAttrs = {}
|
||||
pairs = [p.strip() for p in mutaAttrsLine.split(',')]
|
||||
for pair in pairs:
|
||||
@@ -77,15 +76,4 @@ def importMutant(lines):
|
||||
if attrInfo is None:
|
||||
continue
|
||||
mutaAttrs[attrInfo.ID] = value
|
||||
for attrID, mutator in module.mutators.items():
|
||||
if attrID in mutaAttrs:
|
||||
mutator.value = mutaAttrs[attrID]
|
||||
return module
|
||||
|
||||
|
||||
def _makeModule(item, *args, **kwargs):
|
||||
try:
|
||||
return Module(item, *args, **kwargs)
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
return baseType, mutaType, mutaAttrs
|
||||
|
||||
@@ -37,7 +37,7 @@ from service.port.esi import exportESI, importESI
|
||||
from service.port.multibuy import exportMultiBuy
|
||||
from service.port.shared import IPortUser, UserCancelException, processing_notify
|
||||
from service.port.xml import importXml, exportXml
|
||||
from service.port.muta import importMutant
|
||||
from service.port.muta import parseMutant
|
||||
|
||||
|
||||
pyfalog = Logger(__name__)
|
||||
@@ -232,7 +232,7 @@ class Port(object):
|
||||
return "EFT", (cls.importEft(string),)
|
||||
|
||||
try:
|
||||
return "Abyssal", (importMutant(string.split("\n")),)
|
||||
return "Abyssal", (parseMutant(string.split("\n")),)
|
||||
except:
|
||||
# Use DNA format for all other cases
|
||||
return "DNA", (cls.importDna(string),)
|
||||
|
||||
Reference in New Issue
Block a user