Skip to content

Commit d932f2e

Browse files
Initial multifile processing
1 parent a09c505 commit d932f2e

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

scaffold/cli.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Annotated
44

55
import orjson
6+
import typer
67
import uvicorn
78
from loguru import logger
89
from typer import Argument, Typer
@@ -36,6 +37,72 @@ def single(
3637
logger.info(f"Message created at {output_path}")
3738

3839

40+
@cli.command()
41+
def batch(
42+
file_path: Annotated[
43+
pathlib.Path,
44+
Argument(help="Path to a JSON file or a directory containing JSON files"),
45+
],
46+
max_files: Annotated[
47+
int, typer.Option("--max-files", help="Maximum number of files to process")
48+
] = None,
49+
count_only: Annotated[
50+
bool,
51+
typer.Option(
52+
"--count-only",
53+
help="Only simulate processing; count successes and failures",
54+
),
55+
] = False,
56+
) -> None:
57+
startup()
58+
59+
if file_path.is_file() and file_path.suffix == ".json":
60+
input_files = [file_path]
61+
elif file_path.is_dir():
62+
input_files = sorted(file_path.glob("*.json"))
63+
else:
64+
logger.error(
65+
f"Invalid input: {file_path} is neither a .json file nor a directory containing .json files."
66+
)
67+
raise SystemExit(1)
68+
69+
if max_files is not None:
70+
input_files = input_files[:max_files]
71+
72+
success_count = 0
73+
failure_count = 0
74+
75+
for input_file in input_files:
76+
try:
77+
input_data = orjson.loads(input_file.read_bytes())
78+
result = pipeline(input_data)
79+
80+
if not count_only:
81+
directory = input_file.parent / "messages"
82+
os.makedirs(directory, exist_ok=True)
83+
84+
performance_month = input_data.get("performance_month", "unknown_month")
85+
new_filename = (
86+
f"{input_file.stem} - message for {performance_month}.json"
87+
)
88+
output_path = directory / new_filename
89+
90+
output_path.write_bytes(
91+
orjson.dumps(result, option=orjson.OPT_INDENT_2)
92+
)
93+
logger.info(f"Message created at {output_path}")
94+
else:
95+
logger.info(f"✔ Would process: {input_file}")
96+
97+
success_count += 1
98+
except Exception as e:
99+
logger.error(f"✘ Failed to process {input_file}: {e}")
100+
failure_count += 1
101+
102+
logger.info(f"Total files scanned: {len(input_files)}")
103+
logger.info(f"Successful: {success_count}, Failed: {failure_count}")
104+
105+
39106
@cli.command()
40107
def web():
41108
uvicorn.run("scaffold.api:app", reload=False, use_colors=True)

0 commit comments

Comments
 (0)