def test_updater_to_dict(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    parser = ConfigParser()
    parser.read(setup_cfg_path)
    parser_dict = {sect: dict(parser[sect]) for sect in parser.sections()}
    assert updater.to_dict() == parser_dict
Esempio n. 2
0
def read_config(path: Optional[Path] = None) -> Box:
    """Return the config"""
    config = Box(box_it_up=True)
    if path:
        config_path_locations: Tuple[Path, ...] = (path,)
    else:
        config_path_locations = (
            Path(Path("/etc") / "awattprice" / "config.ini"),
            Path(os.path.expanduser("~")) / ".config" / "awattprice" / "config.ini",
        )
    found_config_file = False
    for path in config_path_locations:
        if path.exists():
            found_config_file = True
            break
    else:
        log.info(f"No config file found in {path.parent}. Creating one...")
        path = Path(os.path.expanduser("~")) / ".config" / "awattprice" / "config.ini"
        config_updater = bootstrap_config(path)
    if path.parent.exists() and not path.parent.is_dir():
        log.error(f"Expected the config directory {path.parent} to be a directory.")
        sys.exit(1)

    if not verify_file_permissions(path):
        log.error(f"Could not ensure secure file permissions for {path}. Fix them and try again.")
        sys.exit(1)

    if found_config_file:
        config_updater = ConfigUpdater()
        try:
            config_updater.read(path.as_posix())
        except Exception as e:
            log.error(f"Could not read the config from {path}: {e}")
            sys.exit(1)

    config = Box(config_updater.to_dict(), box_dots=True)
    config["config_file_path"] = path

    # Strip off quotes that made it into the config.ini file
    config.file_location.data_dir = config.file_location.data_dir.strip("\"'")
    config.file_location.log_dir = config.file_location.log_dir.strip("\"'")
    config.file_location.apns_dir = config.file_location.apns_dir.strip("\"'")

    config_to_bool(config)

    config.notifications.dev_team_id = config.notifications.dev_team_id.strip("\"'")
    config.notifications.apns_encryption_key_id = config.notifications.apns_encryption_key_id.strip("\"'")
    config.notifications.apns_encryption_key = config.notifications.apns_encryption_key.strip("\"'")

    return config