Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug that prevented --format pretty and --format short from working #1177

Merged
merged 32 commits into from
Mar 6, 2021
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1f8351a
behavior test for format --pretty
sriniv27 Feb 3, 2021
d44a6e8
add pretty exporter
sriniv27 Feb 3, 2021
2251cd3
include vscode debugging config
sriniv27 Feb 3, 2021
3d69131
config file for pretty formatting
sriniv27 Feb 3, 2021
bfb975b
update gitignore
sriniv27 Feb 3, 2021
1739cee
basic colorize unittest
sriniv27 Feb 4, 2021
15efc56
implement behave test steps for format --pretty
sriniv27 Feb 4, 2021
ce930a2
make color handling more robust
sriniv27 Feb 4, 2021
c9404d0
rework core run() so config_upgrade can be tested
sriniv27 Feb 4, 2021
be8c48d
formatting for pretty exporter
sriniv27 Feb 4, 2021
ca45c76
base .vscode/launch.json and ignore further user modifications from SCM
sriniv27 Feb 4, 2021
b8daeec
fix linting errors
sriniv27 Feb 4, 2021
ddb62b7
make format
sriniv27 Feb 4, 2021
457375d
Revert "make format"
sriniv27 Feb 6, 2021
166ca60
Revert "make color handling more robust"
sriniv27 Feb 6, 2021
cc9a2f1
Revert "config file for pretty formatting"
sriniv27 Feb 6, 2021
afc48bb
delete prettyexporter
sriniv27 Feb 6, 2021
06f5ef3
Revert "Revert "config file for pretty formatting""
sriniv27 Feb 7, 2021
1bd5931
delete remaining unneded code
sriniv27 Feb 7, 2021
6e3574c
define dummy exporter type pretty
sriniv27 Feb 7, 2021
2642cde
first cut at wrapper method to export journals
sriniv27 Feb 7, 2021
ddebb67
match indentation to rest of file
sriniv27 Feb 7, 2021
ac0233a
behave test and dummy short exporter
sriniv27 Feb 7, 2021
694ba3b
unittest export_journal
sriniv27 Feb 7, 2021
9d60911
update unittest
sriniv27 Feb 7, 2021
e0f7736
make format
sriniv27 Feb 7, 2021
9b2633a
fix bad mock import
sriniv27 Feb 7, 2021
edc8cd9
address formatting
sriniv27 Feb 14, 2021
0a6e5f9
Address code review
sriniv27 Feb 27, 2021
b1adedb
Update jrnl/jrnl.py with suggestion
sriniv27 Mar 3, 2021
77ac858
update tests and imports for removal of _export_journal
sriniv27 Mar 3, 2021
8256414
Update core.py
sriniv27 Mar 3, 2021
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
26 changes: 26 additions & 0 deletions features/format.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
Feature: Custom formats

Scenario Outline: Short printing via --format flag
Given We use the config "<config>.yaml"
And we use the password "test" if prompted
When we run "jrnl --format short -3"
Then we should get no error

Examples: configs
| config |
| basic_onefile |
| basic_encrypted |
| basic_folder |
| basic_dayone |

Scenario Outline: Pretty Printing aka the Default
Given We use the config "<config>.yaml"
And we use the password "test" if prompted
When we run "jrnl --format pretty -3"
Then we should get no error

Examples: configs
| config |
| basic_onefile |
| basic_encrypted |
| basic_folder |
| basic_dayone |

Scenario Outline: JSON format
Given we use the config "<config>.yaml"
And we use the password "test" if prompted
Expand Down
7 changes: 7 additions & 0 deletions features/steps/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import toml
import yaml


import jrnl.time
from jrnl import Journal
from jrnl import __version__
Expand Down Expand Up @@ -394,6 +395,12 @@ def all_input_was_used(context):
def run(context, command, text=""):
text = text or context.text or ""

if "config_path" in context and context.config_path is not None:
with open(context.config_path) as f:
context.jrnl_config = yaml.load(f, Loader=yaml.FullLoader)
else:
context.jrnl_config = None

if "cache_dir" in context and context.cache_dir is not None:
cache_dir = os.path.join("features", "cache", context.cache_dir)
command = command.format(cache_dir=cache_dir)
Expand Down
2 changes: 1 addition & 1 deletion features/steps/export_steps.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Copyright (C) 2012-2021 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html


import json
import os
import shutil
import random
import string
from xml.etree import ElementTree

from behave import given
from behave import then

