Skip to content

Commit a868c8a

Browse files
committed
feat(cli): add options to disable exports
1 parent 3102b6d commit a868c8a

2 files changed

Lines changed: 68 additions & 7 deletions

File tree

crawler_to_md/cli.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ def main():
8888
help="Delay between requests in seconds",
8989
default=0,
9090
)
91+
parser.add_argument(
92+
"--no-markdown",
93+
action="store_true",
94+
help="Disable generation of the compiled Markdown file",
95+
default=False,
96+
)
97+
parser.add_argument(
98+
"--no-json",
99+
action="store_true",
100+
help="Disable generation of the compiled JSON file",
101+
default=False,
102+
)
91103

92104
try:
93105
import argcomplete
@@ -174,12 +186,13 @@ def main():
174186
logger.info("ExportManager initialized.")
175187

176188

177-
export_manager.export_to_markdown(os.path.join(output, f"{output_name}.md"))
178-
logger.info("Export to markdown completed.")
179-
189+
if not args.no_markdown:
190+
export_manager.export_to_markdown(os.path.join(output, f"{output_name}.md"))
191+
logger.info("Export to markdown completed.")
180192

181-
export_manager.export_to_json(os.path.join(output, f"{output_name}.json"))
182-
logger.info("Export to JSON completed.")
193+
if not args.no_json:
194+
export_manager.export_to_json(os.path.join(output, f"{output_name}.json"))
195+
logger.info("Export to JSON completed.")
183196

184197
output_folder_ei = None
185198
if args.export_individual:
@@ -191,8 +204,10 @@ def main():
191204

192205
markdown_path = os.path.join(output, f"{output_name}.md")
193206
json_path = os.path.join(output, f"{output_name}.json")
194-
print("\033[94mMarkdown file generated at: \033[0m", markdown_path)
195-
print("\033[92mJSON file generated at: \033[0m", json_path)
207+
if not args.no_markdown:
208+
print("\033[94mMarkdown file generated at: \033[0m", markdown_path)
209+
if not args.no_json:
210+
print("\033[92mJSON file generated at: \033[0m", json_path)
196211
if args.export_individual and output_folder_ei:
197212
print(
198213
"\033[95mIndividual Markdown files exported to: \033[0m",

tests/test_cli.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
from crawler_to_md import cli
3+
from crawler_to_md.export_manager import ExportManager
4+
from crawler_to_md.scraper import Scraper
5+
6+
7+
def _run_cli(monkeypatch, tmp_path, extra_args):
8+
calls = {"md": False, "json": False}
9+
10+
def fake_export_markdown(self, path):
11+
calls["md"] = True
12+
13+
def fake_export_json(self, path):
14+
calls["json"] = True
15+
16+
monkeypatch.setattr(ExportManager, "export_to_markdown", fake_export_markdown)
17+
monkeypatch.setattr(ExportManager, "export_to_json", fake_export_json)
18+
monkeypatch.setattr(Scraper, "start_scraping", lambda *a, **k: None)
19+
20+
cache_folder = tmp_path / "cache"
21+
args = [
22+
"prog",
23+
"--url",
24+
"http://example.com",
25+
"--output-folder",
26+
str(tmp_path),
27+
"--cache-folder",
28+
str(cache_folder),
29+
] + extra_args
30+
31+
monkeypatch.setattr(sys, "argv", args)
32+
cli.main()
33+
return calls
34+
35+
36+
def test_cli_default_exports(monkeypatch, tmp_path):
37+
calls = _run_cli(monkeypatch, tmp_path, [])
38+
assert calls["md"] is True
39+
assert calls["json"] is True
40+
41+
42+
def test_cli_disable_exports(monkeypatch, tmp_path):
43+
calls = _run_cli(monkeypatch, tmp_path, ["--no-markdown", "--no-json"])
44+
assert calls["md"] is False
45+
assert calls["json"] is False
46+

0 commit comments

Comments
 (0)