Skip to content

Commit 0e4513f

Browse files
committed
Typing adjustments
1 parent 6d4c96e commit 0e4513f

5 files changed

Lines changed: 19 additions & 12 deletions

File tree

upython/backend/boards/cpython/json_config_storage.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
from protocols.config_storage import ConfigStorage
44

55
class JsonConfigStorage(ConfigStorage):
6+
CONFIG_FILE_PATH = os.environ.get('CONFIG_FILE', 'config.json')
67
def __init__(self):
7-
self.config_file = os.environ.get('CONFIG_FILE', 'config.json')
8+
self.config_file = JsonConfigStorage.CONFIG_FILE_PATH
89

910
def load_str(self, key: str, maximum_size: int=1024, default: str='') -> str | None:
1011
return self.__load_json_config().get(key, default)

upython/backend/boards/simulator/wifi_manager.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
import asyncio
2-
from config import Config, WiFi
2+
from protocols.config import Config
3+
from boards.cpython.json_config_storage import JsonConfigStorage
34
import json
45

56

67
class SimulatorWiFiManager:
78
def __init__(self, config: Config):
89
self.config = config
9-
with open(self.config.storage.config_file, 'r') as config_file:
10+
with open(JsonConfigStorage.CONFIG_FILE_PATH, 'r') as config_file:
1011
self.stations = json.load(config_file)['stations']
1112
print(self.stations)
1213

1314
def scan(self) -> list[str]:
15+
return [station['ssid'] for station in self._sorted_available_stations()]
16+
17+
def _sorted_available_stations(self) -> list[dict]:
1418
available_stations = [st for st in self.stations if st.get('sim_available', True)]
1519
return sorted(available_stations, key=lambda st: st.get('sim_rssi', 0))
1620

1721

1822
async def connect_stations(self, connect_ap_on_failure: bool = True):
1923
print('Connecting to WiFi stations...')
2024
print(f'Configured stations: {self.config.stations}')
21-
available_stations = self.scan()
25+
available_stations = self._sorted_available_stations()
2226
if not available_stations:
2327
print('No stations configured, skipping connection')
2428
print('Starting AP mode...')

upython/backend/protocols/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from protocols.config_storage import ConfigStorage
2+
from typing import Protocol
23

34

4-
class WiFi:
5+
class WiFi(Protocol):
56
def __init__(self, ssid: str, psk: str | None = None):
67
pass
78

@@ -38,7 +39,7 @@ def to_json_list(cls, json: list) -> list[dict]:
3839
raise NotImplementedError()
3940

4041

41-
class Config:
42+
class Config(Protocol):
4243
def __init__(self, storage: ConfigStorage):
4344
pass
4445

upython/backend/protocols/gpio.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import typing
22

3-
class GPIO:
3+
class GPIO(typing.Protocol):
44
def __init__(self, pin_name: str):
55
pass
66

7-
class ButtonPin(GPIO):
7+
class ButtonPin(GPIO, typing.Protocol):
88
def __init__(self, pin_name: str):
99
super().__init__(pin_name)
1010

@@ -15,15 +15,15 @@ def value(self) -> bool:
1515
def on_level_changed(self, callback: typing.Callable[[bool], None]) -> None:
1616
raise NotImplementedError()
1717

18-
class AnalogInputPin(GPIO):
18+
class AnalogInputPin(GPIO, typing.Protocol):
1919
def __init__(self, pin_name: str):
2020
super().__init__(pin_name)
2121

2222
@property
2323
def value(self) -> float:
2424
raise NotImplementedError()
2525

26-
class DigitalOutputPin(GPIO):
26+
class DigitalOutputPin(GPIO, typing.Protocol):
2727
def __init__(self, pin_name: str):
2828
super().__init__(pin_name)
2929

@@ -40,7 +40,7 @@ def is_pwm(self):
4040
return False
4141

4242

43-
class PWMPin(GPIO):
43+
class PWMPin(GPIO, typing.Protocol):
4444
def __init__(self, pin_name: str):
4545
super().__init__(pin_name)
4646

upython/backend/protocols/wifi_manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from protocols.config import Config
2+
from typing import Protocol
23

34

4-
class WiFiManager:
5+
class WiFiManager(Protocol):
56
def __init__(self, config: Config):
67
pass
78

0 commit comments

Comments
 (0)