| 119 | |
| 120 | |
| 121 | class HandledModuleList(HandledList): |
| 122 | |
| 123 | def append(self, mod): |
| 124 | emptyPosition = float("Inf") |
| 125 | for i in range(len(self)): |
| 126 | currMod = self[i] |
| 127 | if currMod.isEmpty and not mod.isEmpty and currMod.slot == mod.slot: |
| 128 | currPos = mod.position or i |
| 129 | if currPos < emptyPosition: |
| 130 | emptyPosition = currPos |
| 131 | |
| 132 | if emptyPosition < len(self): |
| 133 | mod.position = emptyPosition |
| 134 | self.__toModule(emptyPosition, mod) |
| 135 | if mod.isInvalid: |
| 136 | self.__toDummy(mod.position) |
| 137 | else: |
| 138 | self.appendIgnoreEmpty(mod) |
| 139 | |
| 140 | @collection.appender |
| 141 | def appendIgnoreEmpty(self, mod): |
| 142 | mod.position = len(self) |
| 143 | super().append(mod) |
| 144 | if mod.isInvalid: |
| 145 | self.remove(mod) |
| 146 | |
| 147 | def replace(self, idx, mod): |
| 148 | try: |
| 149 | oldMod = self[idx] |
| 150 | except IndexError: |
| 151 | return |
| 152 | self.__toModule(idx, mod) |
| 153 | if mod.isInvalid: |
| 154 | self.__toModule(idx, oldMod) |
| 155 | |
| 156 | def replaceRackPosition(self, rackPosition, mod): |
| 157 | listPositions = [] |
| 158 | for currPos in range(len(self)): |
| 159 | currMod = self[currPos] |
| 160 | if currMod.slot == mod.slot: |
| 161 | listPositions.append(currPos) |
| 162 | listPositions.sort() |
| 163 | try: |
| 164 | modListPosition = listPositions[rackPosition] |
| 165 | except IndexError: |
| 166 | self.appendIgnoreEmpty(mod) |
| 167 | else: |
| 168 | oldMod = self[modListPosition] |
| 169 | if mod.isEmpty: |
| 170 | self.__toDummy(modListPosition) |
| 171 | else: |
| 172 | self.__toModule(modListPosition, mod) |
| 173 | # If new module cannot be appended, restore old state |
| 174 | if mod.isInvalid: |
| 175 | if oldMod.isEmpty: |
| 176 | self.__toDummy(modListPosition) |
| 177 | else: |
| 178 | self.__toModule(modListPosition, oldMod) |