• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SPF-OST / pytrnsys_gui / 11662562960

29 Oct 2024 03:09PM UTC coverage: 67.508% (-0.08%) from 67.591%
11662562960

push

github

web-flow
Merge pull request #564 from SPF-OST/560-black-change-line-length-to-pep8-standard-of-79-and-check-ci-reaction

changed line length in black to 79

1054 of 1475 new or added lines in 174 files covered. (71.46%)

150 existing lines in 74 files now uncovered.

10399 of 15404 relevant lines covered (67.51%)

0.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

35.06
/trnsysGUI/GenericBlock.py
1
# pylint: skip-file
2
# type: ignore
3

4
import pathlib as _pl
1✔
5
import typing as _tp
1✔
6

7
import PyQt5.QtWidgets as _qtw
1✔
8

9
import trnsysGUI.blockItemGraphicItemMixins as _bmx
1✔
10
import trnsysGUI.blockItemHasInternalPiping as _biip
1✔
11
import trnsysGUI.createSinglePipePortItem as _cspi
1✔
12
import trnsysGUI.imageAccessor as _ia
1✔
13
import trnsysGUI.images as _img
1✔
14
import trnsysGUI.internalPiping as _ip
1✔
15
import trnsysGUI.massFlowSolver.networkModel as _mfn
1✔
16

17

18
class GenericBlock(
1✔
19
    _biip.BlockItemHasInternalPiping, _bmx.RasterImageBlockItemMixin
20
):
21
    def __init__(self, trnsysType: str, editor, displayName: str) -> None:
1✔
22
        _biip.BlockItemHasInternalPiping.__init__(
1✔
23
            self, trnsysType, editor, displayName
24
        )
25
        _bmx.RasterImageBlockItemMixin.__init__(self)
1✔
26

27
        self.inputs.append(_cspi.createSinglePipePortItem("i", self))
1✔
28
        self.outputs.append(_cspi.createSinglePipePortItem("o", self))
1✔
29

30
        self.childIds = []
1✔
31
        self.childIds.append(self.trnsysId)
1✔
32

33
        self._imageAccessor = _img.GENERIC_BLOCK_PNG
1✔
34

35
        # Disallow adding port pairs later, because the trnsysIDs of the generated port pairs have to be
36
        # consecutive to be correctly printed out in the export
37
        self.isSet = True
1✔
38

39
        self.changeSize()
1✔
40

41
    def getDisplayName(self) -> str:
1✔
42
        return self.displayName
×
43

44
    def _getImageAccessor(self) -> _tp.Optional[_ia.ImageAccessorBase]:
1✔
45
        return _img.GENERIC_BLOCK_PNG
1✔
46

47
    def changeSize(self):
1✔
48
        w = self.w
1✔
49
        h = self.h
1✔
50
        delta = 4
1✔
51

52
        """ Resize block function """
1✔
53

54
        # Limit the block size:
55
        if h < 20:
1✔
56
            h = 20
×
57
        if w < 40:
1✔
58
            w = 40
×
59
        # center label:
60
        rect = self.label.boundingRect()
1✔
61
        lw, lh = rect.width(), rect.height()
1✔
62
        lx = (w - lw) / 2
1✔
63
        self.label.setPos(lx, h)
1✔
64

65
        self.outputs[0].setPos(2 * delta + w, h - 2 * delta)
1✔
66
        self.inputs[0].setPos(2 * delta + w, 2 * delta)
1✔
67

68
        return w, h
1✔
69

70
    def getPairNb(self, side):
1✔
71
        res = 0
×
72
        for i in self.inputs:
×
73
            if i.side == side:
×
74
                res += 1
×
75

NEW
76
        self.logger.debug(
×
77
            "there are " + str(res) + " pairs on the side " + str(side)
78
        )
UNCOV
79
        return res
×
80

81
    def addPortDlg(self):
1✔
82
        self.editor.showGenericPortPairDlg(self)
×
83

84
    def addPort(self, io, relH):
1✔
85
        self.logger.debug(io)
×
86
        self.logger.debug(relH)
×
87

88
    def setImage(self):
1✔
89
        self._image = self._imageAccessor.image()
×
90

91
    def changeImage(self):
1✔
92
        name = str(self.pickImage().resolve())
×
93
        if name[-3:] == "png" or name[-3:] == "svg":
×
94
            self.setImageSource(name)
×
95
            self.setImage()
×
96
        else:
97
            self.logger.debug("No image picked, name is " + name)
×
98

99
    def setImageSource(self, name):
1✔
100
        self._imageAccessor = _ia.createForFile(_pl.Path(name))
×
101

102
    def pickImage(self):
1✔
NEW
103
        return _pl.Path(
×
104
            _qtw.QFileDialog.getOpenFileName(
105
                self.editor, filter="*.png *.svg"
106
            )[0]
107
        )
108

109
    def _addChildContextMenuActions(self, contextMenu: _qtw.QMenu) -> None:
1✔
110
        super()._addChildContextMenuActions(contextMenu)
×
111

112
        setImageAction = contextMenu.addAction("Set image")
×
113
        setImageAction.triggered.connect(self.changeImage)
×
114

115
        if not self.isSet:
×
116
            addPortAction = contextMenu.addAction("Add port")
×
117
            addPortAction.triggered.connect(self.addPortDlg)
×
118

119
    def encode(self):
1✔
120
        _, dct = super().encode()
×
121

122
        dct["PortPairsNb"] = [self.getPairNb(i) for i in range(4)]
×
123
        dct["Imagesource"] = self._imageAccessor.getResourcePath()
×
124

125
        dictName = "Block-"
×
126
        return dictName, dct
×
127

128
    def decode(self, i, resBlockList):
