1515
1616# @fileoverview This is a class to handle port types
1717# @author [email protected] (Alan Smith) 18+ from abc import ABC , abstractmethod
1819from enum import Enum
1920from typing import Self
2021
@@ -33,51 +34,61 @@ class PortType(Enum):
3334 EXPANSION_HUB_MOTOR = BASE_COMPOUND + 2 # This is combination expansion hub and motor
3435 EXPANSION_HUB_SERVO = BASE_COMPOUND + 3 # This is combination expansion hub and servo
3536
36- class Port :
37- def __init__ (self , port_type : PortType = None , location : int = None , port1 : type [Self ] = None , port2 : type [Self ] = None ):
37+ class Port (ABC ):
38+ """Abstract base class for all port types."""
39+
40+ def __init__ (self , port_type : PortType ):
41+ self .type = port_type
42+
43+ @abstractmethod
44+ def get_all_ports (self ) -> list [tuple [PortType , int ]]:
45+ """Return a list of all simple ports contained in this port."""
46+ pass
47+
48+ def get_type (self ) -> PortType :
49+ """Returns the port type"""
50+ return self .type
51+
52+ class SimplePort (Port ):
53+ def __init__ (self , port_type : PortType , location : int ):
54+ """
55+ Create a simple port with a type and location.
56+
57+ Args:
58+ port_type: PortType for this port (must be a simple type)
59+ location: int location for this port
60+ """
61+ if port_type .value >= PortType .BASE_COMPOUND .value :
62+ raise ValueError ("Port must be of a simple type" )
63+ super ().__init__ (port_type )
64+ self .location = location
65+
66+ def get_all_ports (self ) -> list [tuple [PortType , int ]]:
67+ """Return a list containing this simple port."""
68+ return [(self .type , self .location )]
69+
70+ def __str__ (self ) -> str :
71+ return f"SimplePort({ self .type } , { self .location } )"
72+
73+ class CompoundPort (Port ):
74+ def __init__ (self , port_type : PortType , port1 : Port , port2 : Port ):
3875 """
39- Create a port that can be either simple (type + location) or compound ( two other ports) .
76+ Create a compound port from two other ports.
4077
4178 Args:
42- port_type: PortType or CompoundPortType for this port
43- location: int location for simple ports
79+ port_type: PortType for this port (must be a compound type)
4480 port1: First Port for compound ports
4581 port2: Second Port for compound ports
4682 """
47- if port1 is not None and port2 is not None :
48- # Compound port
49- if port_type .value < PortType .BASE_COMPOUND .value :
50- raise ValueError ("Port must be of a compound type" )
51- self .is_compound = True
52- self .type = port_type
53- self .port1 = port1
54- self .port2 = port2
55- self .location = None
56- elif port_type is not None and location is not None :
57- # Simple port
58- if port_type .value > PortType .BASE_COMPOUND .value :
59- raise ValueError ("Port must be of a simple type" )
60- self .is_compound = False
61- self .type = port_type
62- self .location = location
63- self .port1 = None
64- self .port2 = None
65- else :
66- raise ValueError ("Port must be either simple (type + location) or compound (port1 + port2)" )
83+ if port_type .value < PortType .BASE_COMPOUND .value :
84+ raise ValueError ("Port must be of a compound type" )
85+ super ().__init__ (port_type )
86+ self .port1 = port1
87+ self .port2 = port2
6788
6889 def get_all_ports (self ) -> list [tuple [PortType , int ]]:
69- """Return a list of all simple ports contained in this port."""
70- if self .is_compound :
71- return self .port1 .get_all_ports () + self .port2 .get_all_ports ()
72- else :
73- return [(self .type , self .location )]
74-
75- def get_type (self ) -> PortType :
76- """Returns the port type"""
77- return self .type
90+ """Return a list of all simple ports contained in this compound port."""
91+ return self .port1 .get_all_ports () + self .port2 .get_all_ports ()
7892
7993 def __str__ (self ) -> str :
80- if self .is_compound :
81- return f"CompoundPort({ self .type } : { self .port1 } , { self .port2 } )"
82- else :
83- return f"Port({ self .type } , { self .location } )"
94+ return f"CompoundPort({ self .type } : { self .port1 } , { self .port2 } )"
0 commit comments