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