1✔
129
        self._removePortPairs()
×
130

131
        numberOfPortPairsBySide = i["PortPairsNb"]
×
132
        self._addPortPairs(numberOfPortPairsBySide)
×
133

134
        self._imageAccessor = _ia.createFromResourcePath(i["Imagesource"])
×
135
        self.setImage()
×
136

137
        super().decode(i, resBlockList)
×
138

139
    def _addPortPairs(self, numberOfPortPairsBySide):
1✔
140
        for side in range(3):
×
141
            numberOfPortPairsToAdd = numberOfPortPairsBySide[side]
×
142
            for _ in range(numberOfPortPairsToAdd):
×
143
                self.addPortPair(side)
×
144

145
    def _removePortPairs(self):
1✔
146
        assert len(self.inputs) == len(self.outputs)
×
147
        numberOfPortPairs = len(self.inputs)
×
148
        for portPairIndex in range(numberOfPortPairs):
×
149
            self.removePortPair(portPairIndex)
×
150

151
    def addPortPair(self, side):
1✔
152
        h = self.h
×
153
        w = self.w
×
154
        delta = 4
×
155
        self.logger.debug("side is " + str(side))
×
156
        self.inputs.append(_cspi.createSinglePipePortItem("i", self))
×
157
        self.outputs.append(_cspi.createSinglePipePortItem("o", self))
×
158
        # Allocate id
159
        self.childIds.append(self.editor.idGen.getTrnsysID())
×
160

161
        portNb = [0, 0, 0, 0]
×
162
        for i in self.inputs:
×
163
            if i.side == 0:
×
NEW
164
                distBetweenPorts = (self.h - 4 * delta) / (
×
165
                    2 * self.getPairNb(0) - 1
166
                )
NEW
167
                self.logger.debug(
×
168
                    "distance betw ports " + str(distBetweenPorts)
169
                )
170
                i.setPos(-2 * delta, 2 * delta + distBetweenPorts * portNb[0])
×
171
                portNb[0] += 1
×
172

NEW
173
                self.outputs[self.inputs.index(i)].setPos(
×
174
                    -2 * delta, 2 * delta + distBetweenPorts * portNb[0]
175
                )
176
                portNb[0] += 1
×
177
            elif i.side == 1:
×
NEW
178
                distBetweenPorts = (self.w - 4 * delta) / (
×
179
                    2 * self.getPairNb(1) - 1
180
                )
181
                i.setPos(2 * delta + distBetweenPorts * portNb[1], -2 * delta)
×
182
                portNb[1] += 1
×
183

NEW
184
                self.outputs[self.inputs.index(i)].setPos(
×
185
                    2 * delta + distBetweenPorts * portNb[1], -2 * delta
186
                )
UNCOV
187
                portNb[1] += 1
×
188

189
            elif i.side == 2:
×
190
                self.logger.debug("side == 2")
×
NEW
191
                distBetweenPorts = (self.h - 4 * delta) / (
×
192
                    2 * self.getPairNb(2) - 1
193
                )
NEW
194
                self.logger.debug(
×
195
                    "side 2 dist betw ports is " + str(distBetweenPorts)
196
                )
NEW
197
                i.setPos(
×
198
                    2 * delta + w, 2 * delta + distBetweenPorts * portNb[2]
199
                )
200
                self.logger.debug(2 * delta + distBetweenPorts * portNb[2])
×
201
                portNb[2] += 1
×
202

NEW
203
                self.outputs[self.inputs.index(i)].setPos(
×
204
                    2 * delta + w, 2 * delta + distBetweenPorts * portNb[2]
205
                )
206
                self.logger.debug(2 * delta + distBetweenPorts * portNb[2])
×
207
                portNb[2] += 1
×
208

209
            else:
NEW
210
                distBetweenPorts = (self.w - 4 * delta) / (
×
211
                    2 * self.getPairNb(3) - 1
212
                )
NEW
213
                self.logger.debug(
×
214
                    "distance betw ports " + str(distBetweenPorts)
215
                )
NEW
216
                i.setPos(
×
217
                    2 * delta + distBetweenPorts * portNb[3], 2 * delta + h
218
                )
UNCOV
219
                portNb[3] += 1
×
220

NEW
221
                self.outputs[self.inputs.index(i)].setPos(
×
222
                    2 * delta + distBetweenPorts * portNb[3], 2 * delta + h
223
                )
UNCOV
224
                portNb[3] += 1
×
225

226
    def removePortPairOnSide(self, side):
1✔
227
        for i in self.inputs:
×
228
            if i.side == side:
×
229
                self.removePortPair(self.inputs.index(i))
×
230
                return
×
231

232
    def removePortPair(self, n):
1✔
233
        self.inputs.remove(self.inputs[n])
×
234
        self.outputs.remove(self.outputs[n])
×
235

236
    def getInternalPiping(self) -> _ip.InternalPiping:
1✔
237
        assert len(self.inputs) == len(self.outputs)
×
238

239
        pipes = []
×
240
        portItems = {}
×
NEW
241
        for i, (graphicalInputPort, graphicalOutputPort) in enumerate(
×
242
            zip(self.inputs, self.outputs)
243
        ):
244
            inputPort = _mfn.PortItem("In", _mfn.PortItemDirection.INPUT)
×
245
            outputPort = _mfn.PortItem("Out", _mfn.PortItemDirection.OUTPUT)
×
246
            pipe = _mfn.Pipe(inputPort, outputPort, name=f"Side{i}")
×
247

248
            pipes.append(pipe)
×
249
            portItems[inputPort] = graphicalInputPort
×
250
            portItems[outputPort] = graphicalOutputPort
×
251

252
        return _ip.InternalPiping(pipes, portItems)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc