Skip to content

Commit f6434b6

Browse files
committed
Docstrings and linting.
1 parent 64cdabc commit f6434b6

File tree

5 files changed

+69
-18
lines changed

5 files changed

+69
-18
lines changed

datashuttle/datashuttle_class.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ def _transfer_specific_file_or_folder(
830830
stdout, stderr, errors = rclone.parse_rclone_copy_output(
831831
top_level_folder, output
832832
)
833-
rclone.log_rclone_output_python_api(stdout, stderr)
833+
rclone.log_stdout_stderr_python_api(stdout, stderr)
834834
rclone.log_rclone_copy_errors_api(errors)
835835

836836
return errors

datashuttle/tui/screens/modal_dialogs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ def __init__(
6666
border_color
6767
Color of the MessageBox border (e.g. green if the message is positive).
6868
69+
height
70+
The height of the messagebox.
71+
72+
width
73+
The width of the messagebox.
74+
6975
"""
7076
super(MessageBox, self).__init__()
7177

datashuttle/utils/custom_types.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
from typing import Any, Literal, Tuple, TypeAlias, TypedDict
1+
from __future__ import annotations
22

3-
DisplayMode: TypeAlias = Literal["error", "warn", "print"]
3+
from typing import Any, Literal, Tuple, TypedDict
44

5-
TopLevelFolder: TypeAlias = Literal["rawdata", "derivatives"]
5+
DisplayMode = Literal["error", "warn", "print"]
66

7-
OverwriteExistingFiles: TypeAlias = Literal[
8-
"never", "always", "if_source_newer"
9-
]
7+
TopLevelFolder = Literal["rawdata", "derivatives"]
8+
9+
OverwriteExistingFiles = Literal["never", "always", "if_source_newer"]
1010

11-
Prefix: TypeAlias = Literal["sub", "ses"]
11+
Prefix = Literal["sub", "ses"]
1212

13-
InterfaceOutput: TypeAlias = Tuple[bool, Any]
13+
InterfaceOutput = Tuple[bool, Any]
1414

15-
ConnectionMethods: TypeAlias = Literal[
15+
ConnectionMethods = Literal[
1616
"ssh", "local_filesystem", "gdrive", "aws", "local_only"
1717
]
1818

1919

2020
class TransferErrors(TypedDict):
21+
"""Type `errors` dictionary (used for collecting `rclone copy` output)."""
22+
2123
file_names: list[str]
2224
messages: list[str]
2325
nothing_was_transferred_rawdata: bool | None

datashuttle/utils/data_transfer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def __init__(
9090
self.check_input_arguments()
9191

9292
def run(self):
93+
"""Run the transfer."""
9394
include_list = self.build_a_list_of_all_files_and_folders_to_transfer()
9495

9596
if any(include_list):
@@ -113,7 +114,7 @@ def run(self):
113114
"Please contact the datashuttle team."
114115
)
115116

116-
rclone.log_rclone_output_python_api(stdout, stderr)
117+
rclone.log_stdout_stderr_python_api(stdout, stderr)
117118

118119
else:
119120
utils.log_and_message("No files included. None transferred.")

datashuttle/utils/rclone.py

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,8 @@ def transfer_data(
475475
return output
476476

477477

478-
def log_rclone_output_python_api(stdout, stderr):
479-
""""""
478+
def log_stdout_stderr_python_api(stdout: str, stderr: str) -> None:
479+
"""Log `stdout` and `stderr`."""
480480
message = (
481481
f"\n\n************** STDOUT **************\n"
482482
f"{stdout}"
@@ -488,7 +488,12 @@ def log_rclone_output_python_api(stdout, stderr):
488488

489489

490490
def log_rclone_copy_errors_api(errors):
491-
""""""
491+
"""Log the `errors` dictionary.
492+
493+
The `errors` dictionary contains all pertinent information on
494+
issues that occurred when running `rclone copy`. Note this logs
495+
for the API, the TUI display is handled separately.
496+
"""
492497
message = ""
493498

494499
if errors["nothing_was_transferred_rawdata"] is True:
@@ -517,7 +522,12 @@ def log_rclone_copy_errors_api(errors):
517522

518523

519524
def parse_rclone_copy_output(top_level_folder, output):
520-
""""""
525+
"""Format the `rclone copy` output ready for logging.
526+
527+
Reformat and combine the string streams and `errors`
528+
dictionary from stdout and stderr output of `rclone copy`.
529+
see `reformat_rclone_copy_output() for details.
530+
"""
521531
stdout, out_errors = reformat_rclone_copy_output(
522532
output.stdout, top_level_folder=top_level_folder
523533
)
@@ -526,9 +536,9 @@ def parse_rclone_copy_output(top_level_folder, output):
526536
output.stderr, top_level_folder=top_level_folder
527537
)
528538

539+
# Combine the two `errors` output
529540
all_errors = {
530-
"file_names": out_errors["file_names"]
531-
+ err_errors["file_names"], # TODO: this is so messy
541+
"file_names": out_errors["file_names"] + err_errors["file_names"],
532542
"messages": out_errors["messages"] + err_errors["messages"],
533543
"nothing_was_transferred_rawdata": err_errors[
534544
"nothing_was_transferred_rawdata"
@@ -544,6 +554,11 @@ def parse_rclone_copy_output(top_level_folder, output):
544554

545555

546556
def get_empty_errors_dict() -> TransferErrors:
557+
"""Return the `errors` dictionary with default values.
558+
559+
The `errors` dictionary holds information
560+
about errors which occurred during `rclone copy` transfer.
561+
"""
547562
return {
548563
"file_names": [],
549564
"messages": [],
@@ -556,7 +571,34 @@ def reformat_rclone_copy_output(
556571
stream: bytes,
557572
top_level_folder: TopLevelFolder | None = None,
558573
) -> tuple[str, TransferErrors]:
559-
""":return:"""
574+
"""Parse the output of `rclone copy` for convenient error checking.
575+
576+
Rclone's `copy` command (called with `--use-json-log`) outputs a lot of
577+
information related to the transfer. We dump this in text form to a log
578+
file. However, we also want to grab any key events (errors, or complete
579+
lack of transferred files) so these can be displayed separately.
580+
581+
This function iterates through all lines in the `rclone copy` output.
582+
This output is typically a mix of string format and json format.
583+
If the line is json-encoded, then we extract important information
584+
and format it to string, and re-insert it into the output.
585+
586+
In this way, we have a string-format output ready to be
587+
dumped to the logs, as well as an `errors` dictionary containing
588+
details on all key information.
589+
590+
Returns
591+
-------
592+
format_stream
593+
The input stream, converted to string and with all
594+
json-formatted lines reformatted as string. This is ready
595+
to be dumped to a log file.
596+
597+
errors
598+
A dictionary (`TransferErrors`) containing key information
599+
about issues in the transfer.
600+
601+
"""
560602
split_stream = stream.decode("utf-8").split("\n")
561603

562604
errors = get_empty_errors_dict()

0 commit comments

Comments
 (0)