"""
Vesper archive settings.

The Vesper server serves the Vesper archive that is in the directory
in which the server starts. The archive settings are the composition
of a set of default settings (hard-coded in this module) and settings
(optionally) specified in the file "Archive Settings.yaml" in the
archive directory.
"""


from pathlib import Path
import os
import sys

from vesper.util.settings import Settings
from vesper.util.settings_type import SettingsType
import vesper.archive_paths as archive_paths


_DEFAULT_SETTINGS = Settings.create_from_yaml('''
database:
    engine: SQLite
''')


_SETTINGS_TYPE = SettingsType('Archive Settings', _DEFAULT_SETTINGS)


_SETTINGS_FILE_NAME = 'Archive Settings.yaml'


def _create_settings():
    archive_dir_path = Path(os.getcwd())
    settings = _load_settings_file(archive_dir_path)
    archive_paths.initialize(archive_dir_path, settings)
    return settings

    
def _load_settings_file(archive_dir_path):
    
    file_path = archive_dir_path / _SETTINGS_FILE_NAME
    
    if not file_path.exists():
        # settings file doex not exist
        
        return _SETTINGS_TYPE.defaults
    
    else:
        # settings file exists
        
        try:
            return _SETTINGS_TYPE.create_settings_from_yaml_file(file_path)
        
        except Exception as e:
            print((
                'Load failed for settings file "{}". Error message '
                'was: {}').format(file_path, str(e)))
            sys.exit(1)
    
    
archive_settings = _create_settings()

[((21, 20, 24, 4), 'vesper.util.settings.Settings.create_from_yaml', 'Settings.create_from_yaml', ({(21, 46, 24, 3): '"""\ndatabase:\n    engine: SQLite\n"""'}, {}), '("""\ndatabase:\n    engine: SQLite\n""")', False, 'from vesper.util.settings import Settings\n'), ((27, 17, 27, 68), 'vesper.util.settings_type.SettingsType', 'SettingsType', ({(27, 30, 27, 48): '"""Archive Settings"""', (27, 50, 27, 67): '_DEFAULT_SETTINGS'}, {}), "('Archive Settings', _DEFAULT_SETTINGS)", False, 'from vesper.util.settings_type import SettingsType\n'), ((36, 4, 36, 56), 'vesper.archive_paths.initialize', 'archive_paths.initialize', ({(36, 29, 36, 45): 'archive_dir_path', (36, 47, 36, 55): 'settings'}, {}), '(archive_dir_path, settings)', True, 'import vesper.archive_paths as archive_paths\n'), ((34, 28, 34, 39), 'os.getcwd', 'os.getcwd', ({}, {}), '()', False, 'import os\n'), ((59, 12, 59, 23), 'sys.exit', 'sys.exit', ({(59, 21, 59, 22): '(1)'}, {}), '(1)', False, 'import sys\n')]