-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHyprParser.py
101 lines (73 loc) · 3.43 KB
/
HyprParser.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
import re
import os
from CTkMessagebox import CTkMessagebox
class HyprParser:
@classmethod
def hypr_reader(cls):
home = os.getlogin()
config_file = os.path.join(home,'/.config/hypr/hyprpaper.conf')
with open(config_file, 'r') as file:
config = file.read()
# Regular expressions to capture relevant patterns
splash_pattern = re.compile(r'splash\s*=\s*(\w+)')
ipc_pattern = re.compile(r'#?\s*ipc\s*=\s*(\w+)')
monitor_pattern = re.compile(r'^wallpaper\s*=\s*([^,]+),', re.MULTILINE)
# Extract splash status
splash_status = splash_pattern.search(config)
splash = splash_status.group(1) if splash_status else None
# Extract ipc status
ipc_status = ipc_pattern.search(config)
ipc = ipc_status.group(1) if ipc_status else None
# Extract monitors
monitor_stats = monitor_pattern.findall(config) # findall returns all matches as a list
monitors = monitor_stats if monitor_stats else []
# Return extracted values
return splash, ipc, monitors
@classmethod
def hypr_write(cls,image_path, target_monitor):
home = os.path.expanduser('~')
config_file = os.path.join(home,'.config','hypr' ,'hyprpaper.conf')
print(config_file)
try:
with open(config_file, 'r') as file:
lines = file.readlines()
preload_pattern = re.compile(r'^preload\s*=\s*(.+)$')
wallpaper_pattern = re.compile(r'^wallpaper\s*=\s*([^,]+),(.*)$')
# Extract preload and wallpaper data
preloads = set()
wallpapers = {}
other_lines = []
for line in lines:
if preload_match := preload_pattern.match(line):
preload_path = preload_match.group(1).strip()
preloads.add(preload_path)
elif wallpaper_match := wallpaper_pattern.match(line):
monitor = wallpaper_match.group(1).strip()
wallpaper_path = wallpaper_match.group(2).strip()
wallpapers[monitor] = wallpaper_path
else:
other_lines.append(line.strip())
wallpapers[target_monitor] = image_path
# Synchronize preload with wallpapers
preloads = {path for path in preloads if path in wallpapers.values()} # Keep only valid paths
preloads.add(image_path) # Ensure the new wallpaper is preloaded
# Write the updated configuration back to the file
with open(config_file, 'w') as file:
# Write updated preload entries
for preload_path in sorted(preloads):
file.write(f"preload= {preload_path}\n")
# Write updated wallpaper entries
for monitor, wallpaper_path in wallpapers.items():
file.write(f"wallpaper = {monitor},{wallpaper_path}\n")
# Write other configuration lines
for line in other_lines:
file.write(f"{line}\n")
return True
except FileNotFoundError:
CTkMessagebox(title="Error", icon="warning", message="hyprpaper.conf file not found")
return False
except Exception as e:
print(e)
return False
if __name__ =='__main__':
HyprParser.hypr_write('2121212','cassssssss')