Merge pull request #306 from pcarranza/2.0-r1-logging

Added basic logging feature to understand how is configuration loaded
This commit is contained in:
Manuel Ebert 2014-11-10 11:53:14 +01:00
commit 1a65ae57cb
2 changed files with 21 additions and 0 deletions

View file

@ -16,13 +16,16 @@ import jrnl
import os
import argparse
import sys
import logging
PYCRYPTO = install.module_exists("Crypto")
log = logging.getLogger(__name__)
def parse_args(args=None):
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--version', dest='version', action="store_true", help="prints version information and exits")
parser.add_argument('-ls', dest='ls', action="store_true", help="displays accessible journals")
parser.add_argument('-d', '--debug', dest='debug', action='store_true', help='execute in debug mode')
composing = parser.add_argument_group('Composing', 'To write an entry simply write it on the command line, e.g. "jrnl yesterday at 1pm: Went to the gym."')
composing.add_argument('text', metavar='', nargs="*")
@ -102,6 +105,7 @@ def decrypt(journal, filename=None):
def touch_journal(filename):
"""If filename does not exist, touch the file"""
if not os.path.exists(filename):
log.debug('Creating journal file %s', filename)
util.prompt("[Journal created at {0}]".format(filename))
open(filename, 'a').close()
@ -126,8 +130,15 @@ def update_config(config, new_config, scope, force_local=False):
config.update(new_config)
def configure_logger(debug=False):
logging.basicConfig(level=logging.DEBUG if debug else logging.INFO,
format='%(levelname)-8s %(name)-12s %(message)s')
logging.getLogger('parsedatetime').setLevel(logging.INFO) # disable parsedatetime debug logging
def run(manual_args=None):
args = parse_args(manual_args)
configure_logger(args.debug)
args.text = [p.decode('utf-8') if util.PY2 and not isinstance(p, unicode) else p for p in args.text]
if args.version:
version_str = "{0} version {1}".format(jrnl.__title__, jrnl.__version__)
@ -143,6 +154,7 @@ def run(manual_args=None):
print " * {:{}} -> {}".format(journal, ml, cfg['journal'] if isinstance(cfg, dict) else cfg)
sys.exit(0)
log.debug('Using configuration "%s"', config)
original_config = config.copy()
# check if the configuration is supported by available modules
if config['encrypt'] and not PYCRYPTO:
@ -162,14 +174,17 @@ def run(manual_args=None):
except:
pass
log.debug('Using journal "%s"', journal_name)
journal_conf = config['journals'].get(journal_name)
if type(journal_conf) is dict: # We can override the default config on a by-journal basis
log.debug('Updating configuration with specific jourlnal overrides %s', journal_conf)
config.update(journal_conf)
else: # But also just give them a string to point to the journal file
config['journal'] = journal_conf
config['journal'] = os.path.expanduser(os.path.expandvars(config['journal']))
touch_journal(config['journal'])
mode_compose, mode_export, mode_import = guess_mode(args, config)
log.debug('Using journal path %(journal)s', config)
# How to quit writing?
if "win32" in sys.platform:
@ -205,6 +220,7 @@ def run(manual_args=None):
raw = " ".join(args.text).strip()
if util.PY2 and type(raw) is not unicode:
raw = raw.decode(sys.getfilesystemencoding())
log.debug('Appending raw line "%s" to journal "%s"', raw, journal_name)
journal.new_entry(raw)
util.prompt("[Entry added to {0} journal]".format(journal_name))
journal.write()

View file

@ -11,6 +11,7 @@ from . import util
from . import upgrade
from . import __version__
import yaml
import logging
DEFAULT_CONFIG_NAME = 'jrnl.yaml'
DEFAULT_JOURNAL_NAME = 'journal.txt'
@ -25,6 +26,8 @@ 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)
log = logging.getLogger(__name__)
def module_exists(module_name):
"""Checks if a module exists and can be imported"""
@ -76,11 +79,13 @@ def load_or_install_jrnl():
"""
config_path = CONFIG_FILE_PATH if os.path.exists(CONFIG_FILE_PATH) else CONFIG_FILE_PATH_FALLBACK
if os.path.exists(config_path):
log.debug('Reading configuration from file %s', config_path)
config = util.load_config(CONFIG_FILE_PATH)
upgrade.upgrade_jrnl_if_necessary(CONFIG_FILE_PATH)
upgrade_config(config)
return config
else:
log.debug('Configuration file not found, installing jrnl...')
install()
def install():