forked from Shinonome/dots-hyprland
hyprconfigurator: create conf file if doesn't exist
This commit is contained in:
@@ -5,12 +5,11 @@ import os
|
|||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
def edit_hyprland_config(file_path, set_args, reset_args):
|
def edit_hyprland_config(file_path, set_args, reset_args):
|
||||||
try:
|
if os.path.exists(file_path):
|
||||||
with open(file_path, 'r') as file:
|
with open(file_path, 'r') as file:
|
||||||
lines = file.readlines()
|
lines = file.readlines()
|
||||||
except FileNotFoundError:
|
else:
|
||||||
print(f"Error: File '{file_path}' not found.")
|
lines = []
|
||||||
return
|
|
||||||
|
|
||||||
set_dict = {k: v for k, v in set_args} if set_args else {}
|
set_dict = {k: v for k, v in set_args} if set_args else {}
|
||||||
reset_set = set(reset_args) if reset_args else set()
|
reset_set = set(reset_args) if reset_args else set()
|
||||||
@@ -56,12 +55,18 @@ def edit_hyprland_config(file_path, set_args, reset_args):
|
|||||||
new_lines.append(f"{key} = {value}\n")
|
new_lines.append(f"{key} = {value}\n")
|
||||||
|
|
||||||
dir_name = os.path.dirname(os.path.abspath(file_path))
|
dir_name = os.path.dirname(os.path.abspath(file_path))
|
||||||
|
os.makedirs(dir_name, exist_ok=True)
|
||||||
temp_path = None
|
temp_path = None
|
||||||
try:
|
try:
|
||||||
with tempfile.NamedTemporaryFile(mode='w', dir=dir_name, delete=False) as temp_file:
|
with tempfile.NamedTemporaryFile(mode='w', dir=dir_name, delete=False) as temp_file:
|
||||||
temp_file.writelines(new_lines)
|
temp_file.writelines(new_lines)
|
||||||
temp_path = temp_file.name
|
temp_path = temp_file.name
|
||||||
os.chmod(temp_path, os.stat(file_path).st_mode)
|
|
||||||
|
if os.path.exists(file_path):
|
||||||
|
os.chmod(temp_path, os.stat(file_path).st_mode)
|
||||||
|
else:
|
||||||
|
os.chmod(temp_path, 0o644)
|
||||||
|
|
||||||
os.replace(temp_path, file_path)
|
os.replace(temp_path, file_path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if temp_path and os.path.exists(temp_path):
|
if temp_path and os.path.exists(temp_path):
|
||||||
|
|||||||
Reference in New Issue
Block a user