mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-07-03 07:16:12 +02:00
parent
9e95edd53c
commit
63ad45db75
4 changed files with 27 additions and 38 deletions
|
@ -5,13 +5,12 @@ from __future__ import absolute_import
|
|||
import readline
|
||||
import glob
|
||||
import getpass
|
||||
import json
|
||||
import os
|
||||
import xdg.BaseDirectory
|
||||
from . import util
|
||||
import yaml
|
||||
|
||||
|
||||
DEFAULT_CONFIG_NAME = 'jrnl.json'
|
||||
DEFAULT_CONFIG_NAME = 'jrnl.yaml'
|
||||
DEFAULT_JOURNAL_NAME = 'journal.txt'
|
||||
XDG_RESOURCE = 'jrnl'
|
||||
|
||||
|
@ -19,6 +18,7 @@ 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)
|
||||
|
@ -56,25 +56,32 @@ def upgrade_config(config):
|
|||
if missing_keys:
|
||||
for key in missing_keys:
|
||||
config[key] = default_config[key]
|
||||
with open(CONFIG_FILE_PATH, 'w') as f:
|
||||
json.dump(config, f, indent=2)
|
||||
save_config(config)
|
||||
print("[.jrnl_conf updated to newest version]")
|
||||
|
||||
|
||||
def save_config(config):
|
||||
with open(CONFIG_FILE_PATH, 'w') as f:
|
||||
json.dump(config, f, indent=2)
|
||||
yaml.safe_dump(config, file(CONFIG_FILE_PATH, 'w'), encoding='utf-8', allow_unicode=True, default_flow_style=False)
|
||||
|
||||
|
||||
def install_jrnl():
|
||||
"""
|
||||
If jrnl is already installed, loads and returns a config object.
|
||||
Else, perform various prompts to install jrnl.
|
||||
"""
|
||||
if os.path.exists(CONFIG_FILE_PATH):
|
||||
config = util.load_and_fix_json(CONFIG_FILE_PATH)
|
||||
config = util.load_config(CONFIG_FILE_PATH)
|
||||
upgrade_config(config)
|
||||
return config
|
||||
elif os.path.exists(CONFIG_FILE_PATH_FALLBACK): # Backwards compatibility with jrnl 1.x
|
||||
config = util.load_config(CONFIG_FILE_PATH_FALLBACK)
|
||||
upgrade_config(config)
|
||||
save_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]
|
||||
expansions = glob.glob(os.path.expanduser(os.path.expandvars(text)) + '*')
|
||||
expansions = [e + "/" if os.path.isdir(e) else e for e in expansions]
|
||||
expansions.append(None)
|
||||
return expansions[state]
|
||||
readline.set_completer_delims(' \t\n;')
|
||||
|
@ -108,9 +115,7 @@ def install_jrnl():
|
|||
|
||||
open(default_config['journals']['default'], 'a').close() # Touch to make sure it's there
|
||||
|
||||
# Write config to ~/.jrnl_conf
|
||||
with open(CONFIG_FILE_PATH, 'w') as f:
|
||||
json.dump(default_config, f, indent=2)
|
||||
save_config(config)
|
||||
config = default_config
|
||||
if password:
|
||||
config['password'] = password
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue