Skip to content

Commit 517bb77

Browse files
László Vaskóe3krisztian
authored andcommitted
processing: remove multiple input files support
1 parent e8e5ba4 commit 517bb77

File tree

4 files changed

+36
-31
lines changed

4 files changed

+36
-31
lines changed

tests/test_cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_help(params):
8686
result = runner.invoke(unblob.cli.cli, params)
8787
assert result.exit_code == 0
8888
# NOTE: In practice, it writes "Usage: unblob ...", this is done in the `cli.main` with `click.make_context`
89-
assert result.output.startswith("Usage: cli [OPTIONS] FILES...")
89+
assert result.output.startswith("Usage: cli [OPTIONS] FILE")
9090

9191

9292
@pytest.mark.parametrize(
@@ -118,15 +118,15 @@ def test_without_file(params: List[str]):
118118
runner = CliRunner()
119119
result = runner.invoke(unblob.cli.cli, params)
120120
assert result.exit_code == 2
121-
assert "Missing argument 'FILES...'" in result.output
121+
assert "Missing argument 'FILE'" in result.output
122122

123123

124124
def test_non_existing_file(tmp_path: Path):
125125
runner = CliRunner()
126126
path = Path("non/existing/path/54")
127127
result = runner.invoke(unblob.cli.cli, ["--extract-dir", str(tmp_path), str(path)])
128128
assert result.exit_code == 2
129-
assert "Invalid value for 'FILES...'" in result.output
129+
assert "Invalid value for 'FILE'" in result.output
130130
assert f"Path '{str(path)}' does not exist" in result.output
131131

132132

unblob/cli.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ def __init__(
7777

7878
@click.command(help=get_help_text())
7979
@click.argument(
80-
"files",
81-
nargs=-1,
80+
"file",
8281
type=click.Path(path_type=Path, exists=True, resolve_path=True),
8382
required=True,
8483
)
@@ -160,7 +159,7 @@ def __init__(
160159
expose_value=False,
161160
)
162161
def cli(
163-
files: List[Path],
162+
file: Path,
164163
extract_root: Path,
165164
force: bool,
166165
depth: int,
@@ -191,8 +190,8 @@ def cli(
191190
keep_extracted_chunks=keep_extracted_chunks,
192191
)
193192

194-
logger.info("Start processing files", count=noformat(len(files)))
195-
all_reports = process_files(config, *files)
193+
logger.info("Start processing file", file=file)
194+
all_reports = process_files(config, file)
196195
return all_reports
197196

198197

unblob/processing.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
from .models import ExtractError, File, Task, TaskResult, UnknownChunk, ValidChunk
2121
from .pool import make_pool
2222
from .report import (
23-
ExtractDirectoriesExistReport,
23+
ExtractDirectoryExistsReport,
2424
FileMagicReport,
25+
Report,
2526
StatReport,
2627
UnknownError,
2728
)
@@ -59,24 +60,34 @@ class ExtractionConfig:
5960

6061

6162
@terminate_gracefully
62-
def process_files(config: ExtractionConfig, *paths: Path) -> List[Report]:
63-
existing_extract_dirs = get_existing_extract_dirs(config, paths)
63+
def process_files(config: ExtractionConfig, path: Path) -> List[Report]:
64+
task = Task(
65+
path=path,
66+
depth=0,
67+
)
6468

65-
if config.force_extract:
66-
for d in existing_extract_dirs:
67-
shutil.rmtree(d)
68-
elif existing_extract_dirs:
69-
report = ExtractDirectoriesExistReport(paths=existing_extract_dirs)
70-
logger.error("Extraction directories already exist", **report.asdict())
71-
return [report]
69+
errors = check_extract_directory(task, config)
70+
if errors:
71+
return errors
7272

73-
all_reports = []
74-
for path in paths:
75-
report = _process_one_file(config, path)
76-
all_reports.extend(report)
73+
result = _process_one_file(config, task)
74+
75+
return result
7776

78-
return all_reports
7977

78+
def check_extract_directory(task: Task, config: ExtractionConfig):
79+
errors = []
80+
81+
extract_dir = get_extract_dir_for_input(config, task.path)
82+
if extract_dir.exists():
83+
if config.force_extract:
84+
shutil.rmtree(extract_dir)
85+
else:
86+
report = ExtractDirectoryExistsReport(path=extract_dir)
87+
logger.error("Extraction directory already exist", **report.asdict())
88+
errors.append(report)
89+
90+
return errors
8091

8192
def get_existing_extract_dirs(
8293
config: ExtractionConfig, paths: Iterable[Path]
@@ -95,12 +106,7 @@ def get_existing_extract_dirs(
95106
return extract_dirs
96107

97108

98-
def _process_one_file(config: ExtractionConfig, path: Path) -> List[Report]:
99-
root_task = Task(
100-
path=path,
101-
depth=0,
102-
)
103-
109+
def _process_one_file(config: ExtractionConfig, root_task: Task) -> List[Report]:
104110
processor = Processor(config)
105111
all_reports = []
106112

unblob/report.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ class ExtractCommandFailedReport(ErrorReport):
9090

9191

9292
@attr.define(kw_only=True)
93-
class ExtractDirectoriesExistReport(ErrorReport):
93+
class ExtractDirectoryExistsReport(ErrorReport):
9494
severity: Severity = Severity.ERROR
95-
paths: List[Path]
95+
path: Path
9696

9797

9898
@attr.define(kw_only=True)

0 commit comments

Comments
 (0)