-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathprogressive_xml_lint.py
More file actions
211 lines (177 loc) · 7.48 KB
/
progressive_xml_lint.py
File metadata and controls
211 lines (177 loc) · 7.48 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import subprocess
import sys
import os
import xml.sax
import textwrap
# --- Configuration ---
CONFIG = {
"indent_char": " ", # Use " " for spaces, "\t" for tabs
"indent_size": 4, # How many spaces per level?
"max_line_length": 120, # Maximum characters per line
"extensions": ('.xml', '.xsd', '.wsdl'),
# Map of { Bad_Byte : Replacement_Bytes }
"replacements": {
b'\x85': b'...', # Horizontal ellipsis
b'\x91': b"'", # Left single quote
b'\x92': b"'", # Right single quote
b'\x93': b'"', # Left double quote
b'\x94': b'"', # Right double quote
b'\x95': b'*', # Bullet
b'\x96': b'-', # En dash
b'\x97': b'-', # Em dash
b'\x84': b'"', # Low double quote
b'\x99': b'\xe2\x84\xa2' # Trademark (TM)
}
}
class DepthMapHandler(xml.sax.ContentHandler):
def __init__(self):
self.line_depths = {}
self.protected_lines = set()
self.current_depth = 0
self.locator = None
self.in_programlisting = False
self.programlisting_start = -1
def setDocumentLocator(self, locator):
self.locator = locator
def startElement(self, name, attrs):
line_num = self.locator.getLineNumber()
if line_num not in self.line_depths:
self.line_depths[line_num] = self.current_depth
self.current_depth += 1
if name == 'programlisting':
self.in_programlisting = True
self.programlisting_start = line_num
def endElement(self, name):
self.current_depth -= 1
line_num = self.locator.getLineNumber()
if line_num not in self.line_depths:
self.line_depths[line_num] = self.current_depth
if name == 'programlisting' and self.in_programlisting:
self.in_programlisting = False
self.protected_lines.update(range(self.programlisting_start, line_num + 1))
def characters(self, content):
if content.strip():
line_num = self.locator.getLineNumber()
if line_num not in self.line_depths:
self.line_depths[line_num] = self.current_depth
def get_changed_lines(file_path, base_ref):
"""
Returns a set of 1-based line numbers changed in the file.
Uses errors='replace' to avoid crashing on the very bad bytes we aim to fix.
"""
cmd = ["git", "diff", "-U0", base_ref, "--", file_path]
try:
# Crucial: errors='replace' prevents crash when diffing files with bad encoding
output = subprocess.check_output(cmd).decode("utf-8", errors="replace")
except subprocess.CalledProcessError:
return set()
changed_lines = set()
for line in output.splitlines():
if line.startswith("@@"):
try:
plus_part = line.split("+")[1].split()[0]
if "," in plus_part:
start, count = map(int, plus_part.split(","))
else:
start, count = int(plus_part), 1
if count > 0:
changed_lines.update(range(start, start + count))
except (IndexError, ValueError):
continue
return changed_lines
def smart_wrap(content, indent_str, max_len):
"""
Wraps text content (string) to fill the line up to max_len.
"""
clean_content = ' '.join(content.split())
full_line = indent_str + clean_content
if len(full_line) <= max_len:
return full_line + "\n"
wrapped_lines = textwrap.wrap(
clean_content,
width=max_len,
initial_indent=indent_str,
subsequent_indent=indent_str,
break_long_words=False,
break_on_hyphens=False
)
return "\n".join(wrapped_lines) + "\n"
def process_file(file_path, base_ref):
# 1. Get changed lines (from the dirty file on disk)
changed_lines = get_changed_lines(file_path, base_ref)
if not changed_lines:
return
# 2. Read file as BINARY
# We must operate in binary to handle the specific byte replacements safely.
with open(file_path, "rb") as f:
original_lines_bytes = f.readlines()
# 3. Create a 'Clean-in-Memory' version
# We apply the byte replacements ONLY to the changed lines.
# This buffer will be used for parsing (to avoid crashes) and formatting.
cleaned_lines_bytes = list(original_lines_bytes) # Make a copy
for i in range(len(cleaned_lines_bytes)):
line_num = i + 1
if line_num in changed_lines:
current_line = cleaned_lines_bytes[i]
for bad_byte, replacement in CONFIG["replacements"].items():
current_line = current_line.replace(bad_byte, replacement)
cleaned_lines_bytes[i] = current_line
# 4. Parse the Cleaned Content for Depth
# We join the cleaned lines into a single byte stream for the parser.
# This prevents the parser from crashing on the user's bad bytes.
full_clean_content = b"".join(cleaned_lines_bytes)
handler = DepthMapHandler()
parser = xml.sax.make_parser()
parser.setContentHandler(handler)
try:
# xml.sax can parse bytes directly!
xml.sax.parseString(full_clean_content, handler)
except xml.sax.SAXException:
print(f"Skipping {file_path}: Malformed XML.")
return
# 5. Format and Reconstruct
final_output_lines = []
for i, line_bytes in enumerate(cleaned_lines_bytes):
line_num = i + 1
# IF Protected (programlisting): Keep the bytes exactly as they are
if line_num in handler.protected_lines:
final_output_lines.append(line_bytes)
continue
# IF Changed: Decode -> Format -> Encode
if line_num in changed_lines and line_num in handler.line_depths:
try:
# Decode to string for text wrapping (should be safe now after cleaning)
line_text = line_bytes.decode('utf-8')
except UnicodeDecodeError:
# Fallback: If it still fails, keep original bytes
final_output_lines.append(line_bytes)
continue
depth = handler.line_depths[line_num]
correct_indent = CONFIG["indent_char"] * (CONFIG["indent_size"] * depth)
# Smart wrap returns a string, we must encode back to bytes
formatted_text = smart_wrap(line_text, correct_indent, CONFIG["max_line_length"])
final_output_lines.append(formatted_text.encode('utf-8'))
else:
# IF Unchanged: Use the ORIGINAL bytes from disk (Step 2)
# This ensures we don't accidentally touch bytes in the rest of the file.
final_output_lines.append(original_lines_bytes[i])
# 6. Write back as BINARY
with open(file_path, "wb") as f:
f.writelines(final_output_lines)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python progressive_xml_lint.py <base_ref>")
sys.exit(1)
base_ref = sys.argv[1]
try:
diff_cmd = ["git", "diff", "--name-only", base_ref]
files = subprocess.check_output(diff_cmd).decode("utf-8").splitlines()
target_files = [
f for f in files
if f.lower().endswith(CONFIG["extensions"]) and os.path.exists(f)
]
for file_path in target_files:
process_file(file_path, base_ref)
except Exception as e:
print(f"Error: {e}")
sys.exit(1)