Moving configuration values and methods from install.py to config.py -- everything is broken right now

This commit is contained in:
Micah Jerome Ellison 2020-12-05 14:25:51 -08:00
parent b6d77e4048
commit 340b8fb43d
2 changed files with 78 additions and 58 deletions

View file

@ -1,13 +1,74 @@
import logging
import os
import sys
import colorama
import yaml
import xdg.BaseDirectory
from . import __version__
from .color import ERROR_COLOR
from .color import RESET_COLOR
from .output import list_journals
# Constants
DEFAULT_CONFIG_NAME = "jrnl.yaml"
XDG_RESOURCE = "jrnl"
DEFAULT_JOURNAL_NAME = "journal.txt"
DEFAULT_JOURNAL_KEY = "default"
def save_config(config):
config["version"] = __version__
with open(get_config_path(), "w") as f:
yaml.safe_dump(
config, f, encoding="utf-8", allow_unicode=True, default_flow_style=False
)
def get_config_path():
try:
config_directory_path = xdg.BaseDirectory.save_config_path(XDG_RESOURCE)
except FileExistsError:
# .TODO raise a custom jrnl exception
# this is when XDG tries to create a directory with the same name as a file that exists
raise
return os.path.join(
config_directory_path or os.path.expanduser("~"), DEFAULT_CONFIG_NAME
)
def get_default_config():
return {
"version": __version__,
"journals": {"default": get_default_journal_path()},
"editor": os.getenv("VISUAL") or os.getenv("EDITOR") or "",
"encrypt": False,
"template": False,
"default_hour": 9,
"default_minute": 0,
"timeformat": "%Y-%m-%d %H:%M",
"tagsymbols": "@",
"highlight": True,
"linewrap": 79,
"indent_character": "|",
"colors": {
"date": "none",
"title": "none",
"body": "none",
"tags": "none",
},
}
def get_default_journal_path():
journal_path = xdg.BaseDirectory.save_data_path(XDG_RESOURCE) or os.path.expanduser(
"~"
)
return os.path.join(journal_path, DEFAULT_JOURNAL_NAME)
def scope_config(config, journal_name):
if journal_name not in config["journals"]:
@ -73,13 +134,11 @@ def update_config(config, new_config, scope, force_local=False):
def get_journal_name(args, config):
from . import install
args.journal_name = install.DEFAULT_JOURNAL_KEY
args.journal_name = DEFAULT_JOURNAL_KEY
if args.text and args.text[0] in config["journals"]:
args.journal_name = args.text[0]
args.text = args.text[1:]
elif install.DEFAULT_JOURNAL_KEY not in config["journals"]:
elif DEFAULT_JOURNAL_KEY not in config["journals"]:
print("No default journal configured.", file=sys.stderr)
print(list_journals(config), file=sys.stderr)
sys.exit(1)

View file

@ -5,86 +5,45 @@ import logging
import os
import sys
import xdg.BaseDirectory
import yaml
from . import __version__
from .config import DEFAULT_JOURNAL_KEY
from .config import get_config_path
from .config import get_default_config
from .config import get_default_journal_path
from .config import load_config
from .config import save_config
from .config import verify_config_colors
from .exception import UserAbort
from .prompt import yesno
from .upgrade import is_old_version
DEFAULT_CONFIG_NAME = "jrnl.yaml"
DEFAULT_JOURNAL_NAME = "journal.txt"
DEFAULT_JOURNAL_KEY = "default"
XDG_RESOURCE = "jrnl"
USER_HOME = os.path.expanduser("~")
CONFIG_PATH = xdg.BaseDirectory.save_config_path(XDG_RESOURCE) or USER_HOME
CONFIG_FILE_PATH = os.path.join(CONFIG_PATH, DEFAULT_CONFIG_NAME)
CONFIG_FILE_PATH_FALLBACK = os.path.join(USER_HOME, ".jrnl_config")
JOURNAL_PATH = xdg.BaseDirectory.save_data_path(XDG_RESOURCE) or USER_HOME
JOURNAL_FILE_PATH = os.path.join(JOURNAL_PATH, DEFAULT_JOURNAL_NAME)
default_config = {
"version": __version__,
"journals": {"default": JOURNAL_FILE_PATH},
"editor": os.getenv("VISUAL") or os.getenv("EDITOR") or "",
"encrypt": False,
"template": False,
"default_hour": 9,
"default_minute": 0,
"timeformat": "%Y-%m-%d %H:%M",
"tagsymbols": "@",
"highlight": True,
"linewrap": 79,
"indent_character": "|",
"colors": {
"date": "none",
"title": "none",
"body": "none",
"tags": "none",
},
}
def upgrade_config(config):
"""Checks if there are keys missing in a given config dict, and if so, updates the config file accordingly.
This essentially automatically ports jrnl installations if new config parameters are introduced in later
versions."""
default_config = get_default_config()
missing_keys = set(default_config).difference(config)
if missing_keys:
for key in missing_keys:
config[key] = default_config[key]
save_config(config)
print(
f"[Configuration updated to newest version at {CONFIG_FILE_PATH}]",
f"[Configuration updated to newest version at {get_config_path()}]",
file=sys.stderr,
)
def save_config(config):
config["version"] = __version__
with open(CONFIG_FILE_PATH, "w") as f:
yaml.safe_dump(
config, f, encoding="utf-8", allow_unicode=True, default_flow_style=False
)
def load_or_install_jrnl():
"""
If jrnl is already installed, loads and returns a config object.
Else, perform various prompts to install jrnl.
"""
config_path = (
CONFIG_FILE_PATH
if os.path.exists(CONFIG_FILE_PATH)
else CONFIG_FILE_PATH_FALLBACK
get_config_path()
if os.path.exists(get_config_path())
else os.path.join(os.path.expanduser("~"), ".jrnl_config")
)
if os.path.exists(config_path):
logging.debug("Reading configuration from file %s", config_path)
config = load_config(config_path)
@ -125,8 +84,10 @@ def install():
_initialize_autocomplete()
# Where to create the journal?
path_query = f"Path to your journal file (leave blank for {JOURNAL_FILE_PATH}): "
journal_path = os.path.abspath(input(path_query).strip() or JOURNAL_FILE_PATH)
default_journal_path = get_default_journal_path()
path_query = f"Path to your journal file (leave blank for {default_journal_path}): "
journal_path = os.path.abspath(input(path_query).strip() or default_journal_path)
default_config = get_default_config()
default_config["journals"][DEFAULT_JOURNAL_KEY] = os.path.expanduser(
os.path.expandvars(journal_path)
)