mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-07-03 07:16:12 +02:00
Use PyXDG to save config and journal in XDG dirs
This change also hides the CONFIG_PATH state information entirely within the install module. Therefore, the cli module does not have to care about checking existence of files and paths.
This commit is contained in:
parent
fca3d131bd
commit
afae3d16a7
4 changed files with 39 additions and 25 deletions
|
@ -7,9 +7,23 @@ import glob
|
|||
import getpass
|
||||
import json
|
||||
import os
|
||||
import xdg.BaseDirectory
|
||||
from . import util
|
||||
|
||||
|
||||
DEFAULT_CONFIG_NAME = 'jrnl.json'
|
||||
DEFAULT_JOURNAL_NAME = 'journal.txt'
|
||||
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)
|
||||
|
||||
JOURNAL_PATH = xdg.BaseDirectory.save_data_path(XDG_RESOURCE) or USER_HOME
|
||||
JOURNAL_FILE_PATH = os.path.join(JOURNAL_PATH, DEFAULT_JOURNAL_NAME)
|
||||
|
||||
|
||||
def module_exists(module_name):
|
||||
"""Checks if a module exists and can be imported"""
|
||||
try:
|
||||
|
@ -21,7 +35,7 @@ def module_exists(module_name):
|
|||
|
||||
default_config = {
|
||||
'journals': {
|
||||
"default": os.path.expanduser("~/journal.txt")
|
||||
"default": JOURNAL_FILE_PATH
|
||||
},
|
||||
'editor': os.getenv('VISUAL') or os.getenv('EDITOR') or "",
|
||||
'encrypt': False,
|
||||
|
@ -34,7 +48,7 @@ default_config = {
|
|||
}
|
||||
|
||||
|
||||
def upgrade_config(config, config_path):
|
||||
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."""
|
||||
|
@ -42,17 +56,22 @@ def upgrade_config(config, config_path):
|
|||
if missing_keys:
|
||||
for key in missing_keys:
|
||||
config[key] = default_config[key]
|
||||
with open(config_path, 'w') as f:
|
||||
with open(CONFIG_FILE_PATH, 'w') as f:
|
||||
json.dump(config, f, indent=2)
|
||||
print("[.jrnl_conf updated to newest version]")
|
||||
|
||||
|
||||
def save_config(config, config_path):
|
||||
with open(config_path, 'w') as f:
|
||||
def save_config(config):
|
||||
with open(CONFIG_FILE_PATH, 'w') as f:
|
||||
json.dump(config, f, indent=2)
|
||||
|
||||
|
||||
def install_jrnl(config_path):
|
||||
def install_jrnl():
|
||||
if os.path.exists(CONFIG_FILE_PATH):
|
||||
config = util.load_and_fix_json(CONFIG_FILE_PATH)
|
||||
upgrade_config(config)
|
||||
return config
|
||||
|
||||
def autocomplete(text, state):
|
||||
expansions = glob.glob(os.path.expanduser(os.path.expandvars(text))+'*')
|
||||
expansions = [e+"/" if os.path.isdir(e) else e for e in expansions]
|
||||
|
@ -63,8 +82,8 @@ def install_jrnl(config_path):
|
|||
readline.set_completer(autocomplete)
|
||||
|
||||
# Where to create the journal?
|
||||
path_query = 'Path to your journal file (leave blank for ~/journal.txt): '
|
||||
journal_path = util.py23_input(path_query).strip() or os.path.expanduser('~/journal.txt')
|
||||
path_query = 'Path to your journal file (leave blank for {}): '.format(JOURNAL_FILE_PATH)
|
||||
journal_path = util.py23_input(path_query).strip() or JOURNAL_FILE_PATH
|
||||
default_config['journals']['default'] = os.path.expanduser(os.path.expandvars(journal_path))
|
||||
|
||||
# Encrypt it?
|
||||
|
@ -90,7 +109,7 @@ def install_jrnl(config_path):
|
|||
open(default_config['journals']['default'], 'a').close() # Touch to make sure it's there
|
||||
|
||||
# Write config to ~/.jrnl_conf
|
||||
with open(config_path, 'w') as f:
|
||||
with open(CONFIG_FILE_PATH, 'w') as f:
|
||||
json.dump(default_config, f, indent=2)
|
||||
config = default_config
|
||||
if password:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue