-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsystem.py
59 lines (48 loc) · 1.9 KB
/
system.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
import subprocess
import os
import glob
import psutil
def get_volume():
result = subprocess.run(['pactl', 'get-sink-volume', '@DEFAULT_SINK@'], stdout=subprocess.PIPE)
output = result.stdout.decode()
# Extract the volume percentage from the output
volume_line = [line for line in output.split('\n') if 'Volume:' in line]
if volume_line:
volume = volume_line[0].split()[4].strip('%')
return int(volume)
return None
def get_brightness():
# Find the correct backlight path
backlight_paths = glob.glob('/sys/class/backlight/*/brightness')
if not backlight_paths:
print("No backlight path found")
return None
brightness_path = backlight_paths[0]
max_brightness_path = brightness_path.replace('brightness', 'max_brightness')
# Read the brightness and max brightness values
try:
with open(brightness_path, 'r') as file:
brightness = int(file.read().strip())
with open(max_brightness_path, 'r') as file:
max_brightness = int(file.read().strip())
# Calculate the brightness level as a percentage
brightness_level = round((brightness / max_brightness) * 100, 2)
return brightness_level
except FileNotFoundError:
print("Brightness file not found")
return None
except Exception as e:
print(f"An error occurred: {e}")
return None
def get_battery_state():
battery = psutil.sensors_battery()
if battery:
return round(battery.percent, 2), battery.power_plugged, battery.secsleft
return None, None, None
def get_keyboard():
result = subprocess.run(['niri', 'msg', 'keyboard-layouts'], stdout=subprocess.PIPE)
output = result.stdout.decode()
line = [line for line in output.split('\n') if '*' in line][0]
line = line.translate(str.maketrans('*0123456789', ' ' * 11))
line = line.strip()
return line