diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2009-12-08 20:01:17 +0100 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2009-12-08 20:01:17 +0100 |
commit | 6a417f4cf2df39704aa4c869e88d14e9806894a7 (patch) | |
tree | d11d42889010b43e980f26b939a8368fb861f07f | |
parent | c76a8c5499d22b9c0b4c56f03b671033eb9f9bdd (diff) | |
download | gitpython-6a417f4cf2df39704aa4c869e88d14e9806894a7.tar.gz |
config.set_value: added more convenient set_value method to ConfigReader
-rw-r--r-- | lib/git/config.py | 20 | ||||
-rw-r--r-- | test/git/test_config.py | 6 |
2 files changed, 26 insertions, 0 deletions
diff --git a/lib/git/config.py b/lib/git/config.py index 1739e786..e8bdfc75 100644 --- a/lib/git/config.py +++ b/lib/git/config.py @@ -387,3 +387,23 @@ class GitConfigParser(cp.RawConfigParser, LockFile): raise TypeError( "Invalid value type: only int, long, float and str are allowed", valuestr ) return valuestr + + @_needs_values + @_set_dirty_and_flush_changes + def set_value(self, section, option, value): + """Sets the given option in section to the given value. + It will create the section if required, and will not throw as opposed to the default + ConfigParser 'set' method. + + ``section`` + Name of the section in which the option resides or should reside + + ``option`` + Name of the options whose value to set + + ``value`` + Value to set the option to. It must be a string or convertible to a string + """ + if not self.has_section(section): + self.add_section(section) + self.set(section, option, str(value)) diff --git a/test/git/test_config.py b/test/git/test_config.py index e3d723a0..6103ab8d 100644 --- a/test/git/test_config.py +++ b/test/git/test_config.py @@ -49,6 +49,12 @@ class TestBase(TestCase): assert w_config.has_option(sname,oname) assert w_config.get(sname, oname) == val + sname_new = "new_section" + oname_new = "new_key" + ival = 10 + w_config.set_value(sname_new, oname_new, ival) + assert w_config.get_value(sname_new, oname_new) == ival + file_obj.seek(0) r_config = GitConfigParser(file_obj, read_only=True) assert r_config.has_section(sname) |