Skip to content

Commit 9284a1f

Browse files
authored
Merge pull request #30 from intercreate/feature/use-timeout
Feature/use timeout
2 parents efcd19e + ce5c21c commit 9284a1f

7 files changed

+23
-18
lines changed

envr-default

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ PYTHON_VENV=.venv
77
[ADD_TO_PATH]
88

99
[ALIASES]
10-
lint=black --check . && isort --check-only . && flake8 . && mypy .
10+
lint=black --check --diff --color . && isort --check-only --diff . && flake8 . && mypy .
1111
test=coverage erase && pytest --cov
1212
build=python portable.py
13+
format=black . && isort .

smpmgr/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ def get_smpclient(options: Options) -> SMPClient:
7070
return get_custom_smpclient(options, SMPClient)
7171

7272

73-
async def connect_with_spinner(smpclient: SMPClient) -> None:
73+
async def connect_with_spinner(smpclient: SMPClient, timeout_s: float) -> None:
7474
"""Spin while connecting to the SMP Server; raises `typer.Exit` if connection fails."""
7575
with Progress(
7676
SpinnerColumn(), TextColumn("[progress.description]{task.description}")
7777
) as progress:
7878
connect_task_description = f"Connecting to {smpclient._address}..."
7979
connect_task = progress.add_task(description=connect_task_description, total=None)
8080
try:
81-
await asyncio.wait_for(smpclient.connect(), timeout=2)
81+
await asyncio.wait_for(smpclient.connect(), timeout=timeout_s)
8282
progress.update(
8383
connect_task, description=f"{connect_task_description} OK", completed=True
8484
)

smpmgr/file_management.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def get_supported_hash_types(ctx: typer.Context) -> None:
4040
smpclient = get_smpclient(options)
4141

4242
async def f() -> None:
43-
await connect_with_spinner(smpclient)
43+
await connect_with_spinner(smpclient, options.timeout)
4444

4545
r = await smp_request(smpclient, options, SupportedFileHashChecksumTypes(), "Waiting for supported hash types...") # type: ignore # noqa
4646

@@ -64,7 +64,7 @@ def get_hash(
6464
smpclient = get_smpclient(options)
6565

6666
async def f() -> None:
67-
await connect_with_spinner(smpclient)
67+
await connect_with_spinner(smpclient, options.timeout)
6868

6969
r = await smp_request(smpclient, options, FileHashChecksum(name=file), "Waiting for hash...") # type: ignore # noqa
7070

@@ -86,7 +86,7 @@ def read_size(
8686
smpclient = get_smpclient(options)
8787

8888
async def f() -> None:
89-
await connect_with_spinner(smpclient)
89+
await connect_with_spinner(smpclient, options.timeout)
9090

9191
r = await smp_request(smpclient, options, FileStatus(name=file), "Waiting for file size...") # type: ignore # noqa
9292

@@ -141,10 +141,11 @@ def upload(
141141
) -> None:
142142
"""Upload a file."""
143143

144-
smpclient = get_smpclient(cast(Options, ctx.obj))
144+
options = cast(Options, ctx.obj)
145+
smpclient = get_smpclient(options)
145146

146147
async def f() -> None:
147-
await connect_with_spinner(smpclient)
148+
await connect_with_spinner(smpclient, options.timeout)
148149
with open(file, "rb") as f:
149150
await upload_with_progress_bar(smpclient, f, destination)
150151

@@ -159,10 +160,11 @@ def download(
159160
) -> None:
160161
"""Download a file."""
161162

162-
smpclient = get_smpclient(cast(Options, ctx.obj))
163+
options = cast(Options, ctx.obj)
164+
smpclient = get_smpclient(options)
163165

164166
async def f() -> None:
165-
await connect_with_spinner(smpclient)
167+
await connect_with_spinner(smpclient, options.timeout)
166168
with destination.open("wb") as dest_f:
167169
file_data = await smpclient.download_file(file)
168170
dest_f.write(file_data)

smpmgr/image_management.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def state_read(ctx: typer.Context) -> None:
3737
smpclient = get_smpclient(options)
3838

3939
async def f() -> None:
40-
await connect_with_spinner(smpclient)
40+
await connect_with_spinner(smpclient, options.timeout)
4141

4242
r = await smp_request(smpclient, options, ImageStatesRead(), "Waiting for image states...") # type: ignore # noqa
4343

@@ -104,10 +104,11 @@ def upload(
104104
logger.exception("Inspection of FW image failed")
105105
raise typer.Exit(code=1)
106106

107-
smpclient = get_smpclient(cast(Options, ctx.obj))
107+
options = cast(Options, ctx.obj)
108+
smpclient = get_smpclient(options)
108109

109110
async def f() -> None:
110-
await connect_with_spinner(smpclient)
111+
await connect_with_spinner(smpclient, options.timeout)
111112
with open(file, "rb") as f:
112113
await upload_with_progress_bar(smpclient, f, slot)
113114

smpmgr/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def upgrade(
109109
smpclient = get_smpclient(options)
110110

111111
async def f() -> None:
112-
await connect_with_spinner(smpclient)
112+
await connect_with_spinner(smpclient, options.timeout)
113113

114114
with open(file, "rb") as f:
115115
await upload_with_progress_bar(smpclient, f, slot)

smpmgr/os_management.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def echo(ctx: typer.Context, message: str) -> None:
1818
smpclient = get_smpclient(options)
1919

2020
async def f() -> None:
21-
await connect_with_spinner(smpclient)
21+
await connect_with_spinner(smpclient, options.timeout)
2222
r = await smp_request(smpclient, options, EchoWrite(d=message)) # type: ignore
2323
print(r)
2424

@@ -33,7 +33,7 @@ def reset(ctx: typer.Context) -> None:
3333
smpclient = get_smpclient(options)
3434

3535
async def f() -> None:
36-
await connect_with_spinner(smpclient)
36+
await connect_with_spinner(smpclient, options.timeout)
3737
r = await smp_request(smpclient, options, ResetWrite()) # type: ignore
3838
print(r)
3939

smpmgr/user/intercreate.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ def upload(
6969
) -> None:
7070
"""Upload data to custom image slots, like a secondary MCU or external storage."""
7171

72-
smpclient = get_custom_smpclient(cast(Options, ctx.obj), ic.ICUploadClient)
72+
options = cast(Options, ctx.obj)
73+
smpclient = get_custom_smpclient(options, ic.ICUploadClient)
7374

7475
async def f() -> None:
75-
await connect_with_spinner(smpclient)
76+
await connect_with_spinner(smpclient, options.timeout)
7677
with open(file, "rb") as f:
7778
await upload_with_progress_bar(smpclient, f, image)
7879

0 commit comments

Comments
 (0)