Cai
6 days ago 129e0ebd2ca859b3463ad2c31ae335c72bace22d
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import argparse
import csv
from pathlib import Path
 
 
def read_csv(path):
    with path.open("r", encoding="utf-8-sig", newline="") as handle:
        return list(csv.DictReader(handle))
 
 
def write_csv(path, fieldnames, rows):
    with path.open("w", encoding="utf-8-sig", newline="") as handle:
        writer = csv.DictWriter(handle, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(rows)
 
 
def norm_path(value):
    return (value or "").replace("\\", "/").strip()
 
 
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--source-document", required=True)
    parser.add_argument("--conversion-status", required=True)
    parser.add_argument("--updated-manifest", required=True)
    args = parser.parse_args()
 
    source_path = Path(args.source_document)
    status_path = Path(args.conversion_status)
    updated_manifest_path = Path(args.updated_manifest)
 
    source_rows = read_csv(source_path)
    status_rows = read_csv(status_path)
    status_by_path = {norm_path(row.get("raw_file_path")): row for row in status_rows}
 
    update_rows = []
    for row in source_rows:
        raw_path = norm_path(row.get("raw_file_path"))
        status = status_by_path.get(raw_path)
        if not status:
            continue
        before_processing = row.get("processing_status", "")
        before_text = row.get("converted_text_path", "")
        conversion_status = status.get("conversion_status", "")
        if conversion_status == "TEXT_CONVERTED":
            row["converted_text_path"] = status.get("converted_text_path", "")
            row["processing_status"] = "TEXT_CONVERTED"
        else:
            row["processing_status"] = "CONVERSION_GAP"
        row["run_id"] = status.get("run_id", row.get("run_id", ""))
        update_rows.append(
            {
                "doc_id": row.get("doc_id", ""),
                "raw_file_path": row.get("raw_file_path", ""),
                "file_sha256": row.get("file_sha256", ""),
                "conversion_status": conversion_status,
                "processing_status_before": before_processing,
                "processing_status_after": row.get("processing_status", ""),
                "converted_text_path_before": before_text,
                "converted_text_path_after": row.get("converted_text_path", ""),
                "run_id": row.get("run_id", ""),
            }
        )
 
    fieldnames = list(source_rows[0].keys()) if source_rows else []
    write_csv(source_path, fieldnames, source_rows)
 
    update_fields = [
        "doc_id",
        "raw_file_path",
        "file_sha256",
        "conversion_status",
        "processing_status_before",
        "processing_status_after",
        "converted_text_path_before",
        "converted_text_path_after",
        "run_id",
    ]
    updated_manifest_path.parent.mkdir(parents=True, exist_ok=True)
    write_csv(updated_manifest_path, update_fields, update_rows)
    print(f"updated={len(update_rows)}")
    print(f"manifest={updated_manifest_path.as_posix()}")
 
 
if __name__ == "__main__":
    main()