From 908d3b294f404e0af71cc182dc15106369025d56 Mon Sep 17 00:00:00 2001
From: Karl-Hermann Wieners <karl-hermann.wieners@mpimet.mpg.de>
Date: Sun, 2 Mar 2025 15:57:30 +0100
Subject: [PATCH] setconfig: make deletion of missing keys non-fatal

---
 CHANGES.rst |  5 +++++
 setconfig   | 11 +++++++----
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/CHANGES.rst b/CHANGES.rst
index 664c1fd..cbe36e9 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -20,6 +20,11 @@ Config
 * Setting to skip reading of the default config file (``.NO_DEFAULT = true``)
 * Add function to convert ISO periods to seconds (period2sec)
 
+Tools
+-----
+
+* Make deletion of missing keys non-fatal for setconfig
+
 Release 1.4.1
 =============
 
diff --git a/setconfig b/setconfig
index 561f5f5..dfad453 100755
--- a/setconfig
+++ b/setconfig
@@ -15,7 +15,7 @@ import sys
 from expconfig import ConfigObj
 
 from expargparse import assigns_to_dicts, get_key_chain
-from feedback import die
+from feedback import die, warn
 import package_info
 
 #
@@ -70,9 +70,12 @@ for current in args.delete:
     chain = get_key_chain(current)
     chain.reverse()
     key = chain.pop()
-    for section in chain:
-        config = config[section]
-    del config[key]
+    try:
+        for section in chain:
+            config = config[section]
+        del config[key]
+    except KeyError:
+        warn(f"key to be deleted ({current}) does not exist")
 
 # Merge key=value assignments from command line
 
-- 
GitLab