-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles_to_claude_xml.py
73 lines (52 loc) · 1.74 KB
/
files_to_claude_xml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "rich",
# "typer",
# ]
# ///
from pathlib import Path
from typing import Annotated
import typer
from rich import print
__version__ = "2024.10.4"
app = typer.Typer()
def compile_xml(*, files: list[Path], verbose: bool = False) -> str:
xml_parts = ["<documents>"]
for index, file in enumerate(files, start=1):
if verbose:
print(file.as_posix())
try:
content = file.read_text()
xml_parts.append(f'<document index="{index}">')
xml_parts.append(f"<source>{file.as_posix()}</source>")
xml_parts.append("<document_content>")
xml_parts.append(content)
xml_parts.append("</document_content>")
xml_parts.append("</document>")
except Exception as e:
print(f"[red]Error reading file: {str(e)}[/red]")
xml_parts.append("</documents>")
return "\n".join(xml_parts)
def version_callback(value: bool):
if value:
print(f"files-to-claude-xml version: {__version__}")
raise typer.Exit()
@app.command()
def main(
files: list[Path] = typer.Argument(..., help="Input files to process"),
output: Path = typer.Option(Path("_claude.xml"), help="Output XML file path"),
verbose: bool = False,
version: Annotated[
bool | None, typer.Option("--version", callback=version_callback)
] = None,
):
if not files:
print("No input files provided. Please specify at least one file.")
raise typer.Exit(code=1)
xml_content = compile_xml(files=files, verbose=verbose)
output.write_text(xml_content)
if verbose:
print(f"XML file has been created: {output}")
if __name__ == "__main__":
app()