mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-17 11:38:32 +02:00
update text_exporter with new message handling, get rid of old color constants
This commit is contained in:
parent
28712132b7
commit
eeca1e2338
3 changed files with 27 additions and 27 deletions
|
@ -9,10 +9,6 @@ from .os_compat import on_windows
|
|||
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
|
||||
|
|
|
@ -11,8 +11,6 @@ from rich.text import Text
|
|||
from rich import box
|
||||
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 MsgType
|
||||
from jrnl.messages import MsgText
|
||||
|
|
|
@ -6,8 +6,10 @@ import os
|
|||
import re
|
||||
import unicodedata
|
||||
|
||||
from jrnl.color import ERROR_COLOR
|
||||
from jrnl.color import RESET_COLOR
|
||||
from jrnl.output import print_msg
|
||||
from jrnl.messages import Message
|
||||
from jrnl.messages import MsgText
|
||||
from jrnl.messages import MsgType
|
||||
|
||||
|
||||
class TextExporter:
|
||||
|
@ -29,14 +31,18 @@ class TextExporter:
|
|||
@classmethod
|
||||
def write_file(cls, journal, path):
|
||||
"""Exports a journal into a single file."""
|
||||
try:
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
f.write(cls.export_journal(journal))
|
||||
return f"[Journal exported to {path}]"
|
||||
except IOError as e:
|
||||
return f"[{ERROR_COLOR}ERROR{RESET_COLOR}: {e.filename} {e.strerror}]"
|
||||
except RuntimeError as e:
|
||||
return e
|
||||
print_msg(
|
||||
Message(
|
||||
MsgText.JournalExportedTo,
|
||||
MsgType.NORMAL,
|
||||
{
|
||||
"path": path,
|
||||
},
|
||||
)
|
||||
)
|
||||
return ""
|
||||
|
||||
@classmethod
|
||||
def make_filename(cls, entry):
|
||||
|
@ -48,17 +54,17 @@ class TextExporter:
|
|||
def write_files(cls, journal, path):
|
||||
"""Exports a journal into individual files for each entry."""
|
||||
for entry in journal.entries:
|
||||
try:
|
||||
full_path = os.path.join(path, cls.make_filename(entry))
|
||||
with open(full_path, "w", encoding="utf-8") as f:
|
||||
f.write(cls.export_entry(entry))
|
||||
except IOError as e:
|
||||
return "[{2}ERROR{3}: {0} {1}]".format(
|
||||
e.filename, e.strerror, ERROR_COLOR, RESET_COLOR
|
||||
print_msg(
|
||||
Message(
|
||||
MsgText.JournalExportedTo,
|
||||
MsgType.NORMAL,
|
||||
{"path": path},
|
||||
)
|
||||
except RuntimeError as e:
|
||||
return e
|
||||
return "[Journal exported to {}]".format(path)
|
||||
)
|
||||
return ""
|
||||
|
||||
def _slugify(string):
|
||||
"""Slugifies a string.
|
||||
|
|
Loading…
Add table
Reference in a new issue