-
Notifications
You must be signed in to change notification settings - Fork 9
/
helpers.py
100 lines (66 loc) · 2.49 KB
/
helpers.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
88
89
90
91
92
93
94
95
96
97
98
99
100
import os
import re
import sys
def top_level_file():
top_level_file_path = os.path.realpath(sys.modules["__main__"].__file__)
this_directory = os.path.dirname(os.path.realpath(__file__))
return os.path.relpath(top_level_file_path, this_directory).replace("\\", "/")
def preamble():
return f"// This file was generated by {top_level_file()}\n\n"
def camel_case(string):
result = ""
for word in re.sub("[^0-9a-zA-Z]", " ", string).split():
if result == "":
result += word.lower()
elif len(word) == 2 and word == word.upper(): # if two letter acronym
result += word
else:
result += word.lower().capitalize()
return result
def macro_case(string):
return re.sub("[^0-9a-zA-Z]", " ", string).upper().replace(" ", "_")
def pascal_case(string):
result = ""
for word in re.sub("[^0-9a-zA-Z]", " ", string).split():
if len(word) == 2 and word == word.upper(): # if two letter acronym
result += word
else:
result += word.lower().capitalize()
return result
def snake_case(string):
return re.sub("[^0-9a-zA-Z]", " ", string).lower().replace(" ", "_")
def title_case(string):
result = ""
for word in string.split():
result += word[0].upper() + word[1:] + " "
return result[:-1]
def insert(file_path, code, id):
with open(file_path) as file:
all_lines = file.readlines()
start_key = f"// Start of code block #{id} generated by {top_level_file()}\n"
end_key = f"// End of code block #{id} generated by {top_level_file()}\n"
waiting_for_start = True
waiting_for_end = True
lines_before_start = []
lines_after_end = []
for line in all_lines:
if waiting_for_start:
lines_before_start += line
if line.strip() == start_key.strip():
waiting_for_start = False
elif waiting_for_end:
if line.strip() == end_key.strip():
lines_after_end += line
waiting_for_end = False
else:
lines_after_end += line
if waiting_for_start:
raise Exception(f"Error: Cannot find {start_key.strip()} in {file_path}")
if waiting_for_end:
raise Exception(f"Error: Cannot find {end_key.strip()} in {file_path}")
with open(file_path, "w") as file:
for line in lines_before_start:
file.write(line)
file.write(code)
for line in lines_after_end:
file.write(line)