MCPcopy Index your code
hub / github.com/pyfa-org/Pyfa / CalcReplaceLocalModuleCommand

Class CalcReplaceLocalModuleCommand

gui/fitCommands/calc/module/localReplace.py:12–101  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10
11
12class CalcReplaceLocalModuleCommand(wx.Command):
13
14 def __init__(self, fitID, position, newModInfo, unloadInvalidCharges=False, ignoreRestrictions=False, recalc=True):
15 wx.Command.__init__(self, True, 'Replace Module')
16 self.fitID = fitID
17 self.position = position
18 self.newModInfo = newModInfo
19 self.oldModInfo = None
20 self.unloadInvalidCharges = unloadInvalidCharges
21 self.ignoreRestrictions = ignoreRestrictions
22 self.recalc = recalc
23 self.savedStateCheckChanges = None
24 self.unloadedCharge = None
25
26 def Do(self):
27 pyfalog.debug('Doing replacement of local module at position {} to {} on fit {}'.format(self.position, self.newModInfo, self.fitID))
28 self.unloadedCharge = False
29 sFit = Fit.getInstance()
30 fit = sFit.getFit(self.fitID)
31 oldMod = fit.modules[self.position]
32 if not oldMod.isEmpty:
33 self.oldModInfo = ModuleInfo.fromModule(oldMod)
34 if self.newModInfo == self.oldModInfo:
35 return False
36 newMod = self.newModInfo.toModule(fallbackState=activeStateLimit(self.newModInfo.itemID))
37 if newMod is None:
38 return False
39 if newMod.slot != oldMod.slot:
40 return False
41 # Dummy it out in case the next bit fails
42 fit.modules.replace(self.position, newMod)
43 if newMod not in fit.modules:
44 pyfalog.warning('Failed to replace in list')
45 self.Undo()
46 return False
47 if self.recalc:
48 # Need to flush because checkStates sometimes relies on module->fit
49 # relationship via .owner attribute, which is handled by SQLAlchemy
50 eos.db.flush()
51 sFit.recalc(fit)
52 self.savedStateCheckChanges = sFit.checkStates(fit, newMod)
53 if not self.ignoreRestrictions and not newMod.fits(fit):
54 pyfalog.warning('Module does not fit')
55 self.Undo()
56 return False
57 if not self.ignoreRestrictions and not newMod.isValidCharge(newMod.charge):
58 if self.unloadInvalidCharges:
59 newMod.charge = None
60 self.unloadedCharge = True
61 else:
62 pyfalog.warning('Invalid charge')
63 self.Undo()
64 return False
65 return True
66
67 def Undo(self):
68 pyfalog.debug('Undoing replacement of local module at position {} to {} on fit {}'.format(self.position, self.newModInfo, self.fitID))
69 sFit = Fit.getInstance()

Callers 8

DoMethod · 0.90
DoMethod · 0.90
DoMethod · 0.90
DoMethod · 0.90
DoMethod · 0.90
DoMethod · 0.90
UndoMethod · 0.85
DoMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected