MCPcopy
hub / github.com/ArchiveBox/ArchiveBox / write_config_file

Function write_config_file

archivebox/config.py:668–744  ·  view source on GitHub ↗

load the ini-formatted config file from OUTPUT_DIR/Archivebox.conf

(config: Dict[str, str], out_dir: str=None)

Source from the content-addressed store, hash-verified

666
667
668def write_config_file(config: Dict[str, str], out_dir: str=None) -> ConfigDict:
669 """load the ini-formatted config file from OUTPUT_DIR/Archivebox.conf"""
670
671 from .system import atomic_write
672
673 CONFIG_HEADER = (
674 """# This is the config file for your ArchiveBox collection.
675 #
676 # You can add options here manually in INI format, or automatically by running:
677 # archivebox config --set KEY=VALUE
678 #
679 # If you modify this file manually, make sure to update your archive after by running:
680 # archivebox init
681 #
682 # A list of all possible config with documentation and examples can be found here:
683 # https://github.com/ArchiveBox/ArchiveBox/wiki/Configuration
684
685 """)
686
687 out_dir = out_dir or Path(os.getenv('OUTPUT_DIR', '.')).resolve()
688 config_path = Path(out_dir) / CONFIG_FILENAME
689
690 if not config_path.exists():
691 atomic_write(config_path, CONFIG_HEADER)
692
693 config_file = ConfigParser()
694 config_file.optionxform = str
695 config_file.read(config_path)
696
697 with open(config_path, 'r', encoding='utf-8') as old:
698 atomic_write(f'{config_path}.bak', old.read())
699
700 find_section = lambda key: [name for name, opts in CONFIG_SCHEMA.items() if key in opts][0]
701
702 # Set up sections in empty config file
703 for key, val in config.items():
704 section = find_section(key)
705 if section in config_file:
706 existing_config = dict(config_file[section])
707 else:
708 existing_config = {}
709 config_file[section] = {**existing_config, key: val}
710
711 # always make sure there's a SECRET_KEY defined for Django
712 existing_secret_key = None
713 if 'SERVER_CONFIG' in config_file and 'SECRET_KEY' in config_file['SERVER_CONFIG']:
714 existing_secret_key = config_file['SERVER_CONFIG']['SECRET_KEY']
715
716 if (not existing_secret_key) or ('not a valid secret' in existing_secret_key):
717 from django.utils.crypto import get_random_string
718 chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
719 random_secret_key = get_random_string(50, chars)
720 if 'SERVER_CONFIG' in config_file:
721 config_file['SERVER_CONFIG']['SECRET_KEY'] = random_secret_key
722 else:
723 config_file['SERVER_CONFIG'] = {'SECRET_KEY': random_secret_key}
724
725 with open(config_path, 'w+', encoding='utf-8') as new:

Callers 2

initFunction · 0.85
configFunction · 0.85

Calls 3

atomic_writeFunction · 0.85
load_all_configFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected