-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxx-split.py
More file actions
70 lines (53 loc) · 1.93 KB
/
Copy pathxx-split.py
File metadata and controls
70 lines (53 loc) · 1.93 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
import os
import re
def _is_function_start(line):
function_name = re.match(r"^ {2}(\w+):", line)
if function_name:
return function_name.group(1)
def _is_function_comment(line):
return re.match(r"^ {2}#.*", line)
def _is_function_owner(line):
function_owner = re.match(r"^ {4}owner: (.*)", line)
if function_owner:
return function_owner.group(1)
def _split_functions(lines):
current_function = None
function_owner = None
content = []
comments = []
for line in lines:
# Comments are "buffered" until we hit a function start
if _is_function_comment(line):
comments.append(line)
continue
# Start of new function
if _is_function_start(line):
if current_function and content:
# hand back previous function content
yield current_function, function_owner, content
content = []
current_function = _is_function_start(line)
# prepend any pending comments
if comments:
content.extend(comments)
comments = []
if _is_function_owner(line):
function_owner = _is_function_owner(line)
# Collect lines for the current function
if current_function:
content.append(line)
# Yield last function
if current_function and content:
yield current_function, function_owner, content
def main() -> None:
with open("data/functions.yaml", "r") as file:
lines = file.readlines()
for function_name, function_owner, content in _split_functions(lines):
out_file = f"{function_name}.yaml"
with open(out_file, "w") as f:
# include the top-level "function:" header for valid YAML
f.write("function:\n")
f.writelines(content)
print(f"Created {out_file} owned by {function_owner}.")
if __name__ == "__main__":
main()