Commit d18fe6c4 authored by mathieui's avatar mathieui

Do not traceback when unable to read the config file

parent a1b20551
...@@ -144,10 +144,15 @@ class Config(RawConfigParser): ...@@ -144,10 +144,15 @@ class Config(RawConfigParser):
Just find the right section, and then find the Just find the right section, and then find the
right option, and edit it. right option, and edit it.
""" """
if path.exists(self.file_name): if file_ok(self.file_name):
df = open(self.file_name, 'r', encoding='utf-8') try:
lines_before = (line.strip() for line in df.readlines()) with open(self.file_name, 'r', encoding='utf-8') as df:
df.close() 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: else:
lines_before = [] lines_before = []
result_lines = [] result_lines = []
...@@ -176,6 +181,7 @@ class Config(RawConfigParser): ...@@ -176,6 +181,7 @@ class Config(RawConfigParser):
result_lines.append('%s = %s' % (option, value)) result_lines.append('%s = %s' % (option, value))
elif not written: elif not written:
result_lines.append('%s = %s' % (option, value)) result_lines.append('%s = %s' % (option, value))
try: try:
prefix, file = path.split(self.file_name) prefix, file = path.split(self.file_name)
filename = path.join(prefix, '.%s.tmp' % file) filename = path.join(prefix, '.%s.tmp' % file)
...@@ -257,6 +263,15 @@ class Config(RawConfigParser): ...@@ -257,6 +263,15 @@ class Config(RawConfigParser):
return res 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(): def check_create_config_dir():
""" """
create the configuration directory if it doesn't exist create the configuration directory if it doesn't exist
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment