From 446e78f225d454143de2d6f36eb69b92aea113be Mon Sep 17 00:00:00 2001 From: yarko Date: Mon, 21 Jul 2014 15:26:23 -0500 Subject: [PATCH 1/4] use latest system dateutils; add wing & vi ignores --- .gitignore | 7 +++++++ setup.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3b171aba..0153f8ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ *.py[cod] +# vi: +*.swp + # C extensions *.so @@ -45,6 +48,10 @@ env*/ docs/_themes/jrnl/static/less/3L.less +# wingide project files +*.wpr +*.wpu + # PyCharm Project files .idea/ diff --git a/setup.py b/setup.py index f0015be2..998c192a 100644 --- a/setup.py +++ b/setup.py @@ -70,7 +70,7 @@ conditional_dependencies = { "readline>=6.2": not readline_available and "win32" not in sys.platform, "colorama>=0.2.5": "win32" in sys.platform, "argparse>=1.1.0": sys.version.startswith("2.6"), - "python-dateutil==1.5": sys.version.startswith("2."), + "python-dateutil>=1.5": sys.version.startswith("2."), "python-dateutil>=2.2": sys.version.startswith("3."), } From 4dde1834849db180835d873cc177e0c83e418a20 Mon Sep 17 00:00:00 2001 From: yarko Date: Mon, 21 Jul 2014 16:52:06 -0500 Subject: [PATCH 2/4] replace error prone json config with yaml config --- jrnl/cli.py | 5 +++-- jrnl/install.py | 17 +++++++++-------- jrnl/util.py | 31 +++++++++++-------------------- 3 files changed, 23 insertions(+), 30 deletions(-) diff --git a/jrnl/cli.py b/jrnl/cli.py index af60bbe4..e95502e3 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -19,7 +19,8 @@ import argparse import sys xdg_config = os.environ.get('XDG_CONFIG_HOME') -CONFIG_PATH = os.path.join(xdg_config, "jrnl") if xdg_config else os.path.expanduser('~/.jrnl_config') +CONFIG_PATH = os.path.join(xdg_config, "jrnl.yml") if xdg_config else \ + os.path.expanduser('~/.jrnl_config.yml') PYCRYPTO = install.module_exists("Crypto") @@ -125,7 +126,7 @@ def run(manual_args=None): if not os.path.exists(CONFIG_PATH): config = install.install_jrnl(CONFIG_PATH) else: - config = util.load_and_fix_json(CONFIG_PATH) + config = util.load_config(CONFIG_PATH) install.upgrade_config(config, config_path=CONFIG_PATH) if args.ls: diff --git a/jrnl/install.py b/jrnl/install.py index dcb83601..5bc0b51d 100644 --- a/jrnl/install.py +++ b/jrnl/install.py @@ -5,7 +5,7 @@ from __future__ import absolute_import import readline import glob import getpass -import json +import yaml import os from . import util @@ -34,7 +34,7 @@ default_config = { } -def upgrade_config(config, config_path=os.path.expanduser("~/.jrnl_conf")): +def upgrade_config(config, config_path): """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.""" @@ -43,16 +43,17 @@ def upgrade_config(config, config_path=os.path.expanduser("~/.jrnl_conf")): for key in missing_keys: config[key] = default_config[key] with open(config_path, 'w') as f: - json.dump(config, f, indent=2) + yaml.safe_dump(config, f, indent=2, allow_unicode=True) print("[.jrnl_conf updated to newest version]") -def save_config(config=default_config, config_path=os.path.expanduser("~/.jrnl_conf")): +# def save_config(config=default_config, config_path): +def save_config(config, config_path): with open(config_path, 'w') as f: - json.dump(config, f, indent=2) + yaml.safe_dump(config, f, indent=2, allow_unicode=True) -def install_jrnl(config_path='~/.jrnl_config'): +def install_jrnl(config_path): 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] @@ -89,9 +90,9 @@ def install_jrnl(config_path='~/.jrnl_config'): open(default_config['journals']['default'], 'a').close() # Touch to make sure it's there - # Write config to ~/.jrnl_conf + # Write config to ~/.jrnl_conf.yml with open(config_path, 'w') as f: - json.dump(default_config, f, indent=2) + yaml.dump(default_config, f, indent=2, allow_unicode=True) config = default_config if password: config['password'] = password diff --git a/jrnl/util.py b/jrnl/util.py index b06113c2..aa76cf6f 100644 --- a/jrnl/util.py +++ b/jrnl/util.py @@ -4,7 +4,8 @@ import sys import os import getpass as gp import keyring -import json +import yaml +import StringIO if "win32" in sys.platform: import colorama colorama.init() @@ -87,30 +88,20 @@ def yesno(prompt, default=True): raw = py23_input(prompt) 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. +def load_config(yaml_path): + """Tries to load a yaml object from a file. If that fails, tries to fix common errors (no or extra , at end of the line). """ - with open(json_path) as f: - json_str = f.read() + with open(yaml_path) as f: + yaml_str = f.read() config = fixed = None try: - return json.loads(json_str) + f = StringIO.StringIO(yaml_str) + return yaml.load(f) 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) + prompt("[There seems to be something wrong with your jrnl config at {0}: {1}]".format(yaml_path, e.message)) + prompt("[Entry was NOT added to your journal]") + sys.exit(1) def get_text_from_editor(config, template=""): tmpfile = os.path.join(tempfile.mktemp(prefix="jrnl")) From bb40c1166b7122634335fdac18f73e911e13d391 Mon Sep 17 00:00:00 2001 From: yarko Date: Tue, 22 Jul 2014 11:57:09 -0500 Subject: [PATCH 3/4] tentatively using dump() in place of safe_dump(), and no default_flow_style; safe_dump() probably only needed for converting from old json config - a separate utility would do for that --- jrnl/install.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/jrnl/install.py b/jrnl/install.py index 5bc0b51d..269623ef 100644 --- a/jrnl/install.py +++ b/jrnl/install.py @@ -43,14 +43,18 @@ def upgrade_config(config, config_path): for key in missing_keys: config[key] = default_config[key] with open(config_path, 'w') as f: - yaml.safe_dump(config, f, indent=2, allow_unicode=True) + yaml.dump(config, f, indent=2, default_flow_style=False) + # when converting from original json version, + # needed to quiet the yaml explicit unicode marking + # yaml.safe_dump(config, f, indent=2, allow_unicode=True) print("[.jrnl_conf updated to newest version]") # def save_config(config=default_config, config_path): def save_config(config, config_path): with open(config_path, 'w') as f: - yaml.safe_dump(config, f, indent=2, allow_unicode=True) + # yaml.safe_dump(config, f, indent=2, allow_unicode=True) + yaml.dump(config, f, indent=2, default_flow_style=False) def install_jrnl(config_path): @@ -92,7 +96,8 @@ def install_jrnl(config_path): # Write config to ~/.jrnl_conf.yml with open(config_path, 'w') as f: - yaml.dump(default_config, f, indent=2, allow_unicode=True) + # yaml.safe_dump(default_config, f, indent=2, allow_unicode=True) + yaml.dump(config, f, indent=2, default_flow_style=False) config = default_config if password: config['password'] = password From 7a9f07fe921c7b8a41c97db34a59c64741159d0e Mon Sep 17 00:00:00 2001 From: yarko Date: Tue, 22 Jul 2014 22:10:06 -0500 Subject: [PATCH 4/4] add yaml dependency; --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 998c192a..dd6d8f2f 100644 --- a/setup.py +++ b/setup.py @@ -83,6 +83,7 @@ setup( install_requires = [ "parsedatetime>=1.2", "pytz>=2013b", + "PyYAML>=3.11", "six>=1.6.1", "tzlocal>=1.1", "keyring>=3.3",