Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
e130f26
fix missed statement from last PR
wren Mar 19, 2022
f1bd015
replace print statement for adding an entry to a journal
wren Mar 19, 2022
4b9fe0c
WIP
wren Apr 16, 2022
253b807
clean up linting and format
wren Apr 16, 2022
f142c91
change print statement over to new print_msg function
wren Apr 16, 2022
0fc8bcf
make print_msg always print to stderr
wren Apr 16, 2022
4237376
change print statement over to new print_msg function
wren Apr 16, 2022
c16aa42
update importer to use new message function
wren May 1, 2022
7ded211
update yaml format to use new message function
wren May 1, 2022
af220be
code cleanup
wren May 1, 2022
7eb385d
update yaml format to use new message function
wren May 1, 2022
6945d6a
update yaml format to use new exception handling
wren May 1, 2022
7690594
update Journal class to use new message function
wren May 1, 2022
2df7419
update install module to use new message function
wren May 1, 2022
8088083
update config module to use new message function
wren May 1, 2022
9c030ac
update upgrade module to properly use new message and exception handling
wren May 1, 2022
cf5b177
fix typo
wren May 1, 2022
cd464ee
update upgrade module to use new message handling
wren May 1, 2022
f1119e1
update welcome message to use new handling
wren May 1, 2022
93650f9
update upgrade module to use new message handling
wren May 1, 2022
1b6c3ed
update upgrade module journal summaries to use new message handling
wren May 1, 2022
03c0ae3
take out old code
wren May 1, 2022
3ccf695
update upgrade module to use new message handling
wren May 1, 2022
48d36ec
update upgrade module to use new message handling
wren May 1, 2022
9eb3e16
update more modules to use new message handling
wren May 1, 2022
8da4a99
take out old comment
wren May 1, 2022
2871213
update deprecated_cmd to use new message handling
wren May 1, 2022
eeca1e2
update text_exporter with new message handling, get rid of old color …
wren May 1, 2022
fd93092
get rid of hardcoded text
wren May 1, 2022
6f5a082
whitespace changes
wren May 1, 2022
16755f8
rework MsgType into MsgStyle so messages can have different styles
wren May 1, 2022
782b14c
add comment
wren May 1, 2022
e5fdf72
Move around code to separate concerns of each function a bit more
wren May 7, 2022
082e665
update create_password and yesno prompt functions for new messaging
wren May 16, 2022
fcb8b22
fix missing newline for keyboard interrupts
wren May 16, 2022
59e3477
fix misc linting
wren May 16, 2022
222a696
fix bug with panel titles always showing 'error' after one error
wren May 16, 2022
a1758d5
fix missing import
wren May 16, 2022
d58cb31
update debug output after uncaught exception
wren May 17, 2022
3381ca7
update exception for new exception handling
wren May 17, 2022
4e41251
rewrite yesno function to use new centralized messages
wren May 17, 2022
13b1046
reduce the debug output slightly
wren May 17, 2022
7015730
clean up print_msgs function
wren May 17, 2022
1a6afd9
clean up create_password function
wren May 17, 2022
fabcd53
clean up misc linting
wren May 17, 2022
32ab358
WIP
wren May 18, 2022
ebfc7bd
rename screen_input to hide_input to be more clear
wren May 28, 2022
71eb746
update encrypted journal prompt to use new messaging functionality
wren May 28, 2022
bdbd3ca
fix typo in message key
wren May 28, 2022
4636f1a
move rich console into function so we can mock properly
wren May 28, 2022
1461f03
update password mock to use rich console instead of getpass
wren May 28, 2022
a15807a
add more helpful output to then step
wren May 28, 2022
bf8e779
fix test by updating expected output
wren May 28, 2022
d2f2967
update message to use new functionality
wren May 28, 2022
34df64c
rework mocks in test suite for new messaging functionality
wren May 28, 2022
c325ba3
fix linting issue
wren Jun 5, 2022
5cb46f5
fix more tests
wren Jun 5, 2022
57f53e9
fix more tests
wren Jun 5, 2022
920bc80
fix more tests
wren Jun 5, 2022
178c913
fix more tests
wren Jun 5, 2022
15b86ca
merge in develop
wren Jun 5, 2022
517c522
fix merge bug
wren Jun 5, 2022
ac05bc6
update prompt_action_entries to use new messaging functionality
wren Jun 5, 2022
6d18e75
Add new input_method "type"
wren Jun 11, 2022
f1c5cba
get rid of old commented code
wren Jun 11, 2022
c1b4d1b
get rid of unused code
wren Jun 11, 2022
15c0eea
move some files around
wren Jun 11, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions jrnl/EncryptedJournal.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import base64
import getpass
import hashlib
import logging
import os
import sys
from typing import Callable
from typing import Optional

