-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathparse.py
170 lines (141 loc) · 5.68 KB
/
parse.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
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
import sys
import json
MODS = ["SHIFT","CTRL","ALT","META","SUPER"] # [TODO] find a proper list of valid modifiers
def parse_value(val:str,t:str):
match t.lower():
case "int":
return int(val)
case "float":
return float(val)
case "bool":
if val.isnumeric():
return bool(int(val))
match val.lower():
case "true":
return True
case "false":
return False
case "yes":
return True
case "no":
return False
case "on":
return True
case "off":
return False
case "vec2":
if val.startswith("(") and val.endswith(")"):
val = val[1:-1]
if val.startswith("[") and val.endswith("]"):
val = val[1:-1]
if "," in val:
return tuple([float(x) for x in val.split(",")])
return tuple([float(x) for x in val.split()])
case "mod":
mods = []
buf = ""
for char in val.upper():
buf += char
if buf in MODS:
mods.append(buf)
buf = ""
return mods
# case "color":
# ...
# case "gradient":
# ...
case _:
return val
def string_value(val):
if isinstance(val,bool) or isinstance(val,int) or isinstance(val,float):
return str(val)
elif isinstance(val,tuple):
return f"({','.join([str(x) for x in val])})"
elif isinstance(val,list):
return f"[{','.join([str(x) for x in val])}]"
elif val == "\\[\\[Empty\\]\\]" or val == "[[EMPTY]]":
return None
elif val == "\\[\\[Auto\\]\\]" or val == "[[AUTO]]":
return '"auto"'
else:
return f'"{val}"'
def standardize_name(name:str):
return name.lower().replace(" ","_").replace("-","_").replace(".","_")
def gen_variables():
path = 'hyprland-wiki/pages/Configuring/Variables.md'
dat = {}
keys = {}
with open(path, 'r') as f:
raw = f.readlines()
in_sections = False
current_section = None
ignore = False
for line in raw:
if ignore:
if line.startswith("{{< /callout >}}"):
ignore = False
continue
if line.startswith("{{< callout type=info >}}"):
ignore = True
if line.startswith('## Sections'):
in_sections = True
continue
if in_sections:
if line.startswith('### '):
current_section = line[4:-1]
dat[current_section] = []
table_heads = []
keys[current_section.title().replace(" ","").replace("-","")] = f"{current_section.replace(' ','_').replace('-','_').lower()}:"
if line.startswith('#### '):
current_section = line[5:-1]
dat[current_section] = []
table_heads = []
keys[current_section.title().replace(" ","").replace("-","")] = f"{current_section.replace(' ','_').replace('-','_').lower()}:"
if line.lower().startswith("_subcategory"):
key = line[14:-2].strip(":")
parent = key.split(":")[0].replace(' ','_').replace('-','_').lower()
keys[current_section.title().replace(" ","").replace("-","")] = f"{parent}:{keys[current_section.title().replace(" ","").replace("-","")]}"
if line.startswith('|'):
if not table_heads:
table_heads = [x.strip() for x in line[1:-1].split('|')[:-1]]
else:
table_data = [x.strip() for x in line[1:-1].split('|')]
if table_data[0] == "---":
continue
if len(table_data) < len(table_heads):
continue
dat[current_section].append({table_heads[i]:table_data[i] for i in range(len(table_heads))})
return dat, keys
def gen_python(variables:dict, keys: dict):
out = f"from ._util import Section\nKEYS = {keys}\n\n"
for section in variables:
out += f"class {section.title().replace(" ","").replace("-","")}(Section):\n"
section_map = {}
part = ""
for variable in variables[section]:
val = parse_value(variable['default'],variable['type'])
section_map[standardize_name(variable['name'])] = variable
part += f""" {standardize_name(variable['name'])}: {type(val).__name__} = {string_value(val)}\n # {variable['description']}\n"""
out += " _section_name = " + f'"{section}"' + "\n"
out += " _section_map = " + json.dumps(section_map) + "\n"
out += f' _section_key = "{keys[section.title().replace(" ","").replace("-","")]}"\n\n'
out += part + "\n"
out += """class Default:\n"""
for section in variables:
out += f" {section.replace(' ','_').replace('-','_').lower()}: {section.title().replace(' ','').replace('-','')} = {section.title().replace(' ','').replace('-','')}()\n"
return out
if __name__ == "__main__":
name = sys.argv[1:][0].lower()
target = None
if len(sys.argv[1:]) >= 2:
target = sys.argv[1:][1]
match name:
case "variables":
if target:
with open(target, 'w') as f:
f.write(gen_python(*gen_variables()))
else:
print(gen_python(gen_variables()))
case _:
print("Invalid argument")
sys.exit(1)