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

View File

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

View File

@ -138,11 +138,16 @@ class ConfigValue:
class KConfManager: 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.data = {}
self.json_dict = json_dict self.json_dict = json_dict
self.filepath = filepath self.filepath = filepath
self.override_config = override_config self.reset = reset
self._json_value_checks() self._json_value_checks()
# The nix expressions will have / to separate groups, and \/ to escape a /. # The nix expressions will have / to separate groups, and \/ to escape a /.
# This parses the groups into tuples of unescaped group names. # This parses the groups into tuples of unescaped group names.
@ -221,12 +226,9 @@ class KConfManager:
# We won't bother reading empty lines. # We won't bother reading empty lines.
if l.strip() != "": if l.strip() != "":
key, value = ConfigValue.parse_line(l) 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) 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( self.set_value(
current_group, current_group,
key, key,
@ -314,27 +316,19 @@ def write_configs(d: Dict, reset_files: Set):
def main(): def main():
if len(sys.argv) != 4: if len(sys.argv) != 3:
raise ValueError( 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] json_path = sys.argv[1]
with open(json_path, "r") as f: with open(json_path, "r") as f:
json_str = f.read() json_str = f.read()
# We send in "true" as the second argument if overrideConfig is enabled in reset_files = set(sys.argv[2].split(" ")) if len(sys.argv) >= 2 else {}
# 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(" "))
d = json.loads(json_str) d = json.loads(json_str)
# If overrideConfig is enabled we delete all the kde config files which are remove_config_files(d, reset_files)
# not configured through plasma-manager. write_configs(d, reset_files)
if override_config:
remove_config_files(d, oc_reset_files)
write_configs(d, oc_reset_files if override_config else {})
if __name__ == "__main__": if __name__ == "__main__":