Skip to content

Commit

Permalink
Refact iterator with side effects
Browse files Browse the repository at this point in the history
Iterators should have no side effects, because calling them would be
very suprising (nothing would happen if the iterator is not exhausted).
  • Loading branch information
kissgyorgy authored and kukovecz committed Jan 27, 2022
1 parent 3fa5d07 commit 7b1c18e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
21 changes: 10 additions & 11 deletions unblob/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import shlex
import subprocess
from pathlib import Path
from typing import Iterator, List
from typing import List

from structlog import get_logger

Expand Down Expand Up @@ -85,13 +85,12 @@ def carve_unknown_chunks(
return carved_paths


def extract_valid_chunks(
extract_dir: Path, file: io.BufferedIOBase, valid_chunks: List[ValidChunk]
) -> Iterator[Path]:
for chunk in valid_chunks:
filename = f"{chunk.start_offset}-{chunk.end_offset}.{chunk.handler.NAME}"
carve_path = extract_dir / filename
logger.info("Extracting valid chunk", path=carve_path, chunk=chunk)
carve_chunk_to_file(carve_path, file, chunk)
extracted = extract_with_command(extract_dir, carve_path, chunk.handler)
yield extracted
def extract_valid_chunk(
extract_dir: Path, file: io.BufferedIOBase, chunk: ValidChunk
) -> Path:
filename = f"{chunk.start_offset}-{chunk.end_offset}.{chunk.handler.NAME}"
carve_path = extract_dir / filename
logger.info("Extracting valid chunk", path=carve_path, chunk=chunk)
carve_chunk_to_file(carve_path, file, chunk)
extracted = extract_with_command(extract_dir, carve_path, chunk.handler)
return extracted
5 changes: 3 additions & 2 deletions unblob/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import plotext as plt
from structlog import get_logger

from .extractor import carve_unknown_chunks, extract_valid_chunks, make_extract_dir
from .extractor import carve_unknown_chunks, extract_valid_chunk, make_extract_dir
from .file_utils import iterate_file
from .finder import search_chunks_by_priority
from .iter_utils import pairwise
Expand Down Expand Up @@ -83,7 +83,8 @@ def process_file( # noqa: C901
for carved_path in carved_paths:
calculate_entropy(carved_path, draw_plot=verbose)

for new_path in extract_valid_chunks(extract_dir, file, outer_chunks):
for chunk in outer_chunks:
new_path = extract_valid_chunk(extract_dir, file, chunk)
process_file(
extract_root,
new_path,
Expand Down

0 comments on commit 7b1c18e

Please sign in to comment.