Uses stderr for prompts instead stdout

This commit is contained in:
Manuel Ebert 2013-07-22 12:04:01 +02:00
parent 4b9b5e827b
commit d3edbfd53b
5 changed files with 26 additions and 16 deletions

View file

@ -1,6 +1,10 @@
Changelog Changelog
========= =========
### 1.3.2
* [Improved] Everything that is not direct output of jrnl will be written stderr to improve integration
### 1.3.0 ### 1.3.0
* [New] Export to multiple files * [New] Export to multiple files

View file

@ -3,8 +3,8 @@
try: from . import Entry try: from . import Entry
except (SystemError, ValueError): import Entry except (SystemError, ValueError): import Entry
try: from .util import get_local_timezone try: from .util import get_local_timezone, prompt
except (SystemError, ValueError): from util import get_local_timezone except (SystemError, ValueError): from util import get_local_timezone, prompt
import codecs import codecs
import os import os
try: import parsedatetime.parsedatetime_consts as pdt try: import parsedatetime.parsedatetime_consts as pdt
@ -76,7 +76,7 @@ class Journal(object):
try: try:
plain = crypto.decrypt(cipher[16:]) plain = crypto.decrypt(cipher[16:])
except ValueError: except ValueError:
print("ERROR: Your journal file seems to be corrupted. You do have a backup, don't you?") prompt("ERROR: Your journal file seems to be corrupted. You do have a backup, don't you?")
sys.exit(-1) sys.exit(-1)
if plain[-1] != " ": # Journals are always padded if plain[-1] != " ": # Journals are always padded
return None return None
@ -118,9 +118,9 @@ class Journal(object):
attempts += 1 attempts += 1
self.config['password'] = None # This password doesn't work. self.config['password'] = None # This password doesn't work.
if attempts < 3: if attempts < 3:
print("Wrong password, try again.") prompt("Wrong password, try again.")
else: else:
print("Extremely wrong password.") prompt("Extremely wrong password.")
sys.exit(-1) sys.exit(-1)
journal = decrypted journal = decrypted
else: else:

View file

@ -7,7 +7,7 @@ jrnl is a simple journal application for your command line.
""" """
__title__ = 'jrnl' __title__ = 'jrnl'
__version__ = '1.3.1' __version__ = '1.3.2'
__author__ = 'Manuel Ebert' __author__ = 'Manuel Ebert'
__license__ = 'MIT License' __license__ = 'MIT License'
__copyright__ = 'Copyright 2013 Manuel Ebert' __copyright__ = 'Copyright 2013 Manuel Ebert'

View file

