config: fix file issues with identities

This commit is contained in:
dece 2021-05-29 22:07:55 +02:00
parent a165ca6bff
commit 9140cedad0
2 changed files with 16 additions and 7 deletions

View file

@ -301,7 +301,7 @@ def _handle_cert_required(
The result of `open_gemini_url` with the client certificate provided. The result of `open_gemini_url` with the client certificate provided.
""" """
identities = load_identities(get_identities_list_path()) identities = load_identities(get_identities_list_path())
if not identities: if identities is None:
browser.set_status_error("Can't load identities.") browser.set_status_error("Can't load identities.")
return None return None
browser.identities = identities browser.identities = identities

View file

@ -2,7 +2,7 @@
import json import json
import logging import logging
import os.path from pathlib import Path
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
@ -33,8 +33,8 @@ DEFAULT_CONFIG = {
RENDER_MODES = ("fancy", "dumb") RENDER_MODES = ("fancy", "dumb")
def load_config(config_path): def load_config(config_path: Path):
if not os.path.isfile(config_path): if not config_path.is_file():
create_default_config(config_path) create_default_config(config_path)
return DEFAULT_CONFIG return DEFAULT_CONFIG
@ -42,9 +42,11 @@ def load_config(config_path):
with open(config_path, "rt") as config_file: with open(config_path, "rt") as config_file:
config = json.load(config_file) config = json.load(config_file)
except OSError as exc: 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: 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: else:
# Fill missing values with defaults. # Fill missing values with defaults.
for key, value in DEFAULT_CONFIG.items(): for key, value in DEFAULT_CONFIG.items():
@ -54,7 +56,14 @@ def load_config(config_path):
return DEFAULT_CONFIG 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: try:
with open(config_path, "wt") as config_file: with open(config_path, "wt") as config_file:
json.dump(DEFAULT_CONFIG, config_file, indent=2) json.dump(DEFAULT_CONFIG, config_file, indent=2)