From 51a6ac2d51862c3a18cf4ade84094f23e31e0161 Mon Sep 17 00:00:00 2001 From: Manuel Ebert Date: Mon, 4 Aug 2014 17:19:15 +0200 Subject: [PATCH] Upgrade scaffolding --- jrnl/cli.py | 2 +- jrnl/install.py | 20 ++++++++++++-------- jrnl/upgrade.py | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 jrnl/upgrade.py diff --git a/jrnl/cli.py b/jrnl/cli.py index fa45c8ea..76b9d43c 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -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))) diff --git a/jrnl/install.py b/jrnl/install.py index 5e643935..4d20a6bc 100644 --- a/jrnl/install.py +++ b/jrnl/install.py @@ -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] diff --git a/jrnl/upgrade.py b/jrnl/upgrade.py new file mode 100644 index 00000000..3039bd87 --- /dev/null +++ b/jrnl/upgrade.py @@ -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("")