@ -29,6 +29,7 @@ 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") if xdg_config else os.path.expanduser('~/.jrnl_config')
PYCRYPTO = install.module_exists("Crypto") PYCRYPTO = install.module_exists("Crypto")
def parse_args(args=None): def parse_args(args=None):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
composing = parser.add_argument_group('Composing', 'Will make an entry out of whatever follows as arguments') composing = parser.add_argument_group('Composing', 'Will make an entry out of whatever follows as arguments')
@ -77,7 +78,7 @@ def get_text_from_editor(config):
raw = f.read() raw = f.read()
os.remove(tmpfile) os.remove(tmpfile)
else: else:
print('[Nothing saved to file]') util.prompt('[Nothing saved to file]')
raw = '' raw = ''
return raw return raw
@ -89,19 +90,19 @@ def encrypt(journal, filename=None):
journal.make_key(prompt="Enter new password:") journal.make_key(prompt="Enter new password:")
journal.config['encrypt'] = True journal.config['encrypt'] = True
journal.write(filename) journal.write(filename)
print("Journal encrypted to {0}.".format(filename or journal.config['journal'])) util.prompt("Journal encrypted to {0}.".format(filename or journal.config['journal']))
def decrypt(journal, filename=None): def decrypt(journal, filename=None):
""" Decrypts into new file. If filename is not set, we encrypt the journal file itself. """ """ Decrypts into new file. If filename is not set, we encrypt the journal file itself. """
journal.config['encrypt'] = False journal.config['encrypt'] = False
journal.config['password'] = "" journal.config['password'] = ""
journal.write(filename) journal.write(filename)
print("Journal decrypted to {0}.".format(filename or journal.config['journal'])) util.prompt("Journal decrypted to {0}.".format(filename or journal.config['journal']))
def touch_journal(filename): def touch_journal(filename):
"""If filename does not exist, touch the file""" """If filename does not exist, touch the file"""
if not os.path.exists(filename): if not os.path.exists(filename):
print("[Journal created at {0}]".format(filename)) util.prompt("[Journal created at {0}]".format(filename))
open(filename, 'a').close() open(filename, 'a').close()
def update_config(config, new_config, scope): def update_config(config, new_config, scope):
@ -122,15 +123,15 @@ def cli(manual_args=None):
try: try:
config = json.load(f) config = json.load(f)
except ValueError as e: except ValueError as e:
print("[There seems to be something wrong with your jrnl config at {}: {}]".format(CONFIG_PATH, e.message)) util.prompt("[There seems to be something wrong with your jrnl config at {}: {}]".format(CONFIG_PATH, e.message))
print("[Entry was NOT added to your journal]") util.prompt("[Entry was NOT added to your journal]")
sys.exit(-1) sys.exit(-1)
install.update_config(config, config_path=CONFIG_PATH) install.update_config(config, config_path=CONFIG_PATH)
original_config = config.copy() original_config = config.copy()
# check if the configuration is supported by available modules # check if the configuration is supported by available modules
if config['encrypt'] and not PYCRYPTO: if config['encrypt'] and not PYCRYPTO:
print("According to your jrnl_conf, your journal is encrypted, however PyCrypto was not found. To open your journal, install the PyCrypto package from http://www.pycrypto.org.") util.prompt("According to your jrnl_conf, your journal is encrypted, however PyCrypto was not found. To open your journal, install the PyCrypto package from http://www.pycrypto.org.")
sys.exit(-1) sys.exit(-1)
args = parse_args(manual_args) args = parse_args(manual_args)
@ -173,7 +174,7 @@ def cli(manual_args=None):
raw = raw.decode(sys.getfilesystemencoding()) raw = raw.decode(sys.getfilesystemencoding())
entry = journal.new_entry(raw, args.date) entry = journal.new_entry(raw, args.date)
entry.starred = args.star entry.starred = args.star
print("[Entry added to {0} journal]".format(journal_name)) util.prompt("[Entry added to {0} journal]".format(journal_name))
journal.write() journal.write()
# Reading mode # Reading mode
@ -193,7 +194,7 @@ def cli(manual_args=None):
print(exporters.export(journal, args.export, args.output)) print(exporters.export(journal, args.export, args.output))
elif (args.encrypt is not False or args.decrypt is not False) and not PYCRYPTO: elif (args.encrypt is not False or args.decrypt is not False) and not PYCRYPTO:
print("PyCrypto not found. To encrypt or decrypt your journal, install the PyCrypto package from http://www.pycrypto.org.") util.prompt("PyCrypto not found. To encrypt or decrypt your journal, install the PyCrypto package from http://www.pycrypto.org.")
elif args.encrypt is not False: elif args.encrypt is not False:
encrypt(journal, filename=args.encrypt) encrypt(journal, filename=args.encrypt)
@ -211,7 +212,7 @@ def cli(manual_args=None):
elif args.delete_last: elif args.delete_last:
last_entry = journal.entries.pop() last_entry = journal.entries.pop()
print("[Deleted Entry:]") util.prompt("[Deleted Entry:]")
print(last_entry) print(last_entry)
journal.write() journal.write()

View file

@ -11,9 +11,14 @@ def u(s):
return s if PY3 else unicode(s, "unicode_escape") return s if PY3 else unicode(s, "unicode_escape")
STDIN = sys.stdin STDIN = sys.stdin
STDERR = sys.stderr
STDOUT = sys.stdout STDOUT = sys.stdout
__cached_tz = None __cached_tz = None
def prompt(msg):
"""Prints a message to the std err stream defined in util."""
print(msg, file=STDERR)
def py23_input(msg): def py23_input(msg):
STDOUT.write(msg) STDOUT.write(msg)
return STDIN.readline().strip() return STDIN.readline().strip()