mirror of
https://github.com/ArchiveBox/ArchiveBox.git
synced 2025-05-17 00:24:26 -04:00
allow setting config via aliases and show side effect changes in stderr
This commit is contained in:
parent
05f327fab0
commit
2b4244bb52
2 changed files with 47 additions and 7 deletions
|
@ -13,10 +13,14 @@ from ..legacy.util import SmartFormatter
|
||||||
from ..legacy.config import (
|
from ..legacy.config import (
|
||||||
check_data_folder,
|
check_data_folder,
|
||||||
OUTPUT_DIR,
|
OUTPUT_DIR,
|
||||||
|
load_all_config,
|
||||||
write_config_file,
|
write_config_file,
|
||||||
CONFIG,
|
CONFIG,
|
||||||
|
CONFIG_FILE,
|
||||||
|
USER_CONFIG,
|
||||||
ConfigDict,
|
ConfigDict,
|
||||||
stderr,
|
stderr,
|
||||||
|
get_real_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -69,6 +73,7 @@ def main(args: List[str]=None, stdin: Optional[str]=None) -> None:
|
||||||
matching_config: ConfigDict = {}
|
matching_config: ConfigDict = {}
|
||||||
if command.get or no_args:
|
if command.get or no_args:
|
||||||
if config_options:
|
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}
|
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]
|
failed_config = [key for key in config_options if key not in CONFIG]
|
||||||
if failed_config:
|
if failed_config:
|
||||||
|
@ -79,7 +84,7 @@ def main(args: List[str]=None, stdin: Optional[str]=None) -> None:
|
||||||
else:
|
else:
|
||||||
matching_config = CONFIG
|
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)
|
raise SystemExit(not matching_config)
|
||||||
elif command.set:
|
elif command.set:
|
||||||
new_config = {}
|
new_config = {}
|
||||||
|
@ -92,15 +97,32 @@ def main(args: List[str]=None, stdin: Optional[str]=None) -> None:
|
||||||
stderr(f' {line}')
|
stderr(f' {line}')
|
||||||
raise SystemExit(2)
|
raise SystemExit(2)
|
||||||
|
|
||||||
key, val = line.split('=')
|
raw_key, val = line.split('=')
|
||||||
if key.upper().strip() in CONFIG:
|
raw_key = raw_key.upper().strip()
|
||||||
new_config[key.upper().strip()] = val.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:
|
else:
|
||||||
failed_options.append(line)
|
failed_options.append(line)
|
||||||
|
|
||||||
if new_config:
|
if new_config:
|
||||||
|
before = CONFIG
|
||||||
matching_config = write_config_file(new_config, out_dir=OUTPUT_DIR)
|
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:
|
if failed_options:
|
||||||
stderr()
|
stderr()
|
||||||
stderr('[X] These options failed to set:', color='red')
|
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')
|
stderr(' archivebox config --set SOME_KEY=SOME_VALUE')
|
||||||
raise SystemExit(2)
|
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__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -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 ##############################
|
############################## Derived Config ##############################
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
|
@ -275,7 +285,7 @@ def load_config_file(out_dir: str=None) -> Optional[Dict[str, str]]:
|
||||||
return config_file_vars
|
return config_file_vars
|
||||||
return None
|
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"""
|
"""load the ini-formatted config file from OUTPUT_DIR/Archivebox.conf"""
|
||||||
|
|
||||||
out_dir = out_dir or os.path.abspath(os.getenv('OUTPUT_DIR', '.'))
|
out_dir = out_dir or os.path.abspath(os.getenv('OUTPUT_DIR', '.'))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue