This commit is contained in:
yarko 2014-07-23 03:10:18 +00:00
commit 05a5daa635
5 changed files with 37 additions and 31 deletions

7
.gitignore vendored
View file

@ -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/

View file

@ -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:

View file

@ -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,21 @@ 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.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=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)
yaml.dump(config, f, indent=2, default_flow_style=False)
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 +94,10 @@ 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.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

View file

@ -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,28 +88,18 @@ 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("[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)

View file

@ -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."),
}
@ -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",