Minor refactor

This commit is contained in:
Luflosi 2019-03-14 18:39:13 +01:00
parent 764a058cf6
commit a28710c1fc
No known key found for this signature in database
GPG Key ID: 14140F703B7D8362

View File

@ -60,29 +60,30 @@ def parse_line(line, type_map, special_handling, ans, all_keys, base_path_for_in
if not line or line.startswith('#'):
return
m = key_pat.match(line)
if m is not None:
key, val = m.groups()
if special_handling(key, val, ans):
return
if key == 'include':
val = os.path.expandvars(os.path.expanduser(val.strip()))
if not os.path.isabs(val):
val = os.path.join(base_path_for_includes, val)
try:
with open(val, encoding='utf-8', errors='replace') as include:
_parse(include, type_map, special_handling, ans, all_keys)
except FileNotFoundError:
log_error('Could not find included config file: {}, ignoring'.format(val))
except EnvironmentError:
log_error('Could not read from included config file: {}, ignoring'.format(val))
return
if all_keys is not None and key not in all_keys:
log_error('Ignoring unknown config key: {}'.format(key))
return
tm = type_map.get(key)
if tm is not None:
val = tm(val)
ans[key] = val
if m is None:
return
key, val = m.groups()
if special_handling(key, val, ans):
return
if key == 'include':
val = os.path.expandvars(os.path.expanduser(val.strip()))
if not os.path.isabs(val):
val = os.path.join(base_path_for_includes, val)
try:
with open(val, encoding='utf-8', errors='replace') as include:
_parse(include, type_map, special_handling, ans, all_keys)
except FileNotFoundError:
log_error('Could not find included config file: {}, ignoring'.format(val))
except EnvironmentError:
log_error('Could not read from included config file: {}, ignoring'.format(val))
return
if all_keys is not None and key not in all_keys:
log_error('Ignoring unknown config key: {}'.format(key))
return
tm = type_map.get(key)
if tm is not None:
val = tm(val)
ans[key] = val
def _parse(lines, type_map, special_handling, ans, all_keys):