Add possibility to reset files with overrideConfig disabled (+ some refactoring) (#216)

This commit is contained in:
magnouvean 2024-06-20 13:06:57 +02:00 committed by GitHub
parent d6fc3d0e71
commit 0537704f79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 32 deletions

View File

@ -12,21 +12,20 @@ let
# attribute-set as json. Here a is the attribute-set.
#
# Type: AttrSet -> string
writeConfig = json: overrideConfig: ocRemoveList:
writeConfig = json: overrideConfig: resetFilesList:
let
jsonStr = builtins.toJSON json;
# Writing to file handles special characters better than passing it in as
# an argument to the script.
jsonFile = pkgs.writeText "data.json" jsonStr;
overrideConfigStr = builtins.toString overrideConfig;
ocRemoveStr = builtins.toString
resetFilesStr = builtins.toString
(if overrideConfig then
ocRemoveList ++ [ "${config.xdg.dataHome}/plasma-manager/last_run_*" ]
resetFilesList ++ [ "${config.xdg.dataHome}/plasma-manager/last_run_*" ]
else
ocRemoveList);
resetFilesList);
in
''
${writeConfigScript}/bin/write_config ${jsonFile} "${overrideConfigStr}" "${ocRemoveStr}"
${writeConfigScript}/bin/write_config ${jsonFile} "${resetFilesStr}"
'';
in
{

View File

@ -21,12 +21,12 @@ let
##############################################################################
# Generate a script that will use write_config.py to update all
# settings.
ocRemoveList = (map (f: "${config.xdg.configHome}/${f}") (lib.lists.subtractLists plasmaCfg.overrideConfigExclude plasmaCfg.overrideConfigFiles));
script = pkgs.writeScript "plasma-config" (writeConfig cfg plasmaCfg.overrideConfig ocRemoveList);
resetFilesList = (map (f: "${config.xdg.configHome}/${f}") (lib.lists.subtractLists plasmaCfg.resetFilesExclude plasmaCfg.resetFiles));
script = pkgs.writeScript "plasma-config" (writeConfig cfg plasmaCfg.overrideConfig resetFilesList);
##############################################################################
# Generate a script that will remove all the current config files.
defaultResetFiles = [
defaultResetFiles = (if plasmaCfg.overrideConfig then [
"baloofilerc"
"dolphinrc"
"ffmpegthumbsrc"
@ -34,6 +34,7 @@ let
"katerc"
"kcminputrc"
"kded5rc"
"kded6rc"
"kdeglobals"
"kgammarc"
"kglobalshortcutsrc"
@ -57,7 +58,7 @@ let
"plasmarc"
"plasmashellrc"
"systemsettingsrc"
];
] else [ ]);
in
{
options.programs.plasma = {
@ -99,14 +100,14 @@ in
option.
'';
};
overrideConfigFiles = lib.mkOption {
resetFiles = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = defaultResetFiles;
description = ''
Config-files which should be deleted on each generation.
'';
};
overrideConfigExclude = lib.mkOption {
resetFilesExclude = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = ''
@ -117,6 +118,8 @@ in
imports = [
(lib.mkRenamedOptionModule [ "programs" "plasma" "files" ] [ "programs" "plasma" "configFile" ])
(lib.mkRenamedOptionModule [ "programs" "plasma" "overrideConfigFiles" ] [ "programs" "plasma" "resetFiles" ])
(lib.mkRenamedOptionModule [ "programs" "plasma" "overrideConfigExclude" ] [ "programs" "plasma" "resetFilesExclude" ])
];
config.home.activation = lib.mkIf plasmaCfg.enable {

View File

@ -138,11 +138,16 @@ class ConfigValue:
class KConfManager:
def __init__(self, filepath: str, json_dict: Dict, override_config: bool):
def __init__(self, filepath: str, json_dict: Dict, reset: bool):
"""
filepath (str): The full path to the config-file to manage
json_dict (Dict): The nix-configuration presented in a dictionary (converted from json)
reset (bool): Whether to reset the file, i.e. remove all the lines not present in the configuration
"""
self.data = {}
self.json_dict = json_dict
self.filepath = filepath
self.override_config = override_config
self.reset = reset
self._json_value_checks()
# The nix expressions will have / to separate groups, and \/ to escape a /.
# This parses the groups into tuples of unescaped group names.
@ -221,12 +226,9 @@ class KConfManager:
# We won't bother reading empty lines.
if l.strip() != "":
key, value = ConfigValue.parse_line(l)
# We only read the current key if overrideConfig is
# disabled or the value is marked as persistent in the
# plasma-manager config.
is_persistent = self.key_is_persistent(current_group, key)
if not self.override_config or is_persistent:
should_keep_key = is_persistent or not self.reset
if should_keep_key:
self.set_value(
current_group,
key,
@ -314,27 +316,19 @@ def write_configs(d: Dict, reset_files: Set):
def main():
if len(sys.argv) != 4:
if len(sys.argv) != 3:
raise ValueError(
f"Must receive exactly three arguments, got: {len(sys.argv) - 1}"
f"Must receive exactly two arguments, got: {len(sys.argv) - 1}"
)
json_path = sys.argv[1]
with open(json_path, "r") as f:
json_str = f.read()
# We send in "true" as the second argument if overrideConfig is enabled in
# plasma-manager.
override_config = bool(sys.argv[2])
# The files to be reset when we have overrideConfig enabled.
oc_reset_files = set(sys.argv[3].split(" "))
reset_files = set(sys.argv[2].split(" ")) if len(sys.argv) >= 2 else {}
d = json.loads(json_str)
# If overrideConfig is enabled we delete all the kde config files which are
# not configured through plasma-manager.
if override_config:
remove_config_files(d, oc_reset_files)
write_configs(d, oc_reset_files if override_config else {})
remove_config_files(d, reset_files)
write_configs(d, reset_files)
if __name__ == "__main__":