Support for templates

This commit is contained in:
Manuel Ebert 2015-07-01 22:23:00 -07:00
parent 43bb1f1cb2
commit 7c6b2e4bce
5 changed files with 31 additions and 9 deletions

View file

@ -313,12 +313,6 @@ def open_journal(name, config, legacy=False):
backwards compatibility with jrnl 1.x
"""
config = config.copy()
journal_conf = config['journals'].get(name)
if type(journal_conf) is dict: # We can override the default config on a by-journal basis
log.debug('Updating configuration with specific journal 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']))
if os.path.isdir(config['journal']):

View file

@ -153,6 +153,7 @@ def run(manual_args=None):
# If the first textual argument points to a journal file,
# use this!
journal_name = args.text[0] if (args.text and args.text[0] in config['journals']) else 'default'
if journal_name is not 'default':
args.text = args.text[1:]
elif "default" not in config['journals']:
@ -160,6 +161,8 @@ def run(manual_args=None):
util.prompt(list_journals(config))
sys.exit(1)
config = util.scope_config(config, journal_name)
# If the first remaining argument looks like e.g. '-3', interpret that as a limiter
if not args.limit and args.text and args.text[0].startswith("-"):
try:
@ -182,7 +185,14 @@ def run(manual_args=None):
# Piping data into jrnl
raw = util.py23_read()
elif config['editor']:
raw = util.get_text_from_editor(config)
template = ""
if config['template']:
try:
template = open(config['template']).read()
except:
util.prompt("[Could not read template at '']".format(config['template']))
sys.exit(1)
raw = util.get_text_from_editor(config, template)
else:
try:
raw = util.py23_read("[Compose Entry; " + _exit_multiline_code + " to finish writing]\n")

View file

@ -47,6 +47,7 @@ default_config = {
},
'editor': os.getenv('VISUAL') or os.getenv('EDITOR') or "",
'encrypt': False,
'template': False,
'default_hour': 9,
'default_minute': 0,
'timeformat': "%Y-%m-%d %H:%M",

View file

@ -82,7 +82,7 @@ older versions of jrnl anymore.
for journal_name, path in encrypted_journals.items():
util.prompt("\nUpgrading encrypted '{}' journal stored in {}...".format(journal_name, path))
backup(path, binary=True)
old_journal = Journal.open_journal(journal_name, config, legacy=True)
old_journal = Journal.open_journal(journal_name, util.scope_config(config, journal_name), legacy=True)
new_journal = EncryptedJournal.from_journal(old_journal)
new_journal.write()
util.prompt(" Done.")
@ -90,7 +90,7 @@ older versions of jrnl anymore.
for journal_name, path in plain_journals.items():
util.prompt("\nUpgrading plain text '{}' journal stored in {}...".format(journal_name, path))
backup(path)
old_journal = Journal.open_journal(journal_name, config, legacy=True)
old_journal = Journal.open_journal(journal_name, util.scope_config(config, journal_name), legacy=True)
new_journal = Journal.PlainJournal.from_journal(old_journal)
new_journal.write()
util.prompt(" Done.")

View file

@ -13,6 +13,9 @@ import subprocess
import codecs
import unicodedata
import shlex
import logging
log = logging.getLogger(__name__)
PY3 = sys.version_info[0] == 3
@ -122,6 +125,20 @@ def load_config(config_path):
return yaml.load(f)
def scope_config(config, journal_name):
if journal_name not in config['journals']:
return config
config = config.copy()
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 journal 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.pop('journals')
return config
def get_text_from_editor(config, template=""):
filehandle, tmpfile = tempfile.mkstemp(prefix="jrnl", text=True, suffix=".txt")
with codecs.open(tmpfile, 'w', "utf-8") as f: