-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle-touchpad.py
More file actions
executable file
·89 lines (73 loc) · 2.87 KB
/
toggle-touchpad.py
File metadata and controls
executable file
·89 lines (73 loc) · 2.87 KB
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
#!/usr/bin/env python3
import os
import sys
from pathlib import Path
def main() -> None:
"""Main Entry point of our script."""
config_path = Path("~/.config/niri/config.kdl").expanduser()
config, toggle_off_line_num = open_config(config_path)
if toggle_off_line_num:
toggle_on_off(toggle_off_line_num, config)
write_config(config_path, config)
else:
sys.exit(1)
def open_config(path: Path) -> tuple[list[str], int | None]:
"""Opens and reads the niri config file config.kdl This function returns our
configuration file as a tuple containing a list containing each line in the
configuration and an int our line number that our inline comment toggleOffTouchpad
is located at.
:param path: The path to niri configuration file.
:type path: Path
:return: A tuple(list[str], int)
:rtype: tuple[list[str], int | None]
:raises FileNotFoundError: If the path does not exist.
:raises ValueError: If config.kdl is not found.
:raises PermissionError: If user lacks r/w/x permissions.
"""
if not path.exists():
raise FileNotFoundError(f"Config file not found at location: {path}")
if not path.is_file():
raise ValueError(f"File does not exist: {path}")
if not os.access(path, os.R_OK):
raise PermissionError(f"No read persmission: {path}")
contents = []
toggle_off_line_num = None
with open(path, "r") as f:
for line_num, line in enumerate(f):
if "toggleOffTouchpad" in line:
toggle_off_line_num = line_num
contents.append(line)
else:
contents.append(line)
return contents, toggle_off_line_num
def write_config(path: Path, config: list) -> None:
"""Write Config function rewrites our configuration file setting either toucpad off
or // off.
:param path: The path to niri configuration file.
:type path: Path
:param config: Our newly written configuration file.
"""
with open(path, "w") as f:
for line in config:
f.write(line)
def toggle_on_off(line_num: int, contents: list) -> list:
"""Toggle function sets the touchpad setting in the niri configuration file to off
or // off while keeping users indentation.
:param line_num: line number of the setting identified by the inline comment.
:type line_num: int
:param contents: our configuration file as type list.
:type contents: list
:return: A list the modified configuration file.
:rtype: list
"""
toggle = contents[line_num]
leading_spaces = len(toggle) - len(toggle.lstrip())
indent = " " * leading_spaces
if "// off // toggleOffTouchpad" in toggle:
toggle = f"{indent}off // toggleOffTouchpad\n"
else:
toggle = f"{indent}// off // toggleOffTouchpad\n"
contents[line_num] = toggle
return contents
if __name__ == "__main__":
main()