Expand All @@ -24,7 +22,8 @@
from jrnl.exception import JrnlException
from jrnl.messages import Message
from jrnl.messages import MsgText
from jrnl.messages import MsgType
from jrnl.messages import MsgStyle
from jrnl.output import print_msg


def make_key(password):
Expand All @@ -46,21 +45,26 @@ def decrypt_content(
keychain: str = None,
max_attempts: int = 3,
) -> str:
def get_pw():
return print_msg(
Message(MsgText.Password, MsgStyle.PROMPT), get_input=True, hide_input=True
)

pwd_from_keychain = keychain and get_keychain(keychain)
password = pwd_from_keychain or getpass.getpass()
password = pwd_from_keychain or get_pw()
result = decrypt_func(password)
# Password is bad:
if result is None and pwd_from_keychain:
set_keychain(keychain, None)
attempt = 1
while result is None and attempt < max_attempts:
print("Wrong password, try again.", file=sys.stderr)
password = getpass.getpass()
print_msg(Message(MsgText.WrongPasswordTryAgain, MsgStyle.WARNING))
password = get_pw()
result = decrypt_func(password)
attempt += 1

if result is None:
raise JrnlException(Message(MsgText.PasswordMaxTriesExceeded, MsgType.ERROR))
raise JrnlException(Message(MsgText.PasswordMaxTriesExceeded, MsgStyle.ERROR))

return result

Expand All @@ -79,13 +83,22 @@ def open(self, filename=None):
if not os.path.exists(filename):
if not os.path.isdir(dirname):
os.makedirs(dirname)
print(f"[Directory {dirname} created]", file=sys.stderr)
print_msg(
Message(
MsgText.DirectoryCreated,
MsgStyle.NORMAL,
{"directory_name": dirname},
)
)
self.create_file(filename)
self.password = create_password(self.name)

