Skip to content

Commit 232a375

Browse files
Fix an issue with transport classes kwargs
1 parent 6c7f42b commit 232a375

3 files changed

Lines changed: 43 additions & 45 deletions

File tree

obdii/connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __init__(
6161
Whether to set up the root logger.
6262
6363
**kwargs: :class:`dict`
64-
Additional keyword arguments to pass to the transport's connect method. Only used if `auto_connect` is True.
64+
Additional keyword arguments forwarded to the transport's constructor.
6565
"""
6666
self.transport = self._resolve_transport(transport, **kwargs)
6767

@@ -131,7 +131,7 @@ def connect(self, **kwargs) -> None:
131131
Parameters
132132
----------
133133
**kwargs
134-
Additional parameters forwarded to the transport's `connect()` method.
134+
Additional keyword arguments forwarded to the transport's connect method.
135135
"""
136136
_log.info(f"Attempting to connect to {repr(self.transport)}.")
137137
try:

obdii/transports/transport_port.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,44 @@
1-
from typing import Optional
1+
from typing import Optional, Dict, Any
22

33
from serial import Serial
44

55
from .transport_base import TransportBase
66

77
from ..basetypes import MISSING
8-
from ..utils.helper import override_class_attributes
98

109

1110
class TransportPort(TransportBase):
12-
def __init__(self, **kwargs) -> None:
13-
self.overridable_attributes = {
14-
"port": MISSING,
15-
"baudrate": 38400,
16-
"timeout": 5.0,
17-
"write_timeout": 3.0,
11+
def __init__(
12+
self,
13+
port: str = MISSING,
14+
baudrate: int = 38400,
15+
timeout: float = 5.0,
16+
write_timeout: float = 3.0,
17+
**kwargs,
18+
) -> None:
19+
self.config: Dict[str, Any] = {
20+
"port": port,
21+
"baudrate": baudrate,
22+
"timeout": timeout,
23+
"write_timeout": write_timeout,
24+
**kwargs,
1825
}
1926

20-
self.port: str
21-
self.baudrate: int
22-
self.timeout: float
23-
self.write_timeout: float
24-
2527
self.serial_conn: Optional[Serial] = None
2628

27-
override_class_attributes(self, self.overridable_attributes, **kwargs)
28-
29-
if self.port is MISSING:
29+
if port is MISSING:
3030
raise ValueError("Port must be specified for TransportPort.")
3131

3232
def __repr__(self) -> str:
33-
return f"<TransportPort {self.port} at {self.baudrate} baud>"
33+
return f"<TransportPort {self.config.get('port')} at {self.config.get('baudrate')} baud>"
3434

3535
def is_connected(self) -> bool:
3636
return self.serial_conn is not None and self.serial_conn.is_open
3737

3838
def connect(self, **kwargs) -> None:
39-
override_class_attributes(self, self.overridable_attributes, True, **kwargs)
39+
self.config.update(kwargs)
4040

41-
self.serial_conn = Serial(
42-
port=self.port,
43-
baudrate=self.baudrate,
44-
timeout=self.timeout,
45-
write_timeout=self.write_timeout,
46-
**kwargs,
47-
)
41+
self.serial_conn = Serial(**self.config)
4842

4943
def close(self) -> None:
5044
if self.serial_conn and self.serial_conn.is_open:

obdii/transports/transport_wifi.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
from socket import AF_INET, SOCK_STREAM, error as s_error, socket
2-
from typing import Optional, Union
2+
from typing import Optional, Union, Dict, Any
33

44
from .transport_base import TransportBase
55

66
from ..basetypes import MISSING
7-
from ..utils.helper import override_class_attributes
87

98

109
class TransportWifi(TransportBase):
11-
def __init__(self, **kwargs) -> None:
12-
self.overridable_attributes = {
13-
"address": MISSING,
14-
"port": MISSING,
15-
"timeout": 5.0,
10+
def __init__(
11+
self,
12+
address: str = MISSING,
13+
port: Union[str, int] = MISSING,
14+
timeout: float = 5.0,
15+
**kwargs,
16+
) -> None:
17+
self.config: Dict[str, Any] = {
18+
"address": address,
19+
"port": port,
20+
"timeout": timeout,
21+
**kwargs,
1622
}
1723

18-
self.address: str
19-
self.port: Union[str, int]
20-
self.timeout: float
21-
2224
self.socket_conn: Optional[socket] = None
2325

24-
override_class_attributes(self, self.overridable_attributes, **kwargs)
25-
26-
if self.address is MISSING or self.port is MISSING:
26+
if address is MISSING or port is MISSING:
2727
raise ValueError(
2828
"Both address and port must be specified for TransportWifi."
2929
)
3030

3131
def __repr__(self) -> str:
32-
return f"<TransportWifi {self.address}:{self.port}>"
32+
return f"<TransportWifi {self.config.get('address')}:{self.config.get('port')}>"
3333

3434
def is_connected(self) -> bool:
3535
if self.socket_conn is None:
@@ -41,11 +41,15 @@ def is_connected(self) -> bool:
4141
return False
4242

4343
def connect(self, **kwargs) -> None:
44-
override_class_attributes(self, self.overridable_attributes, True, **kwargs)
44+
self.config.update(kwargs)
45+
46+
timeout = self.config.get("timeout")
47+
address = self.config.get("address")
48+
port = self.config.get("port")
4549

4650
self.socket_conn = socket(AF_INET, SOCK_STREAM)
47-
self.socket_conn.settimeout(self.timeout)
48-
self.socket_conn.connect((self.address, int(self.port)))
51+
self.socket_conn.settimeout(timeout)
52+
self.socket_conn.connect((address, port))
4953

5054
def close(self) -> None:
5155
if self.socket_conn:

0 commit comments

Comments
 (0)