Skip to content

Commit 4b75bc7

Browse files
jeromechooclaude
andcommitted
Create missing folders for -o output; bump to 3.1.1
db extract -o, db crawl -o, and db ask -o opened the output path directly, so writing into a folder that did not exist failed with FileNotFoundError. db dql export --out already created parents. All three now go through a shared write_output helper that expands ~ and creates missing parent folders before writing. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 8040ce7 commit 4b75bc7

5 files changed

Lines changed: 48 additions & 9 deletions

File tree

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "diffbot"
7-
version = "3.1.0"
7+
version = "3.1.1"
88
description = "Python client library for Diffbot APIs"
99
readme = "README.md"
1010
requires-python = ">=3.9"

‎src/diffbot/cli/__init__.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
json_schema_format,
2222
)
2323

24-
from ._common import get_client
24+
from ._common import get_client, write_output
2525

2626
console = Console()
2727
is_interactive = sys.stdout.isatty()
@@ -74,8 +74,7 @@ def extract(url, output, fmt, api):
7474
output_str = f"Title: {title}\n\nURL: {url}\n\nContent: {content}"
7575

7676
if output:
77-
with open(output, "w") as f:
78-
f.write(output_str)
77+
write_output(output, output_str)
7978
click.echo(f"Output written to {output}")
8079
else:
8180
print_markdown(output_str)
@@ -168,8 +167,7 @@ def ask(prompt: str, output: str = None, as_json: bool = False, schema_path: str
168167
else:
169168
for chunk in db.ask(messages):
170169
response += chunk
171-
with open(output, "w") as f:
172-
f.write(response)
170+
write_output(output, response)
173171
click.echo(f"Output written to {output}")
174172
elif as_json:
175173
sys.stdout.write(
@@ -256,8 +254,7 @@ def crawl(site, hops, job_name, max_to_crawl, max_to_process, restrict_domain,
256254
output_str = "\n".join(lines)
257255

258256
if output:
259-
with open(output, "w") as f:
260-
f.write(output_str)
257+
write_output(output, output_str)
261258
click.echo(f"Output written to {output}")
262259
else:
263260
print_markdown(output_str)

‎src/diffbot/cli/_common.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pathlib
2+
13
import click
24

35
from diffbot import Diffbot, resolve_token
@@ -19,3 +21,10 @@ def get_client() -> Diffbot:
1921
)
2022
raise click.Abort()
2123
return Diffbot(token=token)
24+
25+
26+
def write_output(path: str, text: str) -> None:
27+
"""Write text to path, creating missing parent folders."""
28+
dest = pathlib.Path(path).expanduser()
29+
dest.parent.mkdir(parents=True, exist_ok=True)
30+
dest.write_text(text)

‎tests/test_cli_output.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import json
2+
3+
from click.testing import CliRunner
4+
5+
import diffbot.cli as cli
6+
7+
8+
class StubClient:
9+
def extract(self, url, api, fmt):
10+
return {"objects": [{"title": "T"}]}
11+
12+
def crawl(self, site, **kwargs):
13+
return iter(())
14+
15+
16+
def test_extract_output_creates_missing_folders(monkeypatch, tmp_path):
17+
monkeypatch.setattr(cli, "get_client", StubClient)
18+
out = tmp_path / "a" / "b" / "extracted.json"
19+
result = CliRunner().invoke(
20+
cli.main, ["extract", "https://example.com", "-f", "json", "-o", str(out)]
21+
)
22+
assert result.exit_code == 0, result.output
23+
assert json.loads(out.read_text()) == {"objects": [{"title": "T"}]}
24+
25+
26+
def test_crawl_output_creates_missing_folders(monkeypatch, tmp_path):
27+
monkeypatch.setattr(cli, "get_client", StubClient)
28+
out = tmp_path / "a" / "b" / "crawl.json"
29+
result = CliRunner().invoke(
30+
cli.main, ["crawl", "https://example.com", "-f", "json", "-o", str(out)]
31+
)
32+
assert result.exit_code == 0, result.output
33+
assert out.exists()

‎uv.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)