big updates
This commit is contained in:
1 parent
0c82692536
commit
c7c8ea67c7
42 files changed
+1083
-665
No files matched your search
+72
-38
@@ -1,6 +1,6 @@
|
||||
import argparse
|
||||
import re
|
||||
import os
|
||||
import re
|
||||
import json
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
@@ -9,28 +9,29 @@ group.add_argument('--browser', type=str, help="Change default browser")
|
||||
group.add_argument('--filemanager', type=str, help="Change file manager")
|
||||
group.add_argument('--editor', type=str, help="Change default editor")
|
||||
group.add_argument('--terminal', type=str, help="Change default terminal")
|
||||
group.add_argument('--get', type=str, help="Get default app", choices=["browser", "editor", "terminal", "filemanager"])
|
||||
group.add_argument('--get', type=str, help="Get default app",
|
||||
choices=["browser", "editor", "terminal", "filemanager"])
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
config_file = os.path.expanduser("~/dotfiles/hypr/UserConfigs/Apps.conf")
|
||||
json_config_file = os.path.expanduser("~/dotfiles/ags/assets/apps.json")
|
||||
browser = args.browser
|
||||
filemanager = args.filemanager
|
||||
editor = args.editor
|
||||
terminal = args.terminal
|
||||
get = args.get
|
||||
|
||||
def read_apps_conf():
|
||||
with open(config_file) as f:
|
||||
return f.read().strip()
|
||||
value = (args.browser or args.filemanager or
|
||||
args.editor or args.terminal or args.get)
|
||||
|
||||
def write_apps_conf(content):
|
||||
with open(config_file, 'w') as f:
|
||||
f.write(content + '\n')
|
||||
u = os.path.expanduser
|
||||
|
||||
def read_json_config():
|
||||
with open(json_config_file) as json_file:
|
||||
return json.load(json_file)
|
||||
CONFIG_FILE = u("~/dotfiles/hypr/conf/apps.conf")
|
||||
JSON_CONFIG_FILE = u("~/dotfiles/.settings/apps.json")
|
||||
|
||||
with open(CONFIG_FILE) as f:
|
||||
original_env_str = f.read().strip()
|
||||
env_str = original_env_str
|
||||
|
||||
def write_json_config(config):
|
||||
with open(json_config_file, 'w') as json_file:
|
||||
json.dump(config, json_file, indent=4)
|
||||
|
||||
def which(program: str):
|
||||
def is_executable(fpath):
|
||||
@@ -48,8 +49,12 @@ def which(program: str):
|
||||
return exe_file
|
||||
raise ProgramNotFound(value)
|
||||
|
||||
|
||||
def extract_associations(env_str):
|
||||
pattern = re.compile(r'^\s*env\s*=\s*(\w+)\s*,\s*(.*?)\s*#\s*!\s*-\s*@(\w+)\s*$', re.MULTILINE) # noqa
|
||||
pattern = re.compile(
|
||||
r'^\s*env\s*=\s*(\w+)\s*,\s*(.*?)\s*#\s*!\s*-\s*@(\w+)\s*$',
|
||||
re.MULTILINE
|
||||
)
|
||||
associations = {}
|
||||
matches = pattern.findall(env_str)
|
||||
for key, _, association in matches:
|
||||
@@ -59,53 +64,82 @@ def extract_associations(env_str):
|
||||
associations[association].append(key)
|
||||
return associations
|
||||
|
||||
|
||||
associations = extract_associations(env_str)
|
||||
|
||||
|
||||
def replace_value(env_str, key, new_value):
|
||||
pattern = re.compile(
|
||||
r'^(\s*env\s*=\s*' +
|
||||
re.escape(key) +
|
||||
r'\s*,\s*)(.*?)\s*(#.*)?$', re.MULTILINE
|
||||
r'\s*,\s*)(.*?)\s*(#.*)?$',
|
||||
re.MULTILINE
|
||||
)
|
||||
return re.sub(pattern, r'\1' + new_value + r' \3', env_str)
|
||||
|
||||
|
||||
class AssociationNotFound(Exception):
|
||||
def __init__(self, name: str, *args: object) -> None:
|
||||
error = f"{name} association not found"
|
||||
error = f"{name} association not found, \n{associations}"
|
||||
super().__init__(error, *args)
|
||||
|
||||
|
||||
class ProgramNotFound(Exception):
|
||||
def __init__(self, name: str, *args: object) -> None:
|
||||
error = f"{name} not found"
|
||||
super().__init__(error, *args)
|
||||
|
||||
|
||||
def change_association(key: str, value: str):
|
||||
env_str = read_apps_conf()
|
||||
global env_str
|
||||
which(value)
|
||||
associations = extract_associations(env_str)
|
||||
if key not in associations:
|
||||
raise AssociationNotFound(key)
|
||||
|
||||
for key in associations[key]:
|
||||
env_str = replace_value(env_str, key, value)
|
||||
for assoc_key in associations[key]:
|
||||
env_str = replace_value(env_str, assoc_key, value)
|
||||
|
||||
|
||||
def read_json_config():
|
||||
try:
|
||||
with open(JSON_CONFIG_FILE) as json_file:
|
||||
return json.load(json_file)
|
||||
except IOError as e:
|
||||
print(f"Error reading {JSON_CONFIG_FILE}: {e}")
|
||||
raise
|
||||
|
||||
|
||||
def write_json_config(config):
|
||||
try:
|
||||
with open(JSON_CONFIG_FILE, 'w') as json_file:
|
||||
json.dump(config, json_file, indent=4)
|
||||
except IOError as e:
|
||||
print(f"Error writing to {JSON_CONFIG_FILE}: {e}")
|
||||
raise
|
||||
|
||||
write_apps_conf(env_str)
|
||||
|
||||
def update_json_config(key: str, value: str):
|
||||
config = read_json_config()
|
||||
config[key] = value
|
||||
write_json_config(config)
|
||||
|
||||
if args.get:
|
||||
|
||||
if get:
|
||||
json_config = read_json_config()
|
||||
print(json_config[args.get])
|
||||
elif args.browser:
|
||||
change_association("browser", args.browser)
|
||||
update_json_config("browser", args.browser)
|
||||
elif args.filemanager:
|
||||
change_association("filemanager", args.filemanager)
|
||||
update_json_config("filemanager", args.filemanager)
|
||||
elif args.editor:
|
||||
change_association("editor", args.editor)
|
||||
update_json_config("editor", args.editor)
|
||||
elif args.terminal:
|
||||
change_association("terminal", args.terminal)
|
||||
update_json_config("terminal", args.terminal)
|
||||
print(json_config[get])
|
||||
elif browser:
|
||||
change_association("browser", value)
|
||||
update_json_config("browser", value)
|
||||
elif filemanager:
|
||||
change_association("filemanager", value)
|
||||
update_json_config("filemanager", value)
|
||||
elif editor:
|
||||
change_association("editor", value)
|
||||
update_json_config("editor", value)
|
||||
elif terminal:
|
||||
change_association("terminal", value)
|
||||
update_json_config("terminal", value)
|
||||
|
||||
if env_str != original_env_str:
|
||||
with open(CONFIG_FILE, 'w') as f:
|
||||
f.write(env_str + '\n')
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
GENERATOR="$HOME/dotfiles/material-colors/generate.py"
|
||||
SETTINGS_FILE="$HOME/dotfiles/ags/assets/settings.json"
|
||||
SETTINGS_FILE="$HOME/dotfiles/.settings/settings.json"
|
||||
|
||||
# Function to read a value from the JSON file
|
||||
get_json_value() {
|
||||
|
||||
Reference in new issue
Block a user