Skip to content

Commit 4a49e19

Browse files
suppressed hash value of SHA from pooch (#695)
* suppressed hash value of SHA from pooch * changed linting issues and hide SHA hash
1 parent 745489d commit 4a49e19

File tree

2 files changed

+97
-7
lines changed

2 files changed

+97
-7
lines changed

movement/sample_data.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
are used.
77
"""
88

9+
import logging
910
import shutil
11+
from contextlib import contextmanager
1012
from pathlib import Path
1113

1214
import pooch
@@ -32,6 +34,32 @@
3234
METADATA_FILE = "metadata.yaml"
3335

3436

37+
@contextmanager
38+
def hide_pooch_hash_logs():
39+
"""Hide SHA256 hash printouts from ``pooch.retrieve``.
40+
41+
This context manager temporarily suppresses SHA256 hash messages
42+
when downloading files with Pooch.
43+
"""
44+
logger = pooch.get_logger()
45+
46+
class HashFilter(logging.Filter):
47+
def filter(self, record):
48+
msg = record.getMessage()
49+
# Suppress only hash display lines
50+
return not (
51+
"SHA256 hash of downloaded file" in msg
52+
or "Use this value as the 'known_hash'" in msg
53+
)
54+
55+
flt = HashFilter()
56+
logger.addFilter(flt)
57+
try:
58+
yield
59+
finally:
60+
logger.removeFilter(flt)
61+
62+
3563
def _download_metadata_file(file_name: str, data_dir: Path = DATA_DIR) -> Path:
3664
"""Download the metadata yaml file.
3765
@@ -54,13 +82,15 @@ def _download_metadata_file(file_name: str, data_dir: Path = DATA_DIR) -> Path:
5482
Path to the downloaded file.
5583
5684
"""
57-
local_file_path = pooch.retrieve(
58-
url=f"{DATA_URL}/{file_name}",
59-
known_hash=None,
60-
path=data_dir,
61-
fname=f"temp_{file_name}",
62-
progressbar=False,
63-
)
85+
with hide_pooch_hash_logs():
86+
local_file_path = pooch.retrieve(
87+
url=f"{DATA_URL}/{file_name}",
88+
known_hash=None,
89+
path=data_dir,
90+
fname=f"temp_{file_name}",
91+
progressbar=False,
92+
)
93+
6494
logger.debug(
6595
f"Successfully downloaded sample metadata file {file_name} "
6696
f"from {DATA_URL} to {data_dir}"

tests/test_unit/test_sample_data.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Test suite for the sample_data module."""
22

3+
import logging
34
from pathlib import Path
45
from unittest.mock import MagicMock, patch
56

@@ -14,6 +15,7 @@
1415
_fetch_metadata,
1516
fetch_dataset,
1617
fetch_dataset_paths,
18+
hide_pooch_hash_logs,
1719
list_datasets,
1820
)
1921

@@ -273,3 +275,61 @@ def test_fetch_and_unzip_exceptions(
273275
assert result == tmp_path / "poses" / "test"
274276
assert ".DS_Store" not in result_names
275277
assert expected_warning in caplog.records[-1].getMessage()
278+
279+
280+
@pytest.mark.parametrize(
281+
"message, should_block",
282+
[
283+
("SHA256 hash of downloaded file: abc123", True),
284+
("Use this value as the 'known_hash' argument", True),
285+
("Downloading file from ", False),
286+
],
287+
)
288+
def test_hide_pooch_hash_logs(message, should_block):
289+
"""Test that ``HashFilter`` blocks only hash-related messages."""
290+
with hide_pooch_hash_logs():
291+
logger = pooch.get_logger()
292+
hash_filter = logger.filters[-1]
293+
294+
# Create a mock log record
295+
record = logging.LogRecord(
296+
name="pooch",
297+
level=logging.INFO,
298+
pathname="",
299+
lineno=0,
300+
msg=message,
301+
args=(),
302+
exc_info=None,
303+
)
304+
305+
# The filter returns False to block, True to allow
306+
message_allowed = hash_filter.filter(record)
307+
308+
if should_block:
309+
assert not message_allowed, (
310+
f"Expected message to be blocked but was allowed: '{message}'"
311+
)
312+
else:
313+
assert message_allowed, (
314+
f"Expected message to be allowed but was blocked: '{message}'"
315+
)
316+
317+
318+
@pytest.mark.parametrize("raise_exception", [True, False])
319+
def test_hash_filter_removed_after_context(raise_exception):
320+
"""Test that the hash filter applied by ``hide_pooch_hash_logs``
321+
is properly removed after exiting context, even when an exception occurs.
322+
"""
323+
logger = pooch.get_logger()
324+
initial_filter_count = len(logger.filters)
325+
326+
if raise_exception:
327+
with pytest.raises(ValueError), hide_pooch_hash_logs():
328+
assert len(logger.filters) == initial_filter_count + 1
329+
raise ValueError("Test exception")
330+
else:
331+
with hide_pooch_hash_logs():
332+
assert len(logger.filters) == initial_filter_count + 1
333+
334+
# Filter should be removed after context exits
335+
assert len(logger.filters) == initial_filter_count

0 commit comments

Comments
 (0)