diff options
Diffstat (limited to 'git/config.py')
-rw-r--r-- | git/config.py | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/git/config.py b/git/config.py index 4c4cb491..38dd1b44 100644 --- a/git/config.py +++ b/git/config.py @@ -23,6 +23,7 @@ from git.compat import ( string_types, FileType, defenc, + force_text, with_metaclass, PY3 ) @@ -412,7 +413,7 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje fp.write(("[%s]\n" % name).encode(defenc)) for (key, value) in section_dict.items(): if key != "__name__": - fp.write(("\t%s = %s\n" % (key, str(value).replace('\n', '\n\t'))).encode(defenc)) + fp.write(("\t%s = %s\n" % (key, self._value_to_string(value).replace('\n', '\n\t'))).encode(defenc)) # END if key is not __name__ # END section writing @@ -478,8 +479,6 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje if self.read_only: raise IOError("Cannot execute non-constant method %s.%s" % (self, method_name)) - @needs_values - @set_dirty_and_flush_changes def add_section(self, section): """Assures added options will stay in order""" return super(GitConfigParser, self).add_section(section) @@ -531,6 +530,11 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje return valuestr + def _value_to_string(self, value): + if isinstance(value, (int, float, bool)): + return str(value) + return force_text(value) + @needs_values @set_dirty_and_flush_changes def set_value(self, section, option, value): @@ -542,7 +546,29 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje :param option: Name of the options whose value to set :param value: Value to set the option to. It must be a string or convertible - to a string""" + to a string + :return: this instance""" if not self.has_section(section): self.add_section(section) - self.set(section, option, str(value)) + self.set(section, option, self._value_to_string(value)) + return self + + def rename_section(self, section, new_name): + """rename the given section to new_name + :raise ValueError: if section doesn't exit + :raise ValueError: if a section with new_name does already exist + :return: this instance + """ + if not self.has_section(section): + raise ValueError("Source section '%s' doesn't exist" % section) + if self.has_section(new_name): + raise ValueError("Destination section '%s' already exists" % new_name) + + super(GitConfigParser, self).add_section(new_name) + for k, v in self.items(section): + self.set(new_name, k, self._value_to_string(v)) + # end for each value to copy + + # This call writes back the changes, which is why we don't have the respective decorator + self.remove_section(section) + return self |