2
0
Fork 0
mirror of https://github.com/ArchiveBox/ArchiveBox synced 2025-03-06 16:17:22 +00:00

allow setting config via aliases and show side effect changes in stderr

This commit is contained in:
Nick Sweeting 2019-04-26 14:43:13 -04:00
parent 05f327fab0
commit 2b4244bb52
2 changed files with 47 additions and 7 deletions

View file

@ -13,10 +13,14 @@ from ..legacy.util import SmartFormatter
from ..legacy.config import (
check_data_folder,
OUTPUT_DIR,
load_all_config,
write_config_file,
CONFIG,
CONFIG_FILE,
USER_CONFIG,
ConfigDict,
stderr,
get_real_name,
)
@ -69,6 +73,7 @@ def main(args: List[str]=None, stdin: Optional[str]=None) -> None:
matching_config: ConfigDict = {}
if command.get or no_args:
if config_options:
config_options = [get_real_name(key) for key in config_options]
matching_config = {key: CONFIG[key] for key in config_options if key in CONFIG}
failed_config = [key for key in config_options if key not in CONFIG]
if failed_config:
@ -79,7 +84,7 @@ def main(args: List[str]=None, stdin: Optional[str]=None) -> None:
else:
matching_config = CONFIG
print('\n'.join(f'{key}={val}' for key, val in matching_config.items()))
print(printable_config(matching_config))
raise SystemExit(not matching_config)
elif command.set:
new_config = {}
@ -92,15 +97,32 @@ def main(args: List[str]=None, stdin: Optional[str]=None) -> None:
stderr(f' {line}')
raise SystemExit(2)
key, val = line.split('=')
if key.upper().strip() in CONFIG:
new_config[key.upper().strip()] = val.strip()
raw_key, val = line.split('=')
raw_key = raw_key.upper().strip()
key = get_real_name(raw_key)
if key != raw_key:
stderr(f'[i] Note: The config option {raw_key} has been renamed to {key}, please use the new name going forwards.', color='lightyellow')
if key in CONFIG:
new_config[key] = val.strip()
else:
failed_options.append(line)
if new_config:
before = CONFIG
matching_config = write_config_file(new_config, out_dir=OUTPUT_DIR)
print('\n'.join(f'{key}={val}' for key, val in matching_config.items()))
after = load_all_config()
print(printable_config(matching_config))
side_effect_changes: ConfigDict = {}
for key, val in after.items():
if key in USER_CONFIG and (before[key] != after[key]) and (key not in matching_config):
side_effect_changes[key] = after[key]
if side_effect_changes:
stderr()
stderr('[i] Note: This change also affected these other options that depended on it:', color='lightyellow')
print(' {}'.format(printable_config(side_effect_changes, prefix=' ')))
if failed_options:
stderr()
stderr('[X] These options failed to set:', color='red')
@ -113,5 +135,13 @@ def main(args: List[str]=None, stdin: Optional[str]=None) -> None:
stderr(' archivebox config --set SOME_KEY=SOME_VALUE')
raise SystemExit(2)
def printable_config(config: ConfigDict, prefix: str='') -> str:
return f'\n{prefix}'.join(
f'{key}={val}'
for key, val in config.items()
if not (isinstance(val, dict) or callable(val))
)
if __name__ == '__main__':
main()

View file

@ -92,6 +92,16 @@ CONFIG_DEFAULTS: Dict[str, ConfigDefaultDict] = {
},
}
CONFIG_ALIASES = {
alias: key
for section in CONFIG_DEFAULTS.values()
for key, default in section.items()
for alias in default.get('aliases', ())
}
USER_CONFIG = {key for section in CONFIG_DEFAULTS.values() for key in section.keys()}
def get_real_name(key: str) -> str:
return CONFIG_ALIASES.get(key.upper().strip(), key.upper().strip())
############################## Derived Config ##############################
# Constants
@ -275,7 +285,7 @@ def load_config_file(out_dir: str=None) -> Optional[Dict[str, str]]:
return config_file_vars
return None
def write_config_file(config: Dict[str, str], out_dir: str=None) -> Optional[Dict[str, str]]:
def write_config_file(config: Dict[str, str], out_dir: str=None) -> ConfigDict:
"""load the ini-formatted config file from OUTPUT_DIR/Archivebox.conf"""
out_dir = out_dir or os.path.abspath(os.getenv('OUTPUT_DIR', '.'))
@ -285,7 +295,7 @@ def write_config_file(config: Dict[str, str], out_dir: str=None) -> Optional[Dic
f.write(CONFIG_HEADER)
config_file = ConfigParser()
config_file.optionxform = str
config_file.optionxform = str
config_file.read(config_path)
find_section = lambda key: [name for name, opts in CONFIG_DEFAULTS.items() if key in opts][0]