PR feedback

make exception more informative

update check_linewrap signature in src and test

make check_linewrap a free function
This commit is contained in:
Suhas 2021-04-10 09:59:59 -04:00
parent e063fa9715
commit 502d47f223
4 changed files with 22 additions and 16 deletions

3
.gitignore vendored
View file

@ -61,6 +61,3 @@ coverage.xml
.coverage .coverage
.vscode/tasks.json .vscode/tasks.json
todo.txt todo.txt
tags
.vimrc

View file

@ -33,8 +33,10 @@ class JrnlError(Exception):
), ),
"LineWrapTooSmallForDateFormat": textwrap.dedent( "LineWrapTooSmallForDateFormat": textwrap.dedent(
""" """
The provided linewrap value of {config_linewrap} is too small The provided linewrap value of {config_linewrap} is too small by {columns} columns
to display the timestamps in the configured time format. to display the timestamps in the configured time format for journal {journal}.
You can avoid this error by specifying a linewrap value that is larger by at least {columns} in the configuration file or by using --config-override at the command line
""" """
), ),
} }

View file

@ -41,7 +41,8 @@ class FancyExporter(TextExporter):
card = [ card = [
cls.border_a + cls.border_b * (initial_linewrap) + cls.border_c + date_str cls.border_a + cls.border_b * (initial_linewrap) + cls.border_c + date_str
] ]
cls.check_linewrap(linewrap, card) check_provided_linewrap_viability(linewrap, card, entry.journal)
w = TextWrapper( w = TextWrapper(
width=initial_linewrap, width=initial_linewrap,
initial_indent=cls.border_g + " ", initial_indent=cls.border_g + " ",
@ -75,14 +76,20 @@ class FancyExporter(TextExporter):
card.append(cls.border_l + cls.border_b * body_linewrap + cls.border_m) card.append(cls.border_l + cls.border_b * body_linewrap + cls.border_m)
return "\n".join(card) return "\n".join(card)
@classmethod
def check_linewrap(cls, linewrap, card):
if len(card[0]) > linewrap:
raise JrnlError("LineWrapTooSmallForDateFormat", config_linewrap=linewrap)
else:
pass
@classmethod @classmethod
def export_journal(cls, journal): def export_journal(cls, journal):
"""Returns a unicode representation of an entire journal.""" """Returns a unicode representation of an entire journal."""
return "\n".join(cls.export_entry(entry) for entry in journal) return "\n".join(cls.export_entry(entry) for entry in journal)
def check_provided_linewrap_viability(linewrap, card, journal):
if len(card[0]) > linewrap:
width_violation = len(card[0]) - linewrap
raise JrnlError(
"LineWrapTooSmallForDateFormat",
config_linewrap=linewrap,
columns=width_violation,
journal=journal,
)
else:
pass

View file

@ -1,6 +1,5 @@
from jrnl.exception import JrnlError from jrnl.exception import JrnlError
from jrnl.plugins.fancy_exporter import FancyExporter from jrnl.plugins.fancy_exporter import check_provided_linewrap_viability
import pytest import pytest
@ -30,10 +29,11 @@ def build_card_header(datestr):
class TestFancy: class TestFancy:
def test_too_small_linewrap(self, datestr): def test_too_small_linewrap(self, datestr):
journal = "test_journal"
content = build_card_header(datestr) content = build_card_header(datestr)
total_linewrap = 12 total_linewrap = 12
with pytest.raises(JrnlError) as e: with pytest.raises(JrnlError) as e:
FancyExporter.check_linewrap(total_linewrap, [content]) check_provided_linewrap_viability(total_linewrap, [content], journal)
assert e.value.error_type == "LineWrapTooSmallForDateFormat" assert e.value.error_type == "LineWrapTooSmallForDateFormat"