-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_content_markers.py
More file actions
78 lines (60 loc) · 2.45 KB
/
update_content_markers.py
File metadata and controls
78 lines (60 loc) · 2.45 KB
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
#!/usr/bin/env python3
"""
Script to update Content field in JSON files, replacing $ with _ and * with |
"""
import json
import os
from pathlib import Path
def update_content_markers(json_file):
"""Update the Content field to replace $ with _ and * with |"""
try:
# Read JSON file
with open(json_file, 'r', encoding='utf-8') as f:
data = json.load(f)
# Update Content field if it exists
if "Content" in data:
original_content = data["Content"]
updated_content = original_content.replace("$", "_").replace("*", "|")
if original_content != updated_content:
data["Content"] = updated_content
# Write updated JSON back to file
with open(json_file, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=4)
return True, original_content, updated_content
return False, None, None
except Exception as e:
raise Exception(f"Error processing file: {e}")
def process_directory(directory):
"""Process all JSON files in the directory and subdirectories."""
directory_path = Path(directory)
json_files = list(directory_path.rglob("*.json"))
print(f"Found {len(json_files)} JSON files to process")
print("=" * 80)
updated_count = 0
unchanged_count = 0
error_count = 0
for json_file in json_files:
try:
changed, original, updated = update_content_markers(json_file)
if changed:
updated_count += 1
print(f"✓ Updated: {json_file.name}")
if len(original) < 60 and len(updated) < 60:
print(f" Old: {original}")
print(f" New: {updated}")
else:
unchanged_count += 1
except Exception as e:
error_count += 1
print(f"✗ Error processing {json_file.relative_to(directory_path)}: {e}")
print("=" * 80)
print(f"\nUpdate complete!")
print(f"Files updated: {updated_count}")
print(f"Files unchanged: {unchanged_count}")
print(f"Errors: {error_count}")
if __name__ == "__main__":
ref_dir = Path(__file__).parent / "ref_all_bricks"
if not ref_dir.exists():
print(f"Error: Directory {ref_dir} does not exist")
exit(1)
process_directory(ref_dir)