Skip to content

Commit 1d5f142

Browse files
committed
Changed code that looks at port argument names in pseudo constructors to look for names that match PortType enum values (case insensitive).
Updated python code so that port argument names match the PortType values.
1 parent b8d5344 commit 1d5f142

File tree

5 files changed

+26
-34
lines changed

5 files changed

+26
-34
lines changed

external_samples/rev_touch_sensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def periodic(self) -> None:
5555

5656
# Alternative constructor to create an instance from a smart io port
5757
@classmethod
58-
def from_io_port(cls: type[Self], io_port: int) -> Self:
59-
return cls([(PortType.SMART_IO_PORT, io_port)])
58+
def from_smart_io_port(cls: type[Self], smart_io_port: int) -> Self:
59+
return cls([(PortType.SMART_IO_PORT, smart_io_port)])
6060

6161
# Component specific methods
6262

external_samples/smart_motor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ def get_connection_port_type(self) -> list[PortType]:
4545
def periodic(self) -> None:
4646
pass
4747

48-
# Alternative constructor to create an instance from a motor port
48+
# Alternative constructor to create an instance from a smart motor port
4949
@classmethod
50-
def from_motor_port(cls: type[Self], motor_port: int) -> Self:
51-
return cls([(PortType.SMART_MOTOR_PORT, motor_port)])
50+
def from_smart_motor_port(cls: type[Self], smart_motor_port: int) -> Self:
51+
return cls([(PortType.SMART_MOTOR_PORT, smart_motor_port)])
5252

5353
# Component specific methods
5454

external_samples/spark_mini.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ def get_connection_port_type(self) -> list[PortType]:
6161
def periodic(self) -> None:
6262
pass
6363

64-
# Alternative constructor to create an instance from a motor port
64+
# Alternative constructor to create an instance from a smart motor port
6565
@classmethod
66-
def from_motor_port(cls: type[Self], motor_port: int) -> Self:
67-
return cls([(PortType.SMART_MOTOR_PORT, motor_port)])
66+
def from_smart_motor_port(cls: type[Self], smart_motor_port: int) -> Self:
67+
return cls([(PortType.SMART_MOTOR_PORT, smart_motor_port)])
6868

6969
# Component specific methods
7070

src/blocks/mrc_component.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,6 @@ export const OUTPUT_NAME = 'mrc_component';
3737
export const FIELD_NAME = 'NAME';
3838
export const FIELD_TYPE = 'TYPE';
3939

40-
// key is a regex pattern that matches a function argument name, value is type of mrc_port block to use.
41-
// TODO: Improve these regex pattern.
42-
// The types here match the PortType values in external_samples/component.py.
43-
const RECOGNIZED_PORT_ARG_NAME_PATTERNS: { [key: string]: string } = {
44-
'can_port': 'CAN_PORT',
45-
'io_port': 'SMART_IO_PORT',
46-
'smartIO_port': 'SMART_IO_PORT',
47-
'smart_io_port': 'SMART_IO_PORT',
48-
'motor_port': 'SMART_MOTOR_PORT',
49-
'smart_motor_port': 'SMART_MOTOR_PORT',
50-
'servo_port': 'SERVO_PORT',
51-
'i2c_port': 'I2C_PORT',
52-
'usb_port': 'USB_PORT',
53-
};
54-
55-
5640
export type ConstructorArg = {
5741
name: string,
5842
type: string,
@@ -247,10 +231,18 @@ function createComponentBlock(
247231
}
248232

249233
function getPortTypeForArgument(argName: string): string | null {
250-
for (const pattern in RECOGNIZED_PORT_ARG_NAME_PATTERNS) {
251-
if (new RegExp(pattern, 'i').test(argName)) {
252-
return RECOGNIZED_PORT_ARG_NAME_PATTERNS[pattern];
253-
}
234+
// TODO(lizlooney): Currently the JSON for component.PortType is ClassData
235+
// instead of EnumData. When it is fixed to be EnumData, this code will need
236+
// to be updated.
237+
238+
const argNameLower = argName.toLowerCase();
239+
const classData = getClassData('component.PortType');
240+
if (classData) {
241+
for (const varData of classData.classVariables) {
242+
if (argNameLower === varData.name.toLowerCase()) {
243+
return varData.name;
244+
}
245+
}
254246
}
255247
return null;
256248
}

src/blocks/utils/generated/external_samples_data.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -717,12 +717,12 @@
717717
"args": [
718718
{
719719
"defaultValue": null,
720-
"name": "io_port",
720+
"name": "smart_io_port",
721721
"type": "int"
722722
}
723723
],
724724
"declaringClassName": "rev_touch_sensor.RevTouchSensor",
725-
"functionName": "from_io_port",
725+
"functionName": "from_smart_io_port",
726726
"returnType": "rev_touch_sensor.RevTouchSensor",
727727
"tooltip": ""
728728
}
@@ -1140,12 +1140,12 @@
11401140
"args": [
11411141
{
11421142
"defaultValue": null,
1143-
"name": "motor_port",
1143+
"name": "smart_motor_port",
11441144
"type": "int"
11451145
}
11461146
],
11471147
"declaringClassName": "smart_motor.SmartMotor",
1148-
"functionName": "from_motor_port",
1148+
"functionName": "from_smart_motor_port",
11491149
"returnType": "smart_motor.SmartMotor",
11501150
"tooltip": ""
11511151
}
@@ -1603,12 +1603,12 @@
16031603
"args": [
16041604
{
16051605
"defaultValue": null,
1606-
"name": "motor_port",
1606+
"name": "smart_motor_port",
16071607
"type": "int"
16081608
}
16091609
],
16101610
"declaringClassName": "spark_mini.SparkMiniComponent",
1611-
"functionName": "from_motor_port",
1611+
"functionName": "from_smart_motor_port",
16121612
"returnType": "spark_mini.SparkMiniComponent",
16131613
"tooltip": ""
16141614
}

0 commit comments

Comments
 (0)