Add type hints to the config reading functions
Return a TypedDict from _read_config() and stop reusing variables for different types, so that mypy can check the config values. No change in behaviour.
This commit is contained in:
@@ -18,7 +18,7 @@ from __future__ import annotations
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import pwd
|
import pwd
|
||||||
from typing import Callable, List, Optional, ParamSpec, TypeVar, Union
|
from typing import Callable, List, Optional, ParamSpec, TypedDict, TypeVar, Union
|
||||||
import warnings
|
import warnings
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from shutil import rmtree, which
|
from shutil import rmtree, which
|
||||||
@@ -766,8 +766,8 @@ def _expand_home(path: str, user: str) -> str:
|
|||||||
return os.path.expanduser(path)
|
return os.path.expanduser(path)
|
||||||
|
|
||||||
|
|
||||||
def _expand_inputs(inputs, user: Optional[str] = None):
|
def _expand_inputs(inputs: List[str], user: Optional[str] = None) -> List[str]:
|
||||||
expanded_inputs = []
|
expanded_inputs: List[str] = []
|
||||||
|
|
||||||
for i in inputs:
|
for i in inputs:
|
||||||
if i == '':
|
if i == '':
|
||||||
@@ -790,8 +790,30 @@ def _expand_inputs(inputs, user: Optional[str] = None):
|
|||||||
return expanded_inputs
|
return expanded_inputs
|
||||||
|
|
||||||
|
|
||||||
def _read_config(config_file, user: Optional[str] = None):
|
class ConfigArgs(TypedDict):
|
||||||
config_args = {'inputs': None,
|
"""Values read from the configuration file"""
|
||||||
|
|
||||||
|
inputs: Optional[List[str]]
|
||||||
|
output: Optional[str]
|
||||||
|
exclude: Optional[List[str]]
|
||||||
|
keep: int
|
||||||
|
ssh_host: Optional[str]
|
||||||
|
ssh_user: Optional[str]
|
||||||
|
ssh_keyfile: Optional[str]
|
||||||
|
remote_sudo: bool
|
||||||
|
numeric_ids: bool
|
||||||
|
|
||||||
|
|
||||||
|
def _get_bool(config: configparser.ConfigParser, key: str) -> bool:
|
||||||
|
# Empty values (as in the example config) count as false
|
||||||
|
if config.get('server', key, fallback='').strip() == '':
|
||||||
|
return False
|
||||||
|
|
||||||
|
return config.getboolean('server', key)
|
||||||
|
|
||||||
|
|
||||||
|
def _read_config(config_file: str, user: Optional[str] = None) -> ConfigArgs:
|
||||||
|
config_args: ConfigArgs = {'inputs': None,
|
||||||
'output': None,
|
'output': None,
|
||||||
'exclude': None,
|
'exclude': None,
|
||||||
'keep': -1,
|
'keep': -1,
|
||||||
@@ -823,29 +845,15 @@ def _read_config(config_file, user: Optional[str] = None):
|
|||||||
section = 'default'
|
section = 'default'
|
||||||
inputs = config.get(section, 'inputs')
|
inputs = config.get(section, 'inputs')
|
||||||
|
|
||||||
inputs = [i.strip() for i in inputs.split(',')]
|
config_args['inputs'] = list(set(_expand_inputs([i.strip() for i in inputs.split(',')], user)))
|
||||||
inputs = _expand_inputs(inputs, user)
|
|
||||||
inputs = list(set(inputs))
|
|
||||||
|
|
||||||
config_args['inputs'] = inputs
|
|
||||||
|
|
||||||
# '~' is expanded later, since for remote backups it refers to the home on the server
|
# '~' is expanded later, since for remote backups it refers to the home on the server
|
||||||
config_args['output'] = config.get(section, 'backup_dir')
|
config_args['output'] = config.get(section, 'backup_dir')
|
||||||
|
|
||||||
try:
|
exclude = config.get(section, 'exclude', fallback='')
|
||||||
exclude = config.get(section, 'exclude')
|
config_args['exclude'] = [e.strip() for e in exclude.split(',') if e.strip() != '']
|
||||||
exclude = [e.strip() for e in exclude.split(',') if e.strip() != '']
|
|
||||||
except configparser.NoOptionError:
|
|
||||||
exclude = []
|
|
||||||
|
|
||||||
config_args['exclude'] = exclude
|
config_args['keep'] = config.getint(section, 'keep', fallback=-1)
|
||||||
|
|
||||||
try:
|
|
||||||
keep = config.getint(section, 'keep')
|
|
||||||
except configparser.NoOptionError:
|
|
||||||
keep = -1
|
|
||||||
|
|
||||||
config_args['keep'] = keep
|
|
||||||
|
|
||||||
# Read host and user separately, so that a missing one is reported instead of silently
|
# Read host and user separately, so that a missing one is reported instead of silently
|
||||||
# disabling remote backup. Empty values count as not set
|
# disabling remote backup. Empty values count as not set
|
||||||
@@ -854,12 +862,8 @@ def _read_config(config_file, user: Optional[str] = None):
|
|||||||
|
|
||||||
config_args['ssh_keyfile'] = config.get('server', 'ssh_keyfile', fallback='').strip() or None
|
config_args['ssh_keyfile'] = config.get('server', 'ssh_keyfile', fallback='').strip() or None
|
||||||
|
|
||||||
# Empty values (as in the example config) count as false
|
config_args['remote_sudo'] = _get_bool(config, 'remote_sudo')
|
||||||
for key in ['remote_sudo', 'numeric_ids']:
|
config_args['numeric_ids'] = _get_bool(config, 'numeric_ids')
|
||||||
if config.get('server', key, fallback='').strip() == '':
|
|
||||||
config_args[key] = False
|
|
||||||
else:
|
|
||||||
config_args[key] = config.getboolean('server', key)
|
|
||||||
|
|
||||||
return config_args
|
return config_args
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user