Skip to content

Commit b063e63

Browse files
committed
Second pass on Codacy PR #56 findings
- http_download: bundle stream knobs into _StreamContext dataclass so _stream_to_disk (10 args) and _run_stream (11 args) fit under the project-wide R0913 ceiling. Silence W1514 on the binary-mode open(). - scheduler/cron: make _expand_chunk's effective_high keyword-only so it stays under the five-positional-arg limit. - fast_find: wrap the long nosec/nosemgrep comment to clear C0301. - tests/test_secrets: split the NOSONAR justification above the line so the "password" literal doesn't blow the 100-col budget.
1 parent 4d62ad4 commit b063e63

File tree

4 files changed

+44
-55
lines changed

4 files changed

+44
-55
lines changed

automation_file/remote/http_download.py

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import contextlib
66
import os
7+
from dataclasses import dataclass
78
from pathlib import Path
89
from typing import Any
910

@@ -32,6 +33,19 @@
3233
)
3334

3435

36+
@dataclass(frozen=True)
37+
class _StreamContext:
38+
"""Bundle of per-download stream knobs; avoids 10+ positional args."""
39+
40+
write_mode: str
41+
chunk_size: int
42+
start_byte: int
43+
total_size: int
44+
max_bytes: int
45+
reporter: ProgressReporter | None
46+
token: Any
47+
48+
3549
@retry_on_transient(max_attempts=3, backoff_base=0.5, retriable=_RETRIABLE_EXCEPTIONS)
3650
def _open_stream(
3751
file_url: str,
@@ -73,38 +87,31 @@ def _stream_to_disk(
7387
response: requests.Response,
7488
part_path: Path,
7589
target: Path,
76-
*,
77-
write_mode: str,
78-
chunk_size: int,
79-
start_byte: int,
80-
total_size: int,
81-
max_bytes: int,
82-
reporter: ProgressReporter | None,
83-
token: Any,
90+
ctx: _StreamContext,
8491
) -> int | None:
8592
"""Stream ``response`` into ``part_path``. Returns bytes written, or None on failure."""
86-
written = start_byte
93+
written = ctx.start_byte
8794
with (
88-
open(part_path, write_mode) as output,
89-
_progress(total_size, str(target)) as progress,
95+
open(part_path, ctx.write_mode) as output, # pylint: disable=unspecified-encoding
96+
_progress(ctx.total_size, str(target)) as progress,
9097
):
91-
if start_byte > 0:
92-
progress.update(start_byte)
93-
for chunk in response.iter_content(chunk_size=chunk_size):
94-
if token is not None:
95-
token.raise_if_cancelled()
98+
if ctx.start_byte > 0:
99+
progress.update(ctx.start_byte)
100+
for chunk in response.iter_content(chunk_size=ctx.chunk_size):
101+
if ctx.token is not None:
102+
ctx.token.raise_if_cancelled()
96103
if not chunk:
97104
continue
98105
written += len(chunk)
99-
if written > max_bytes:
106+
if written > ctx.max_bytes:
100107
file_automation_logger.error(
101-
"download_file aborted: stream exceeded %d bytes", max_bytes
108+
"download_file aborted: stream exceeded %d bytes", ctx.max_bytes
102109
)
103110
return None
104111
output.write(chunk)
105112
progress.update(len(chunk))
106-
if reporter is not None:
107-
reporter.update(len(chunk))
113+
if ctx.reporter is not None:
114+
ctx.reporter.update(len(chunk))
108115
return written
109116

110117

@@ -151,41 +158,23 @@ def _run_stream(
151158
response: requests.Response,
152159
part_path: Path,
153160
target: Path,
154-
*,
155-
write_mode: str,
156-
chunk_size: int,
157-
start_byte: int,
158-
total_size: int,
159-
max_bytes: int,
160-
reporter: ProgressReporter | None,
161-
token: Any,
161+
ctx: _StreamContext,
162162
progress_name: str | None,
163163
) -> int | None:
164164
try:
165-
written = _stream_to_disk(
166-
response,
167-
part_path,
168-
target,
169-
write_mode=write_mode,
170-
chunk_size=chunk_size,
171-
start_byte=start_byte,
172-
total_size=total_size,
173-
max_bytes=max_bytes,
174-
reporter=reporter,
175-
token=token,
176-
)
165+
written = _stream_to_disk(response, part_path, target, ctx)
177166
except CancelledException:
178167
file_automation_logger.warning("download_file cancelled: %s", progress_name)
179-
if reporter is not None:
180-
reporter.finish(status="cancelled")
168+
if ctx.reporter is not None:
169+
ctx.reporter.finish(status="cancelled")
181170
return None
182171
except OSError as error:
183172
file_automation_logger.error("download_file write error: %r", error)
184-
if reporter is not None:
185-
reporter.finish(status="error")
173+
if ctx.reporter is not None:
174+
ctx.reporter.finish(status="error")
186175
return None
187-
if written is None and reporter is not None:
188-
reporter.finish(status="aborted")
176+
if written is None and ctx.reporter is not None:
177+
ctx.reporter.finish(status="aborted")
189178
return written
190179

191180

@@ -234,20 +223,17 @@ def download_file(
234223
return False
235224

236225
reporter, token = _make_reporter(progress_name, total_size, start_byte)
237-
238-
written = _run_stream(
239-
response,
240-
part_path,
241-
target,
226+
ctx = _StreamContext(
242227
write_mode=write_mode,
243228
chunk_size=chunk_size,
244229
start_byte=start_byte,
245230
total_size=total_size,
246231
max_bytes=max_bytes,
247232
reporter=reporter,
248233
token=token,
249-
progress_name=progress_name,
250234
)
235+
236+
written = _run_stream(response, part_path, target, ctx, progress_name)
251237
if written is None:
252238
return False
253239

automation_file/scheduler/cron.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def _expand_chunk(
8080
field_index: int,
8181
low: int,
8282
high: int,
83+
*,
8384
effective_high: int,
8485
) -> set[int]:
8586
if chunk == "*":
@@ -107,7 +108,7 @@ def _parse_field(raw: str, field_index: int) -> frozenset[int]:
107108
if not chunk:
108109
raise CronException(f"cron: empty chunk in field {field_index}")
109110
base, step = _split_step(chunk)
110-
result |= _expand_chunk(base, step, field_index, low, high, effective_high)
111+
result |= _expand_chunk(base, step, field_index, low, high, effective_high=effective_high)
111112
if field_index == 4 and 7 in result:
112113
result.discard(7)
113114
result.add(0)

automation_file/utils/fast_find.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ def _run_indexer(
167167

168168

169169
def _capture(argv: list[str]) -> list[str]:
170-
completed = subprocess.run( # nosec B603 nosemgrep — argv[0] is a fixed-name indexer; shell=False
170+
# argv[0] is a fixed-name indexer (mdfind/locate/es); shell=False.
171+
completed = subprocess.run( # nosec B603 nosemgrep
171172
argv,
172173
capture_output=True,
173174
timeout=_INDEX_TIMEOUT_SECONDS,

tests/test_secrets.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ def test_resolve_secret_refs_walks_nested_containers(
6464
"sinks": [
6565
{
6666
"username": "${env:U}",
67-
"password": "${env:P}", # NOSONAR test fixture — env-ref placeholder, not a real credential
67+
# NOSONAR env-ref placeholder, not a real credential
68+
"password": "${env:P}", # NOSONAR
6869
"port": 587,
6970
},
7071
]

0 commit comments

Comments
 (0)