optimization

This commit is contained in:
sankalp committed 2024-07-31 06:43:26 +05:45
1 parent 7289ca07fe
commit 5be5a41789
14 files changed
+353 -277

No files matched your search

+8 -11
View File
@@ -1,6 +1,10 @@
#!/bin/bash
# I tried to make it smooth, but it didn't work
wl_gammarelay() {
if ! busctl --user list | grep rs.wl.gammarelay; then
systemctl --user restart wl-gammarelay.service
fi
}
wl_gammarelay >/dev/null
LOCKFILE=/tmp/brightness.lock
@@ -9,19 +13,12 @@ get_brightness() {
}
set_brightness() {
# if [ -e "$LOCKFILE" ] && kill -0 "$(cat $LOCKFILE)"; then
# exit 0
# fi
# trap "rm -f $LOCKFILE; exit" INT TERM EXIT
# echo $$ > "$LOCKFILE"
if (( $(echo "$1 < 0.05" | bc -l) )); then
if (($(echo "$1 < 0.05" | bc -l))); then
BRIGHTNESS=0.05
else
BRIGHTNESS=$1
fi
busctl --user -- set-property rs.wl-gammarelay / rs.wl.gammarelay Brightness d $BRIGHTNESS
# rm -f "$LOCKFILE"
# trap - INT TERM EXIT
}
set() {
@@ -46,6 +43,6 @@ get() {
if [[ "$1" == "--set" ]]; then
set $2
elif [[ "$1" == "--get" ]]; then
elif [[ "$1" == "--get" ]]; then
get
fi
+53 -30
View File
@@ -15,12 +15,15 @@ variables_dict: dict[str, str] = {}
category_pattern = r"\n#! @(\w+): (.+)"
variable_pattern = r"\n\$(\w+)\s*=\s*(.*)"
text = ""
bindings: dict[str, dict[str, str]] = {}
def extract_category_and_description(line: str) -> tuple[str | None, str | None, tuple | None]:
pattern2 = r'bind\s*=\s*.*! @description:\s*"((?:[^"]|\\")*)"\s*;\s*@(\w+);\s*@replace\s*"([^"]*)"\s*>\s*"([^"]*)";.*'
pattern = r'bind\s*=\s*.*! @description:\s*"((?:[^"]|\\")*)"\s*;\s*@(\w+);.*'
def extract_category_and_description(
line: str
) -> tuple[str | None, str | None, tuple | None]:
pattern2 = r'bind\s*=\s*.*! @description:\s*"((?:[^"]|\\")*)"\s*;\s*@(\w+);\s*@replace\s*"([^"]*)"\s*>\s*"([^"]*)";.*' # noqa
pattern = r'bind\s*=\s*.*! @description:\s*"((?:[^"]|\\")*)"\s*;\s*@(\w+);.*' # noqa
match = re.search(pattern, line)
match2 = re.search(pattern2, line)
@@ -66,11 +69,22 @@ def load_categories(content: str):
for key, value in matches:
categories_dict[key] = value
if value not in bindings:
bindings[value] = {}
bindings[value] = {}
return content
def load_sources(file: str):
with open(file) as f:
content = f.read()
for line in content.split("\n"):
line = line.replace(" = ", "=")
if not line.startswith("source="):
continue
file = os.path.expanduser(line.replace("source=", ""))
if file not in files:
files.append(file)
def load_variables(content: str):
matches = re.findall(variable_pattern, content)
@@ -80,12 +94,13 @@ def load_variables(content: str):
content_lines = content.split("\n")
content_lines = [
line for line in content_lines if not line.startswith("$")
]
]
content = "\n".join(content_lines)
return content
def load_file(file: str):
global text
with open(file) as f:
content = f.read()
@@ -94,34 +109,42 @@ def load_file(file: str):
# remove comments
content = re.sub(r'^#.*$\n', '', content, flags=re.MULTILINE)
return content
text += f"\n{content}"
def load():
global bindings
global text, bindings
text = re.sub(r"\$(\w+)", replace_variable, text)
for line in text.split('\n'):
if not line.replace(" ", "").startswith("bind="):
continue
description, category, replace = extract_category_and_description(line)
if category is None or description is None:
continue
binding = extract_binding(line)
if binding is None:
continue
category_name = categories_dict[category]
if category_name not in bindings:
bindings[category_name] = {}
if replace is not None:
binding = binding.replace(*replace)
bindings[category_name][binding.strip()] = description.strip()
while True:
old_list = files
for file in files:
content = load_file(file)
for line in content.split('\n'):
if not line.replace(" ", "").startswith("bind="):
continue
description, category, replace = extract_category_and_description(line)
if category is None or description is None:
continue
binding = extract_binding(line)
if binding is None:
continue
category_name = categories_dict[category]
if category_name not in bindings:
bindings[category_name] = {}
if replace is not None:
binding = binding.replace(*replace)
bindings[category_name][binding.strip()] = description.strip()
load_sources(file)
if old_list == files:
break
for file in files:
load_file(file)
load()
print(json.dumps(bindings, indent=4))