print(
f"Encrypted journal '{self.name}' created at {filename}",
file=sys.stderr,
print_msg(
Message(
MsgText.JournalCreated,
MsgStyle.NORMAL,
{"journal_name": self.name, "filename": filename},
)
)

text = self._load(filename)
Expand Down Expand Up @@ -179,7 +192,7 @@ def get_keychain(journal_name):
return keyring.get_password("jrnl", journal_name)
except keyring.errors.KeyringError as e:
if not isinstance(e, keyring.errors.NoKeyringError):
print("Failed to retrieve keyring", file=sys.stderr)
print_msg(Message(MsgText.KeyringRetrievalFailure, MsgStyle.ERROR))
return ""


Expand All @@ -196,9 +209,7 @@ def set_keychain(journal_name, password):
keyring.set_password("jrnl", journal_name, password)
except keyring.errors.KeyringError as e:
if isinstance(e, keyring.errors.NoKeyringError):
print(
"Keyring backend not found. Please install one of the supported backends by visiting: https://pypi.org/project/keyring/",
file=sys.stderr,
)
msg = Message(MsgText.KeyringBackendNotFound, MsgStyle.WARNING)
else:
print("Failed to retrieve keyring", file=sys.stderr)
msg = Message(MsgText.KeyringRetrievalFailure, MsgStyle.ERROR)
print_msg(msg)
1 change: 0 additions & 1 deletion jrnl/FolderJournal.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def write(self):
filenames = get_files(self.config["journal"])
for filename in filenames:
if os.stat(filename).st_size <= 0:
# print("empty file: {}".format(filename))
os.remove(filename)

def delete_entries(self, entries_to_delete):
Expand Down
43 changes: 35 additions & 8 deletions jrnl/Journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import logging
import os
import re
import sys

from . import Entry
from . import time
from .prompt import yesno
from .path import expand_path

from jrnl.output import print_msg
from jrnl.messages import Message
from jrnl.messages import MsgText
from jrnl.messages import MsgStyle


class Tag:
def __init__(self, name, count=0):
Expand Down Expand Up @@ -83,9 +87,24 @@ def open(self, filename=None):
if not os.path.exists(filename):
if not os.path.isdir(dirname):
os.makedirs(dirname)
print(f"[Directory {dirname} created]", file=sys.stderr)
print_msg(
Message(
MsgText.DirectoryCreated,
MsgStyle.NORMAL,
{"directory_name": dirname},
)
)
self.create_file(filename)
print(f"[Journal '{self.name}' created at {filename}]", file=sys.stderr)
print_msg(
Message(
MsgText.JournalCreated,
MsgStyle.NORMAL,
{
"journal_name": self.name,
"filename": filename,
},
)
)

text = self._load(filename)
self.entries = self._parse(text)
Expand Down Expand Up @@ -269,14 +288,17 @@ def change_date_entries(self, date):
for entry in self.entries:
entry.date = date

def prompt_action_entries(self, message):
def prompt_action_entries(self, msg: MsgText):
"""Prompts for action for each entry in a journal, using given message.
Returns the entries the user wishes to apply the action on."""
to_act = []

def ask_action(entry):
return yesno(
f"{message} '{entry.pprint(short=True)}'?",
Message(
msg,
params={"entry_title": entry.pprint(short=True)},
),
default=False,
)

Expand Down Expand Up @@ -415,9 +437,14 @@ def open_journal(journal_name, config, legacy=False):

if os.path.isdir(config["journal"]):
if config["encrypt"]:
print(
"Warning: This journal's config has 'encrypt' set to true, but this type of journal can't be encrypted.",
file=sys.stderr,
print_msg(
Message(
MsgText.ConfigEncryptedForUnencryptableJournalType,
MsgStyle.WARNING,
{
"journal_name": journal_name,
},
)
)

if config["journal"].strip("/").endswith(".dayone") or "entries" in os.listdir(
Expand Down
16 changes: 12 additions & 4 deletions jrnl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from jrnl.exception import JrnlException
from jrnl.messages import Message
from jrnl.messages import MsgText
from jrnl.messages import MsgType
from jrnl.messages import MsgStyle


def configure_logger(debug=False):
Expand Down Expand Up @@ -45,7 +45,13 @@ def cli(manual_args=None):

except KeyboardInterrupt:
status_code = 1
print_msg("\nKeyboardInterrupt", "\nAborted by user", msg=Message.ERROR)

print_msg(
Message(
MsgText.KeyboardInterruptMsg,
MsgStyle.ERROR_ON_NEW_LINE,
)
)

except Exception as e:
# uncaught exception
Expand All @@ -61,13 +67,15 @@ def cli(manual_args=None):
debug = True

if debug:
print("\n")
from rich.console import Console

traceback.print_tb(sys.exc_info()[2])
Console(stderr=True).print_exception(extra_lines=1)

print_msg(
Message(
MsgText.UncaughtException,
MsgType.ERROR,
MsgStyle.ERROR,
{"name": type(e).__name__, "exception": e},
)
)
Expand Down
4 changes: 0 additions & 4 deletions jrnl/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
if on_windows():
colorama.init()

WARNING_COLOR = colorama.Fore.YELLOW
ERROR_COLOR = colorama.Fore.RED
RESET_COLOR = colorama.Fore.RESET


def colorize(string, color, bold=False):
"""Returns the string colored with colorama.Fore.color. If the color set by
Expand Down
24 changes: 16 additions & 8 deletions jrnl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
"""
import platform
import sys

from jrnl.output import print_msg
from jrnl.exception import JrnlException
from jrnl.messages import Message
from jrnl.messages import MsgText
from jrnl.messages import MsgType
from jrnl.messages import MsgStyle
from jrnl.prompt import create_password


Expand Down Expand Up @@ -77,7 +79,7 @@ def postconfig_encrypt(args, config, original_config, **kwargs):
raise JrnlException(
Message(
MsgText.CannotEncryptJournalType,
MsgType.ERROR,
MsgStyle.ERROR,
{
"journal_name": args.journal_name,
"journal_type": journal.__class__.__name__,
Expand All @@ -95,9 +97,12 @@ def postconfig_encrypt(args, config, original_config, **kwargs):
journal.config["encrypt"] = True
new_journal.write(args.filename)

print(
f"Journal encrypted to {args.filename or new_journal.config['journal']}.",
file=sys.stderr,
print_msg(
Message(
MsgText.JournalEncryptedTo,
MsgStyle.NORMAL,
{"path": args.filename or new_journal.config["journal"]},
)
)

# Update the config, if we encrypted in place
Expand All @@ -120,9 +125,12 @@ def postconfig_decrypt(args, config, original_config, **kwargs):

new_journal = PlainJournal.from_journal(journal)
new_journal.write(args.filename)
print(
f"Journal decrypted to {args.filename or new_journal.config['journal']}.",
file=sys.stderr,
print_msg(
Message(
MsgText.JournalDecryptedTo,
MsgStyle.NORMAL,
{"path": args.filename or new_journal.config["journal"]},
)
)

# Update the config, if we decrypted in place
Expand Down
26 changes: 14 additions & 12 deletions jrnl/config.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import logging
import os
import sys

import colorama
from ruamel.yaml import YAML
import xdg.BaseDirectory

from . import __version__
from jrnl.output import list_journals
from jrnl.output import print_msg
from jrnl.exception import JrnlException
from jrnl.messages import Message
from jrnl.messages import MsgText
from jrnl.messages import MsgType
from jrnl.messages import MsgStyle

from .color import ERROR_COLOR
from .color import RESET_COLOR
from .output import list_journals
from .path import home_dir

# Constants
Expand Down Expand Up @@ -75,7 +73,7 @@ def get_config_path():
raise JrnlException(
Message(
MsgText.ConfigDirectoryIsFile,
MsgType.ERROR,
MsgStyle.ERROR,
{
"config_directory_path": os.path.join(
xdg.BaseDirectory.xdg_config_home, XDG_RESOURCE
Expand Down Expand Up @@ -143,11 +141,15 @@ def verify_config_colors(config):
if upper_color == "NONE":
continue
if not getattr(colorama.Fore, upper_color, None):
print(
"[{2}ERROR{3}: {0} set to invalid color: {1}]".format(
key, color, ERROR_COLOR, RESET_COLOR
),
file=sys.stderr,
print_msg(
Message(
MsgText.InvalidColor,
MsgStyle.NORMAL,
{
"key": key,
"color": color,
},
)
)
all_valid_colors = False
return all_valid_colors
Expand Down Expand Up @@ -197,7 +199,7 @@ def get_journal_name(args, config):
raise JrnlException(
Message(
MsgText.NoDefaultJournal,
MsgType.ERROR,
MsgStyle.ERROR,
{"journals": list_journals(config)},
),
)
Expand Down
Loading