update text_exporter with new message handling, get rid of old color constants

This commit is contained in:
Jonathan Wren 2022-05-01 10:39:08 -07:00
parent 28712132b7
commit eeca1e2338
3 changed files with 27 additions and 27 deletions

View file

@ -9,10 +9,6 @@ from .os_compat import on_windows
if on_windows(): if on_windows():
colorama.init() colorama.init()
WARNING_COLOR = colorama.Fore.YELLOW
ERROR_COLOR = colorama.Fore.RED
RESET_COLOR = colorama.Fore.RESET
def colorize(string, color, bold=False): def colorize(string, color, bold=False):
"""Returns the string colored with colorama.Fore.color. If the color set by """Returns the string colored with colorama.Fore.color. If the color set by

View file

@ -11,8 +11,6 @@ from rich.text import Text
from rich import box from rich import box
from rich.console import Console from rich.console import Console
from jrnl.color import RESET_COLOR
from jrnl.color import WARNING_COLOR
from jrnl.messages import Message from jrnl.messages import Message
from jrnl.messages import MsgType from jrnl.messages import MsgType
from jrnl.messages import MsgText from jrnl.messages import MsgText

View file

@ -6,8 +6,10 @@ import os
import re import re
import unicodedata import unicodedata
from jrnl.color import ERROR_COLOR from jrnl.output import print_msg
from jrnl.color import RESET_COLOR from jrnl.messages import Message
from jrnl.messages import MsgText
from jrnl.messages import MsgType
class TextExporter: class TextExporter:
@ -29,14 +31,18 @@ class TextExporter:
@classmethod @classmethod
def write_file(cls, journal, path): def write_file(cls, journal, path):
"""Exports a journal into a single file.""" """Exports a journal into a single file."""
try: with open(path, "w", encoding="utf-8") as f:
with open(path, "w", encoding="utf-8") as f: f.write(cls.export_journal(journal))
f.write(cls.export_journal(journal)) print_msg(
return f"[Journal exported to {path}]" Message(
except IOError as e: MsgText.JournalExportedTo,
return f"[{ERROR_COLOR}ERROR{RESET_COLOR}: {e.filename} {e.strerror}]" MsgType.NORMAL,
except RuntimeError as e: {
return e "path": path,
},
)
)
return ""
@classmethod @classmethod
def make_filename(cls, entry): def make_filename(cls, entry):
@ -48,17 +54,17 @@ class TextExporter:
def write_files(cls, journal, path): def write_files(cls, journal, path):
"""Exports a journal into individual files for each entry.""" """Exports a journal into individual files for each entry."""
for entry in journal.entries: for entry in journal.entries:
try: full_path = os.path.join(path, cls.make_filename(entry))
full_path = os.path.join(path, cls.make_filename(entry)) with open(full_path, "w", encoding="utf-8") as f:
with open(full_path, "w", encoding="utf-8") as f: f.write(cls.export_entry(entry))
f.write(cls.export_entry(entry)) print_msg(
except IOError as e: Message(
return "[{2}ERROR{3}: {0} {1}]".format( MsgText.JournalExportedTo,
e.filename, e.strerror, ERROR_COLOR, RESET_COLOR MsgType.NORMAL,
) {"path": path},
except RuntimeError as e: )
return e )
return "[Journal exported to {}]".format(path) return ""
def _slugify(string): def _slugify(string):
"""Slugifies a string. """Slugifies a string.