summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2014-04-11 00:43:22 +0200
committermathieui <mathieui@mathieui.net>2014-04-11 00:43:22 +0200
commitd18fe6c477332afd2a81a4723c2951c8328d0cf1 (patch)
tree6dac61a5d9078b69264fb8fed3587992f6f80d5a
parenta1b20551276f2017256453304216988d6f094bd8 (diff)
downloadpoezio-d18fe6c477332afd2a81a4723c2951c8328d0cf1.tar.gz
poezio-d18fe6c477332afd2a81a4723c2951c8328d0cf1.tar.bz2
poezio-d18fe6c477332afd2a81a4723c2951c8328d0cf1.tar.xz
poezio-d18fe6c477332afd2a81a4723c2951c8328d0cf1.zip
Do not traceback when unable to read the config file
-rw-r--r--src/config.py23
1 files changed, 19 insertions, 4 deletions
diff --git a/src/config.py b/src/config.py
index 1bfad784..0e27372b 100644
--- a/src/config.py
+++ b/src/config.py
@@ -144,10 +144,15 @@ class Config(RawConfigParser):
Just find the right section, and then find the
right option, and edit it.
"""
- if path.exists(self.file_name):
- df = open(self.file_name, 'r', encoding='utf-8')
- lines_before = (line.strip() for line in df.readlines())
- df.close()
+ if file_ok(self.file_name):
+ try:
+ with open(self.file_name, 'r', encoding='utf-8') as df:
+ lines_before = (line.strip() for line in df.readlines())
+ except:
+ log.error('Unable to read the config file %s',
+ self.file_name,
+ exc_info=True)
+ return False
else:
lines_before = []
result_lines = []
@@ -176,6 +181,7 @@ class Config(RawConfigParser):
result_lines.append('%s = %s' % (option, value))
elif not written:
result_lines.append('%s = %s' % (option, value))
+
try:
prefix, file = path.split(self.file_name)
filename = path.join(prefix, '.%s.tmp' % file)
@@ -257,6 +263,15 @@ class Config(RawConfigParser):
return res
+def file_ok(filepath):
+ """
+ Returns True if the file exists and is readable and writeable,
+ False otherwise.
+ """
+ val = path.exists(filepath)
+ val &= os.access(filepath, os.R_OK | os.W_OK)
+ return bool(val)
+
def check_create_config_dir():
"""
create the configuration directory if it doesn't exist