Skip to content

Commit 7133704

Browse files
committed
[datatransfer] added upload command
1 parent 7103dd7 commit 7133704

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed

src/instrumentman/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ def cli_download() -> None:
6464
"""Download data from the instrument."""
6565

6666

67+
@cli.group("upload") # type: ignore[misc]
68+
def cli_upload() -> None:
69+
"""Upload data to the instrument."""
70+
71+
6772
cli.add_command(morse.cli)
6873
cli.add_command(terminal.cli)
6974
cli_measure.add_command(setmeasurement.cli_measure)
@@ -81,3 +86,4 @@ def cli_download() -> None:
8186
cli_list.add_command(jobs.cli_list)
8287
cli_download.add_command(filetransfer.cli_download)
8388
cli_download.add_command(datatransfer.cli_download)
89+
cli_upload.add_command(datatransfer.cli_upload)

src/instrumentman/datatransfer/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from click_extra import (
44
extra_command,
5+
argument,
56
option,
67
File
78
)
@@ -43,3 +44,23 @@ def cli_download(**kwargs: Any) -> None:
4344
from .app import main_download
4445

4546
main_download(**kwargs)
47+
48+
49+
@extra_command(
50+
"data",
51+
params=None,
52+
context_settings={"auto_envvar_prefix": None}
53+
) # type: ignore[misc]
54+
@com_port_argument()
55+
@argument(
56+
"file",
57+
help="data file to upload",
58+
type=File("rt", encoding="ascii")
59+
)
60+
@com_baud_option(1200)
61+
@com_timeout_option()
62+
def cli_upload(**kwargs: Any) -> None:
63+
"""Upload ASCII data to the instrument."""
64+
from .app import main_upload
65+
66+
main_upload(**kwargs)

src/instrumentman/datatransfer/app.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from io import BufferedWriter
1+
from io import BufferedWriter, TextIOWrapper
22

33
from serial import SerialTimeoutException
4-
from click_extra import echo
4+
from click_extra import echo, progressbar
55
from geocompy.communication import open_serial
66

77
from ..utils import echo_green, echo_red, echo_yellow
@@ -32,15 +32,43 @@ def main_download(
3232
output.write(data + eol_bytes)
3333

3434
if data == eof_bytes and autoclose:
35-
echo_green("Transfer finished (end-of-file)")
35+
echo_green("Download finished (end-of-file)")
3636
return
3737
except SerialTimeoutException:
3838
if started and autoclose:
39-
echo_green("Transfer finished (timeout)")
39+
echo_green("Download finished (timeout)")
4040
return
4141
except KeyboardInterrupt:
42-
echo_yellow("Transfer stopped manually")
42+
echo_yellow("Download stopped manually")
4343
return
4444
except Exception as e:
45-
echo_red(f"Transfer interrupted by error ({e})")
45+
echo_red(f"Download interrupted by error ({e})")
4646
return
47+
48+
49+
def main_upload(
50+
port: str,
51+
file: TextIOWrapper,
52+
baud: int = 1200,
53+
timeout: int = 15
54+
) -> None:
55+
with open_serial(
56+
port,
57+
speed=baud,
58+
timeout=timeout
59+
) as com:
60+
try:
61+
count = 0
62+
with progressbar(
63+
file,
64+
label="Uploading data",
65+
item_show_func=lambda x: f"{count} line(s)"
66+
) as bar:
67+
for line in bar:
68+
com.send(line)
69+
count += 1
70+
except Exception as e:
71+
echo_red(f"Upload interrupted by error ({e})")
72+
return
73+
74+
echo_green("Upload finished")

src/instrumentman/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ def com_timeout_option(
5454
)
5555

5656

57-
def com_baud_option() -> Callable[[F], F]:
57+
def com_baud_option(
58+
default: int = 9600
59+
) -> Callable[[F], F]:
5860
return option(
5961
"-b",
6062
"--baud",
@@ -75,7 +77,7 @@ def com_baud_option() -> Callable[[F], F]:
7577
]
7678
),
7779
callback=lambda ctx, param, value: int(value),
78-
default="9600"
80+
default=str(default)
7981
)
8082

8183

0 commit comments

Comments
 (0)