summaryrefslogtreecommitdiff
path: root/src/config.py
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2013-04-05 23:57:53 +0200
committermathieui <mathieui@mathieui.net>2013-04-05 23:57:53 +0200
commit91b960b797bbf17f6c4b33279f2d406c5a2c93b2 (patch)
tree91cbb61f2618d448416f4a41043ef2327793a53b /src/config.py
parent16268ba96460c0947be7208cba60ad95270a4da7 (diff)
downloadpoezio-91b960b797bbf17f6c4b33279f2d406c5a2c93b2.tar.gz
poezio-91b960b797bbf17f6c4b33279f2d406c5a2c93b2.tar.bz2
poezio-91b960b797bbf17f6c4b33279f2d406c5a2c93b2.tar.xz
poezio-91b960b797bbf17f6c4b33279f2d406c5a2c93b2.zip
Handle I/O errors better
- Do not crash because of low disk space - Notify the user whenever it happens - A few functions now return a boolean instead of nothing - Config.silent_set is Config.set_and_save without toggle and returning strings. It is used whenever we don’t need set_and_save - Config.set_and_save now returns a tuple (that can be passed directly to core.information()) TODO: display the precise error to the user (instead of “unable to…”)
Diffstat (limited to 'src/config.py')
-rw-r--r--src/config.py33
1 files changed, 26 insertions, 7 deletions
diff --git a/src/config.py b/src/config.py
index e3c93f29..c94bb89a 100644
--- a/src/config.py
+++ b/src/config.py
@@ -12,6 +12,8 @@ from/to the config file
DEFSECTION = "Poezio"
+from gettext import gettext as _
+
from configparser import RawConfigParser, NoOptionError, NoSectionError
from os import environ, makedirs, path
from shutil import copy2
@@ -171,10 +173,16 @@ class Config(RawConfigParser):
result_lines.append('%s = %s' % (option, value))
- df = open(self.file_name, 'w', encoding='utf-8')
- for line in result_lines:
- df.write('%s\n' % line)
- df.close()
+ try:
+ df = open(self.file_name, 'w', encoding='utf-8')
+ for line in result_lines:
+ df.write('%s\n' % line)
+ df.close()
+ except:
+ success = False
+ else:
+ success = True
+ return success
def set_and_save(self, option, value, section=DEFSECTION):
"""
@@ -191,15 +199,26 @@ class Config(RawConfigParser):
elif current.lower() == "true":
value = "false"
else:
- return "Could not toggle option: %s. Current value is %s." % (option, current or "empty")
+ return (_("Could not toggle option: %s. Current value is %s.") % (option, current or _("empty")), 'Warning')
if self.has_section(section):
RawConfigParser.set(self, section, option, value)
else:
self.add_section(section)
RawConfigParser.set(self, section, option, value)
- self.write_in_file(section, option, value)
- return "%s=%s" % (option, value)
+ if not self.write_in_file(section, option, value):
+ return (_('Unable to write in the config file'), 'Error')
+ return ("%s=%s" % (option, value), 'Info')
+ def silent_set(self, option, value, section=DEFSECTION):
+ """
+ Set a value, save, and return True on success and False on failure
+ """
+ if self.has_section(section):
+ RawConfigParser.set(self, section, option, value)
+ else:
+ self.add_section(section)
+ RawConfigParser.set(self, section, option, value)
+ return self.write_in_file(section, option, value)
def set(self, option, value, section=DEFSECTION):
"""