mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-20 21:18:32 +02:00
Moving configuration values and methods from install.py to config.py -- everything is broken right now
This commit is contained in:
parent
b6d77e4048
commit
340b8fb43d
2 changed files with 78 additions and 58 deletions
|
@ -1,13 +1,74 @@
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import colorama
|
import colorama
|
||||||
import yaml
|
import yaml
|
||||||
|
import xdg.BaseDirectory
|
||||||
|
|
||||||
|
from . import __version__
|
||||||
from .color import ERROR_COLOR
|
from .color import ERROR_COLOR
|
||||||
from .color import RESET_COLOR
|
from .color import RESET_COLOR
|
||||||
from .output import list_journals
|
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):
|
def scope_config(config, journal_name):
|
||||||
if journal_name not in config["journals"]:
|
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):
|
def get_journal_name(args, config):
|
||||||
from . import install
|
args.journal_name = DEFAULT_JOURNAL_KEY
|
||||||
|
|
||||||
args.journal_name = install.DEFAULT_JOURNAL_KEY
|
|
||||||
if args.text and args.text[0] in config["journals"]:
|
if args.text and args.text[0] in config["journals"]:
|
||||||
args.journal_name = args.text[0]
|
args.journal_name = args.text[0]
|
||||||
args.text = args.text[1:]
|
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("No default journal configured.", file=sys.stderr)
|
||||||
print(list_journals(config), file=sys.stderr)
|
print(list_journals(config), file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
|
@ -5,86 +5,45 @@ import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import xdg.BaseDirectory
|
from .config import DEFAULT_JOURNAL_KEY
|
||||||
import yaml
|
from .config import get_config_path
|
||||||
|
from .config import get_default_config
|
||||||
from . import __version__
|
from .config import get_default_journal_path
|
||||||
from .config import load_config
|
from .config import load_config
|
||||||
|
from .config import save_config
|
||||||
from .config import verify_config_colors
|
from .config import verify_config_colors
|
||||||
from .exception import UserAbort
|
from .exception import UserAbort
|
||||||
from .prompt import yesno
|
from .prompt import yesno
|
||||||
from .upgrade import is_old_version
|
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):
|
def upgrade_config(config):
|
||||||
"""Checks if there are keys missing in a given config dict, and if so, updates the config file accordingly.
|
"""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
|
This essentially automatically ports jrnl installations if new config parameters are introduced in later
|
||||||
versions."""
|
versions."""
|
||||||
|
default_config = get_default_config()
|
||||||
missing_keys = set(default_config).difference(config)
|
missing_keys = set(default_config).difference(config)
|
||||||
if missing_keys:
|
if missing_keys:
|
||||||
for key in missing_keys:
|
for key in missing_keys:
|
||||||
config[key] = default_config[key]
|
config[key] = default_config[key]
|
||||||
save_config(config)
|
save_config(config)
|
||||||
print(
|
print(
|
||||||
f"[Configuration updated to newest version at {CONFIG_FILE_PATH}]",
|
f"[Configuration updated to newest version at {get_config_path()}]",
|
||||||
file=sys.stderr,
|
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():
|
def load_or_install_jrnl():
|
||||||
"""
|
"""
|
||||||
If jrnl is already installed, loads and returns a config object.
|
If jrnl is already installed, loads and returns a config object.
|
||||||
Else, perform various prompts to install jrnl.
|
Else, perform various prompts to install jrnl.
|
||||||
"""
|
"""
|
||||||
config_path = (
|
config_path = (
|
||||||
CONFIG_FILE_PATH
|
get_config_path()
|
||||||
if os.path.exists(CONFIG_FILE_PATH)
|
if os.path.exists(get_config_path())
|
||||||
else CONFIG_FILE_PATH_FALLBACK
|
else os.path.join(os.path.expanduser("~"), ".jrnl_config")
|
||||||
)
|
)
|
||||||
|
|
||||||
if os.path.exists(config_path):
|
if os.path.exists(config_path):
|
||||||
logging.debug("Reading configuration from file %s", config_path)
|
logging.debug("Reading configuration from file %s", config_path)
|
||||||
config = load_config(config_path)
|
config = load_config(config_path)
|
||||||
|
@ -125,8 +84,10 @@ def install():
|
||||||
_initialize_autocomplete()
|
_initialize_autocomplete()
|
||||||
|
|
||||||
# Where to create the journal?
|
# Where to create the journal?
|
||||||
path_query = f"Path to your journal file (leave blank for {JOURNAL_FILE_PATH}): "
|
default_journal_path = get_default_journal_path()
|
||||||
journal_path = os.path.abspath(input(path_query).strip() or JOURNAL_FILE_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(
|
default_config["journals"][DEFAULT_JOURNAL_KEY] = os.path.expanduser(
|
||||||
os.path.expandvars(journal_path)
|
os.path.expandvars(journal_path)
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue