Skip to content

Commit 605e441

Browse files
authored
Merge pull request #6 from lizlooney/pr_multiple_ports
Make it work on systemcore
2 parents 30e08e7 + d8d2533 commit 605e441

File tree

4 files changed

+31
-33
lines changed

4 files changed

+31
-33
lines changed

external_samples/component.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class InvalidPortException(Exception):
3333
class Component(ABC):
3434
def __init__(self, port : Port, expectedType : PortType):
3535
"""This has the port it is attached to, and the expected type of the port"""
36-
if port.get_type != expectedType:
36+
if port.get_type() != expectedType:
3737
raise InvalidPortException
3838

3939
self.port = port

external_samples/port.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ class PortType(Enum):
3232
USB_HUB = BASE_COMPOUND + 1
3333
EXPANSION_HUB_MOTOR = BASE_COMPOUND + 2
3434
EXPANSION_HUB_SERVO = BASE_COMPOUND + 3
35-
35+
3636
class Port:
37-
def __init__(self, port_type : PortType = None, location : int = None, port1 : type[Self] = None, port2 : type[Self] = None):
37+
def __init__(self, port_type: PortType = None, location: int = None, port1: type[Self] = None, port2: type[Self] = None):
3838
"""
3939
Create a port that can be either simple (type + location) or compound (two other ports).
40-
40+
4141
Args:
4242
port_type: PortType or CompoundPortType for this port
4343
location: int location for simple ports
@@ -46,7 +46,7 @@ def __init__(self, port_type : PortType = None, location : int = None, port1 : t
4646
"""
4747
if port1 is not None and port2 is not None:
4848
# Compound port
49-
if port_type < PortType.BASE_COMPOUND:
49+
if port_type.value < PortType.BASE_COMPOUND.value:
5050
raise ValueError("Port must be of a compound type")
5151
self.is_compound = True
5252
self.type = port_type
@@ -55,33 +55,29 @@ def __init__(self, port_type : PortType = None, location : int = None, port1 : t
5555
self.location = None
5656
elif port_type is not None and location is not None:
5757
# Simple port
58-
if port_type > PortType.BASE_COMPOUND:
59-
raise ValueError("Port must be of a simple type")
58+
if port_type.value > PortType.BASE_COMPOUND.value:
59+
raise ValueError("Port must be of a simple type")
6060
self.is_compound = False
6161
self.type = port_type
6262
self.location = location
6363
self.port1 = None
6464
self.port2 = None
6565
else:
6666
raise ValueError("Port must be either simple (type + location) or compound (port1 + port2)")
67-
68-
def get_all_ports(self):
67+
68+
def get_all_ports(self) -> list[tuple[PortType, int]]:
6969
"""Return a list of all simple ports contained in this port."""
7070
if self.is_compound:
7171
return self.port1.get_all_ports() + self.port2.get_all_ports()
7272
else:
7373
return [(self.type, self.location)]
74-
7574

76-
def get_type(self):
77-
"""This returns the type if it is simple or the type of the last one in the chain if compound"""
78-
if self.is_compound:
79-
return self.port2.get_type()
80-
else:
81-
return self.type
75+
def get_type(self) -> PortType:
76+
"""Returns the port type"""
77+
return self.type
8278

83-
def __str__(self):
79+
def __str__(self) -> str:
8480
if self.is_compound:
8581
return f"CompoundPort({self.type}: {self.port1}, {self.port2})"
8682
else:
87-
return f"Port({self.type}, {self.location})"
83+
return f"Port({self.type}, {self.location})"

src/blocks/mrc_port.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ export const setup = function () {
111111

112112
export const pythonFromBlock = function (
113113
block: PortBlock,
114-
_generator: ExtendedPythonGenerator) {
115-
const ports: string[] = [];
114+
generator: ExtendedPythonGenerator) {
115+
generator.addImport('port');
116116

117+
const ports: string[] = [];
118+
117119
for (let i = 0; i < block.inputList.length; i++) {
118120
const input = block.inputList[i];
119121
if (input.name.startsWith('PORT_')) {
@@ -123,32 +125,32 @@ export const pythonFromBlock = function (
123125
}
124126
}
125127
}
126-
let code = `Port(port_type = PortType.${block.portType_}, `;
128+
let code = `port.Port(port_type = port.PortType.${block.portType_}, `;
127129

128-
if ( ports.length === 1 ) {
130+
if (ports.length === 1) {
129131
code += `location = ${ports[0]})`;
130-
}
131-
else if ( ports.length === 2 ) {
132+
133+
} else if (ports.length === 2) {
132134
let port1Type = 'UNKNOWN';
133135
let port2Type = 'UNKNOWN';
134136

135-
switch(block.portType_){
137+
switch (block.portType_) {
136138
case 'USB_HUB':
137139
port1Type = 'USB_PORT';
138140
port2Type = 'USB_PORT';
139141
break;
140142
case 'EXPANSION_HUB_MOTOR':
141143
port1Type = 'USB_PORT';
142-
port2Type = 'MOTOR_PORT';
144+
port2Type = 'EXPANSION_HUB_MOTOR_PORT';
143145
break;
144146
case 'EXPANSION_HUB_SERVO':
145147
port1Type = 'USB_PORT';
146-
port2Type = 'SERVO_PORT';
148+
port2Type = 'EXPANSION_HUB_SERVO_PORT';
147149
break;
148150
}
149151

150-
code += `\\ \n${_generator.INDENT}port1 = Port(port_type = PortType.${port1Type}, location = ${ports[0]}), `;
151-
code += `\\ \n${_generator.INDENT}port2 = Port(port_type = PortType.${port2Type}, location = ${ports[1]}))`;
152+
code += `\\ \n${generator.INDENT}port1 = port.Port(port_type = port.PortType.${port1Type}, location = ${ports[0]}), `;
153+
code += `\\ \n${generator.INDENT}port2 = port.Port(port_type = port.PortType.${port2Type}, location = ${ports[1]}))`;
152154
}
153155

154156
return [code, Order.ATOMIC];

src/blocks/utils/generated/external_samples_data.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@
12261226
"declaringClassName": "port.Port",
12271227
"functionName": "__init__",
12281228
"returnType": "port.Port",
1229-
"tooltip": "\nCreate a port that can be either simple (type + location) or compound (two other ports).\n\nArgs:\n port_type: PortType or CompoundPortType for this port\n location: int location for simple ports\n port1: First Port for compound ports\n port2: Second Port for compound ports\n"
1229+
"tooltip": "\n Create a port that can be either simple (type + location) or compound (two other ports).\n\n Args:\n port_type: PortType or CompoundPortType for this port\n location: int location for simple ports\n port1: First Port for compound ports\n port2: Second Port for compound ports\n "
12301230
}
12311231
],
12321232
"enums": [],
@@ -1241,7 +1241,7 @@
12411241
],
12421242
"declaringClassName": "port.Port",
12431243
"functionName": "get_all_ports",
1244-
"returnType": "None",
1244+
"returnType": "list[tuple[port.PortType, int]]",
12451245
"tooltip": "Return a list of all simple ports contained in this port."
12461246
},
12471247
{
@@ -1254,8 +1254,8 @@
12541254
],
12551255
"declaringClassName": "port.Port",
12561256
"functionName": "get_type",
1257-
"returnType": "None",
1258-
"tooltip": "This returns the type if it is simple or the type of the last one in the chain if compound"
1257+
"returnType": "port.PortType",
1258+
"tooltip": "Returns the port type"
12591259
}
12601260
],
12611261
"instanceVariables": [],

0 commit comments

Comments
 (0)