Upgrade scaffolding

This commit is contained in:
Manuel Ebert 2014-08-04 17:19:15 +02:00
parent 09b94d1047
commit 51a6ac2d51
3 changed files with 30 additions and 9 deletions

View file

@ -128,7 +128,7 @@ def run(manual_args=None):
print(util.py2encode(version_str))
sys.exit(0)
config = install.install_jrnl()
config = install.load_or_install_jrnl()
if args.ls:
print(util.py2encode(list_journals(config)))

View file

@ -8,6 +8,8 @@ import getpass
import os
import xdg.BaseDirectory
from . import util
from . import upgrade
from . import __version__
import yaml
DEFAULT_CONFIG_NAME = 'jrnl.yaml'
@ -34,6 +36,7 @@ def module_exists(module_name):
return True
default_config = {
'jrnl_version': __version__,
'journals': {
"default": JOURNAL_FILE_PATH
},
@ -53,7 +56,7 @@ def upgrade_config(config):
This essentially automatically ports jrnl installations if new config parameters are introduced in later
versions."""
missing_keys = set(default_config).difference(config)
if missing_keys:
if missing_keys or config['version'] != __version__:
for key in missing_keys:
config[key] = default_config[key]
save_config(config)
@ -61,25 +64,26 @@ def upgrade_config(config):
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 install_jrnl():
def load_or_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_path = CONFIG_FILE_PATH if os.path.exists(CONFIG_FILE_PATH) else CONFIG_FILE_PATH_FALLBACK
if os.path.exists(config_path):
config = util.load_config(CONFIG_FILE_PATH)
upgrade_config(config)
upgrade.upgrade_jrnl_if_necessary()
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
else:
install()
def install():
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]

17
jrnl/upgrade.py Normal file
View file

@ -0,0 +1,17 @@
def upgrade_jrnl_if_necessary(config_path):
with open(config_path) as f:
config = f.read()
if not config.strip().startswith("{"):
return
util.prompt("Welcome to jrnl {}".format(__version__))
util.prompt("jrnl will now upgrade your configuration and journal files.")
util.prompt("Please note that jrnl 1.x is NOT forward compatible with this version of jrnl.")
util.prompt("If you choose to proceed, you will not be able to use your journals with")
util.prompt("older versions of jrnl anymore.")
cont = util.yesno("Continue upgrading jrnl?", default=False)
if not cont:
util.prompt("jrnl NOT upgraded, exiting.")
sys.exit(1)
util.prompt("")