summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2012-02-09 15:45:18 +0100
committermathieui <mathieui@mathieui.net>2012-02-09 15:45:18 +0100
commit6df406b7c1c06b8c7c58cd74d028310008cea6a4 (patch)
tree28568777bdfb916a13115f3525f8a40920721a5c
parent42c8deb9e19bde492c3f17d8f07f5b277eee4d04 (diff)
downloadpoezio-6df406b7c1c06b8c7c58cd74d028310008cea6a4.tar.gz
poezio-6df406b7c1c06b8c7c58cd74d028310008cea6a4.tar.bz2
poezio-6df406b7c1c06b8c7c58cd74d028310008cea6a4.tar.xz
poezio-6df406b7c1c06b8c7c58cd74d028310008cea6a4.zip
Config.write_in_file() now works as expected
(can add sections, add inexistant options, and edit in place)
-rw-r--r--src/config.py27
1 files changed, 21 insertions, 6 deletions
diff --git a/src/config.py b/src/config.py
index af9c9fbe..2ee4abb1 100644
--- a/src/config.py
+++ b/src/config.py
@@ -113,16 +113,31 @@ class Config(RawConfigParser):
df.close()
result_lines = []
we_are_in_the_right_section = False
+ written = False
+ section_found = False
for line in lines_before:
if line.startswith('['): # check the section
+ if we_are_in_the_right_section and not written:
+ result_lines.append('%s= %s' % (option, value))
if line == '[%s]' % section:
we_are_in_the_right_section = True
+ section_found = True
else:
we_are_in_the_right_section = False
if (line.startswith('%s ' % (option,)) or
- line.startswith('%s=' % (option,))) and we_are_in_the_right_section:
- line = '%s = %s' % (option, value)
+ line.startswith('%s=' % (option,)) or
+ line.startswith('%s = ' % (option,))) and we_are_in_the_right_section:
+ line = '%s= %s' % (option, value)
+ written = True
result_lines.append(line)
+
+ if not section_found:
+ result_lines.append('[%s]' % section)
+ result_lines.append('%s= %s' % (option, value))
+ elif not written:
+ result_lines.append('%s= %s' % (option, value))
+
+
df = open(self.file_name, 'w')
for line in result_lines:
df.write('%s\n' % line)
@@ -133,11 +148,11 @@ class Config(RawConfigParser):
set the value in the configuration then save it
to the file
"""
- try:
+ if self.has_section(section):
+ RawConfigParser.set(self, section, option, value)
+ else:
+ self.add_section(section)
RawConfigParser.set(self, section, option, value)
- except NoSectionError:
- # TODO, add this section if it didn't exist
- return
self.write_in_file(section, option, value)
def set(self, option, value, section=DEFSECTION):