From 9140cedad05c27a496ace56d72c8bfc0b79f098b Mon Sep 17 00:00:00 2001 From: dece Date: Sat, 29 May 2021 22:07:55 +0200 Subject: [PATCH] config: fix file issues with identities --- bebop/browser/gemini.py | 2 +- bebop/config.py | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/bebop/browser/gemini.py b/bebop/browser/gemini.py index cca6131..f7ff1d2 100644 --- a/bebop/browser/gemini.py +++ b/bebop/browser/gemini.py @@ -301,7 +301,7 @@ def _handle_cert_required( The result of `open_gemini_url` with the client certificate provided. """ identities = load_identities(get_identities_list_path()) - if not identities: + if identities is None: browser.set_status_error("Can't load identities.") return None browser.identities = identities diff --git a/bebop/config.py b/bebop/config.py index 62701d0..742981a 100644 --- a/bebop/config.py +++ b/bebop/config.py @@ -2,7 +2,7 @@ import json import logging -import os.path +from pathlib import Path DEFAULT_CONFIG = { @@ -33,8 +33,8 @@ DEFAULT_CONFIG = { RENDER_MODES = ("fancy", "dumb") -def load_config(config_path): - if not os.path.isfile(config_path): +def load_config(config_path: Path): + if not config_path.is_file(): create_default_config(config_path) return DEFAULT_CONFIG @@ -42,9 +42,11 @@ def load_config(config_path): with open(config_path, "rt") as config_file: config = json.load(config_file) except OSError as exc: - logging.error(f"Could not read config file {config_path}: {exc}") + abs_path = config_path.absolute() + logging.error(f"Could not read config file {abs_path}: {exc}") except ValueError as exc: - logging.error(f"Could not parse config file {config_path}: {exc}") + abs_path = config_path.absolute() + logging.error(f"Could not parse config file {abs_path}: {exc}") else: # Fill missing values with defaults. for key, value in DEFAULT_CONFIG.items(): @@ -54,7 +56,14 @@ def load_config(config_path): return DEFAULT_CONFIG -def create_default_config(config_path): +def create_default_config(config_path: Path): + config_dir = config_path.parent + if not config_dir.is_dir(): + try: + config_dir.mkdir(parents=True) + except OSError as exc: + logging.error(f"Could not create config dir {config_dir}: {exc}") + return try: with open(config_path, "wt") as config_file: json.dump(DEFAULT_CONFIG, config_file, indent=2)