59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
'''
|
||
MPSTWM
|
||
Автор: Игорь Рагозин
|
||
Лицензия: MIT(подробнее в /LICENSE)
|
||
'''
|
||
from dataclasses import dataclass
|
||
|
||
'''
|
||
Своя реализация парсера конфига Hyprland.
|
||
Примерный алгоритм работы, и концепции на основе
|
||
идеи рассказал ИИ. Код полностью написан с помощью обычных рук
|
||
'''
|
||
@dataclass
|
||
class ConfigComment:
|
||
value: str
|
||
line: int
|
||
|
||
@dataclass
|
||
class AirLine:
|
||
line: int
|
||
|
||
@dataclass
|
||
class ConfigVariable:
|
||
name: str
|
||
value: str
|
||
line: int
|
||
|
||
def ParserKeyValue(line):
|
||
line = line.strip()
|
||
if '=' in line:
|
||
key,value=line.split('=', 1)
|
||
key = key.strip()
|
||
value = value.strip(df)
|
||
return key, value
|
||
|
||
def isBlock(line):
|
||
line = line.strip()
|
||
if '{' in line:
|
||
return True
|
||
|
||
|
||
with open("example_hyprland.conf", "r") as file:
|
||
sections=[]
|
||
file = file.readlines()
|
||
for index, line in enumerate(file):
|
||
no_tab = line.strip()
|
||
if line.strip().startswith('#'):
|
||
comment = lambda value,line: ConfigComment(value,line+1)
|
||
sections.append(comment(no_tab,index))
|
||
elif not line.strip():
|
||
airline = lambda line: AirLine(line+1)
|
||
sections.append(airline(index))
|
||
elif line.strip().startswith('$'):
|
||
value_line = ParserKeyValue(no_tab)
|
||
key,value = value_line[0],value_line[1]
|
||
variable = lambda name,value,line: ConfigVariable(name,value,line+1)
|
||
sections.append(variable(key,value,index))
|
||
print(sections)
|