generated from linkml/linkml-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
rename_properties.py
87 lines (60 loc) · 2.11 KB
/
rename_properties.py
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 typer
from pathlib import Path
import re
import stringcase
def rename_relation_slots(file: Path):
text = file.read_text()
matches: list[str] = re.findall(r"\w.*?:\n", text)
for match in matches:
corrected = match.replace(" ", "_")
text = text.replace(match, corrected)
file.write_text(text)
def snake_to_pascal_case(line: str):
name = line.strip().replace(":", "")
corrected_name = stringcase.pascalcase(name)
return line.replace(name, corrected_name)
def rename_classes(file: Path):
lines = file.read_text().splitlines()
corrected_lines = []
in_class = False
for line in lines:
if in_class:
if "slots:" == line:
in_class = False
corrected_line = line
else:
if re.match(r"^ [a-zA-Z_]*?:$", line):
corrected_line = snake_to_pascal_case(line)
else:
corrected_line = line
else:
if "enums:" == line:
in_class = True
corrected_line = line
corrected_lines.append(corrected_line)
text = "\n".join(corrected_lines)
file.write_text(text)
def range_name_to_pascal(line: str):
name = line.replace("range:", "").strip()
corrected_name = stringcase.pascalcase(name.replace(" ", "_"))
return line.replace(name, corrected_name)
def rename_ranges(file: Path):
lines = file.read_text().splitlines()
corrected_lines = [
range_name_to_pascal(line) if "range:" in line else line for line in lines
]
text = "\n".join(corrected_lines)
file.write_text(text)
def is_a_name_to_pascal(line: str):
name = line.replace("is_a:", "").strip()
corrected_name = stringcase.pascalcase(name.replace(" ", "_"))
return line.replace(name, corrected_name)
def rename_is_a(file: Path):
lines = file.read_text().splitlines()
corrected_lines = [
is_a_name_to_pascal(line) if "is_a:" in line else line for line in lines
]
text = "\n".join(corrected_lines)
file.write_text(text)
if __name__ == "__main__":
typer.run(rename_is_a)