From 12616394b9e4d3845a8f6797a84518384eda44ba Mon Sep 17 00:00:00 2001 From: Christopher Horn Date: Wed, 8 Jun 2022 22:02:01 -0400 Subject: [PATCH] Add support for embedding comments in ini header --- gramps/gen/test/config_test.py | 13 +++++++++++++ gramps/gen/utils/configmanager.py | 10 +++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/gramps/gen/test/config_test.py b/gramps/gen/test/config_test.py index b61042be1..28cadeaf5 100644 --- a/gramps/gen/test/config_test.py +++ b/gramps/gen/test/config_test.py @@ -139,6 +139,19 @@ class CompleteCheck(unittest.TestCase): self.assertEqual(self.CM.get("section2.dict"), {'a': "apple", "b": "banana"}) self.assertEqual(self.CM.get("section2.unicode"), "Raötröme") + self.assertRaises(AttributeError, self.CM.save, TEST2_INI, comments=123) + self.assertRaises(AttributeError, self.CM.save, TEST2_INI, comments={"key":"pair"}) + self.assertRaises(AttributeError, self.CM.save, TEST2_INI, comments=[123]) + self.assertRaises(AttributeError, self.CM.save, TEST2_INI, comments=["line1", 123]) + + self.CM.save(TEST2_INI, comments=["test comment1"]) + self.CM.load(TEST2_INI) + self.assertEqual(self.CM.get("section.setting3"), "Another String") + + self.CM.save(TEST2_INI, comments=["test comment1", "test comment2"]) + self.CM.load(TEST2_INI) + self.assertEqual(self.CM.get("section.setting3"), "Another String") + if __name__ == "__main__": unittest.main() diff --git a/gramps/gen/utils/configmanager.py b/gramps/gen/utils/configmanager.py index 79d3c8086..8ed27ee0d 100644 --- a/gramps/gen/utils/configmanager.py +++ b/gramps/gen/utils/configmanager.py @@ -326,7 +326,7 @@ class ConfigManager: # this could be a third-party setting; add it: self.data[name][setting] = value - def save(self, filename = None): + def save(self, filename = None, comments = None): """ Saves the current section/settings to an .ini file. Optional filename will override the default filename to save to, if given. @@ -345,6 +345,14 @@ class ConfigManager: key_file.write(";; Gramps key file\n") key_file.write(";; Automatically created at %s" % time.strftime("%Y/%m/%d %H:%M:%S") + "\n\n") + if comments: + if not isinstance(comments, list): + raise AttributeError("Comments should be a list") + key_file.write("\n") + for comment in comments: + if not isinstance(comment, str): + raise AttributeError("Comment should be a string") + key_file.write(";; %s\n" % comment.strip("; \n")) for section in sorted(self.data): key_file.write("[%s]\n" % section) for key in sorted(self.data[section]):