diff --git a/jrnl/__init__.py b/jrnl/__init__.py index 030a9d4b..8a71ba2f 100644 --- a/jrnl/__init__.py +++ b/jrnl/__init__.py @@ -8,7 +8,7 @@ jrnl is a simple journal application for your command line. from __future__ import absolute_import __title__ = 'jrnl' -__version__ = '1.9.5' +__version__ = '2.0.0-rc1' __author__ = 'Manuel Ebert' __license__ = 'MIT License' __copyright__ = 'Copyright 2013 - 2014 Manuel Ebert' diff --git a/jrnl/install.py b/jrnl/install.py index 606af840..b287a58e 100644 --- a/jrnl/install.py +++ b/jrnl/install.py @@ -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 diff --git a/jrnl/util.py b/jrnl/util.py index 6a174f74..15a5a5a9 100644 --- a/jrnl/util.py +++ b/jrnl/util.py @@ -4,7 +4,7 @@ import sys import os import getpass as gp import keyring -import json +import yaml if "win32" in sys.platform: import colorama colorama.init() @@ -100,30 +100,13 @@ def yesno(prompt, default=True): return {'y': True, 'n': False}.get(raw.lower(), default) -def load_and_fix_json(json_path): - """Tries to load a json object from a file. - If that fails, tries to fix common errors (no or extra , at end of the line). +def load_config(config_path): + """Tries to load a config file from YAML. + If that fails, fall back to JSON. """ - with open(json_path) as f: - json_str = f.read() - config = None - try: - return json.loads(json_str) - except ValueError as e: - # Attempt to fix extra , - json_str = re.sub(r",[ \n]*}", "}", json_str) - # Attempt to fix missing , - json_str = re.sub(r"([^{,]) *\n *(\")", r"\1,\n \2", json_str) - try: - config = json.loads(json_str) - with open(json_path, 'w') as f: - json.dump(config, f, indent=2) - prompt("[Some errors in your jrnl config have been fixed for you.]") - return config - except ValueError as e: - prompt("[There seems to be something wrong with your jrnl config at {0}: {1}]".format(json_path, e.message)) - prompt("[Entry was NOT added to your journal]") - sys.exit(1) + with open(config_path) as f: + config = yaml.load(f) + return config def get_text_from_editor(config, template=""): diff --git a/setup.py b/setup.py index 09b7ba2a..ae2890f3 100644 --- a/setup.py +++ b/setup.py @@ -86,6 +86,7 @@ setup( "pytz>=2013b", "six>=1.6.1", "tzlocal>=1.1", + "PyYAML>=3.11", "keyring>=3.3", ] + [p for p, cond in conditional_dependencies.items() if cond], extras_require = {