Expand Down
28 changes: 24 additions & 4 deletions jrnl/jrnl.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Copyright (C) 2012-2021 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html

from argparse import Namespace
import logging
import sys

from . import install
from . import plugins
from .Journal import open_journal
from .Journal import Journal, open_journal
from .color import ERROR_COLOR
from .color import RESET_COLOR
from .config import get_journal_name
Expand Down Expand Up @@ -316,17 +317,36 @@ def _delete_search_results(journal, old_entries, **kwargs):


def _display_search_results(args, journal, **kwargs):
if args.short:
if args.short or args.export == "short":
print(journal.pprint(short=True))

elif args.export == "pretty":
print(journal.pprint())

elif args.tags:
print(plugins.get_exporter("tags").export(journal))

elif args.export:
exporter = plugins.get_exporter(args.export)
print(exporter.export(journal, args.filename))
_export_journal(args, journal)
sriniv27 marked this conversation as resolved.
Show resolved Hide resolved
sriniv27 marked this conversation as resolved.
Show resolved Hide resolved

elif kwargs["config"].get("display_format"):
exporter = plugins.get_exporter(kwargs["config"]["display_format"])
print(exporter.export(journal, args.filename))
else:
print(journal.pprint())


def _export_journal(args: Namespace, journal: Journal):
"""Export journal using supplied export format
Export formats "short" and "pretty" are shorthands for pre-configured
jrnl behavior, hence those export formats bypass the export plugins.

:param args: parsed arguments
:type args: Namespace
:param journal: journal under use
:type journal: Journal
"""
# There are no exporter 'plugins' for pretty and short. We shouldn't expect this to be called for those two export formats.
assert args.export and args.export not in ("pretty", "short")
exporter = plugins.get_exporter(args.export)
print(exporter.export(journal, args.filename))
sriniv27 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions jrnl/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
__importers = [JRNLImporter]

__exporter_types = {name: plugin for plugin in __exporters for name in plugin.names}
__exporter_types["pretty"] = None
__exporter_types["short"] = None
__importer_types = {name: plugin for plugin in __importers for name in plugin.names}

EXPORT_FORMATS = sorted(__exporter_types.keys())
Expand Down
17 changes: 17 additions & 0 deletions tests/test_color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

from jrnl.color import colorize
from colorama import Fore, Style


@pytest.fixture()
def data_fixture():
string = "Zwei peanuts walked into a bar"
sriniv27 marked this conversation as resolved.
Show resolved Hide resolved
yield string


def test_colorize(data_fixture):
sriniv27 marked this conversation as resolved.
Show resolved Hide resolved
string = data_fixture
colorized_string = colorize(string, "BLUE", True)

assert colorized_string == Style.BRIGHT + Fore.BLUE + string + Style.RESET_ALL
42 changes: 42 additions & 0 deletions tests/test_display.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import argparse
import jrnl
import pytest
from unittest import mock
from jrnl.jrnl import _display_search_results, _export_journal


# fmt: off
# see: https://github.com/psf/black/issues/664
@pytest.mark.parametrize("export_format", [ "pretty", "short",])
#fmt: on
@mock.patch.object(argparse, "Namespace", return_value={"export": "markdown", "filename": "irrele.vant"})
def test_export_format(mock_args, export_format):

test_journal = jrnl.Journal.Journal
mock_args.export = export_format
#fmt: off
# see: https://github.com/psf/black/issues/664
with mock.patch("builtins.print") as mock_spy_print, \
mock.patch('jrnl.Journal.Journal.pprint') as mock_pprint:
_display_search_results(mock_args, test_journal)
mock_spy_print.assert_called_once_with(mock_pprint())
#fmt: on


@mock.patch.object(argparse, "Namespace", return_value={"export": "markdown", "filename": "foo.jrnl"})
def test_export_plugin(mock_args):
export_format = mock_args.return_value["export"]

test_journal = jrnl.Journal.Journal
mock_args.export = export_format
mock_args.filename = mock_args.return_value['filename']

# fmt: off
# see: https://github.com/psf/black/issues/664
with mock.patch("builtins.print") as print_spy, \
mock.patch("jrnl.plugins.get_exporter") as mock_get_exporter, \
mock.patch("jrnl.Journal.Journal.pprint") :
_export_journal(mock_args, test_journal)
# fmt: on
mock_get_exporter.assert_called_once_with(export_format)
print_spy.assert_called_once_with(mock_get_exporter().export(test_journal,mock_args.filename))