Repository navigation
Expand file tree
/
Copy pathsplit-unicode-files.py
More file actions
277 lines (206 loc) · 7.85 KB
/
Copy pathsplit-unicode-files.py
File metadata and controls
277 lines (206 loc) · 7.85 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python3
from __future__ import annotations
import re
import sys
import ast
from typing import Set, List, Tuple
sys.stdout.reconfigure(encoding="utf-8") # type: ignore[attr-defined]
# ------------------------------------------------------------
# Loading & normalization
# ------------------------------------------------------------
def read_set(path: str) -> Set[str]:
with open(path, "r", encoding="utf-8") as f:
data = f.read()
result = ast.literal_eval(data)
if not isinstance(result, set):
raise ValueError(f"{path} does not contain a Python set literal.")
return result
def load_yaml_file_strip_header(path: str) -> List[str]:
"""Load a YAML file, removing any leading '---'."""
with open(path, "r", encoding="utf-8") as f:
lines = f.readlines()
i = 0
while i < len(lines) and lines[i].strip() == "---":
i += 1
return lines[i:]
def load_and_concat_yaml_files(paths: List[str]) -> List[str]:
lines: List[str] = []
for p in paths:
lines.extend(load_yaml_file_strip_header(p))
return lines
# ------------------------------------------------------------
# Key parsing & expansion
# ------------------------------------------------------------
# Matches: - "a":, - 'b-z':, - "(":, - "\"":, - A-Z:
KEY_RE = re.compile(r'''
^\s*-\s* # leading whitespace, dash, whitespace
(?P<key>
"(?:[^"\\]|\\.)*" # double-quoted key
| '(?:[^'\\]|\\.)*' # single-quoted key
| [^:\s][^:]* # unquoted key (no leading colon)
)
\s*:
''', re.VERBOSE)
def extract_key(line: str) -> str | None:
m = KEY_RE.match(line)
if not m:
return None
key = m.group("key")
# Strip matching quotes
if (key.startswith('"') and key.endswith('"')) or \
(key.startswith("'") and key.endswith("'")):
key = key[1:-1]
return key
def expand_key(key: str) -> Set[str]:
"""
Expand a YAML key like:
"a" → {"a"}
"A-Z" → {"A","B",...,"Z"}
"0-9" → {"0","1",...,"9"}
"𝛛𝛜𝛝..." → {"𝛛","𝛜","𝛝",...}
" " → {" "}
"""
raw = key # preserve exactly what extract_key returned
s = key.strip() # only for detecting ranges
# Simple range like A-Z or α-ω
if "-" in s and len(s) == 3:
start, end = s[0], s[2]
return {chr(c) for c in range(ord(start), ord(end) + 1)}
# Multi-character literal → use raw, not stripped
if len(raw) > 1:
return set(raw)
# Single character (including space)
return {raw}
# ------------------------------------------------------------
# Matching logic
# ------------------------------------------------------------
def key_matches_set(key: str, ops: Set[str]) -> bool:
# Case 1: simple alphabetic range
if "-" in key and len(key) == 3:
start, end = key[0], key[2]
return any(start <= ch <= end for ch in ops)
# Case 2: multi-character literal
if len(key) > 1:
return any(ch in ops for ch in key)
# Case 3: single character
return key in ops
# ------------------------------------------------------------
# Block parsing
# ------------------------------------------------------------
def is_block_terminator(line: str, key_indent: int) -> bool:
"""
A block ends when:
- a comment is LESS indented than the key line
- a non-comment line is LESS indented than the key line
A block does NOT end on:
- blank lines
- comments indented >= key indent
"""
stripped = line.lstrip()
indent = len(line) - len(stripped)
# Blank lines NEVER terminate a block
if stripped == "":
return False
# Outdented comment → terminator
if stripped.startswith("#"):
return indent < key_indent
# Outdented non-comment → terminator
return indent < key_indent
def parse_preamble(lines: List[str], start: int) -> Tuple[List[str], int]:
"""Return (preamble_lines, new_index)."""
preamble: List[str] = []
i = start
n = len(lines)
while i < n and extract_key(lines[i]) is None:
preamble.append(lines[i])
i += 1
return preamble, i
def parse_blocks(lines: List[str], start: int, ops: Set[str]) -> Tuple[List[List[str]], List[List[str]], Set[str]]:
matched: List[List[str]] = []
unmatched: List[List[str]] = []
yaml_char_universe: Set[str] = set()
i = start
n = len(lines)
while i < n:
line = lines[i]
key = extract_key(line)
if line.find('":"') != -1 or line.find('" "') != -1:
print(f"key: '{key}', expanded: '{expand_key(key)}'")
if key is None:
i += 1
continue
# Track characters used in YAML keys
yaml_char_universe.update(expand_key(key))
block: List[str] = [line]
key_indent = len(line) - len(line.lstrip())
is_match = key_matches_set(key, ops)
i += 1
while i < n:
next_line = lines[i]
# 1) New key at same or less indent → end of this block
next_key = extract_key(next_line)
if next_key is not None:
next_indent = len(next_line) - len(next_line.lstrip())
if next_indent <= key_indent:
break # do NOT consume; outer loop will handle it
# 2) Normal terminators (blank or outdented comment)
if is_block_terminator(next_line, key_indent):
break
block.append(next_line)
i += 1
# Include terminator comment (but not blank)
if i < n and lines[i].strip().startswith("#"):
block.append(lines[i])
i += 1
if is_match:
matched.append(block)
else:
unmatched.append(block)
return matched, unmatched, yaml_char_universe
# ------------------------------------------------------------
# Output & sanity checks
# ------------------------------------------------------------
def sanity_check(ops: Set[str], yaml_chars: Set[str]) -> None:
unknown = sorted(ch for ch in ops if ch not in yaml_chars)
if unknown:
print("WARNING: The following characters from the set do not appear in any YAML key:")
for ch in unknown:
display = ch if ch.strip() else repr(ch)
print(f" {display}")
print()
def write_output_files(preamble: List[str], matched: List[List[str]], unmatched: List[List[str]]) -> None:
with open("matched.yaml", "w", encoding="utf-8") as f:
f.write("---\n")
f.writelines(preamble)
for block in matched:
f.writelines(block)
f.write("\n")
with open("not-matched.yaml", "w", encoding="utf-8") as f:
f.write("---\n")
f.writelines(preamble)
for block in unmatched:
f.writelines(block)
f.write("\n")
# ------------------------------------------------------------
# Orchestration
# ------------------------------------------------------------
def split_yaml_by_set(set_path: str, yaml_paths: List[str]) -> None:
ops = read_set(set_path)
lines = load_and_concat_yaml_files(yaml_paths)
preamble, idx = parse_preamble(lines, 0)
matched, unmatched, yaml_chars = parse_blocks(lines, idx, ops)
sanity_check(ops, yaml_chars)
write_output_files(preamble, matched, unmatched)
print(f"Done. Wrote {len(matched)} matched blocks and {len(unmatched)} unmatched blocks.")
# ------------------------------------------------------------
# CLI
# ------------------------------------------------------------
def main() -> None:
if len(sys.argv) < 3:
print("Usage: python split_yaml_by_set.py <setfile> <yaml1> [yaml2 ...]")
sys.exit(1)
set_path = sys.argv[1]
yaml_paths = sys.argv[2:]
split_yaml_by_set(set_path, yaml_paths)
if __name__ == "__main__":
main()