From f0ff6b9926a7029ddd0d0f3835624d3442467f6b Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Tue, 10 Jan 2023 18:12:02 -0800 Subject: [PATCH] move journal class files into journals dir --- jrnl/color.py | 2 +- jrnl/commands.py | 6 +++--- jrnl/controller.py | 6 +++--- jrnl/{ => journals}/DayOneJournal.py | 9 +++++---- jrnl/{ => journals}/Entry.py | 4 ++-- jrnl/{ => journals}/FolderJournal.py | 7 ++++--- jrnl/{ => journals}/Journal.py | 23 ++++++++++++----------- jrnl/journals/__init__.py | 5 +++++ jrnl/plugins/dates_exporter.py | 4 ++-- jrnl/plugins/fancy_exporter.py | 4 ++-- jrnl/plugins/jrnl_importer.py | 2 +- jrnl/plugins/json_exporter.py | 4 ++-- jrnl/plugins/markdown_exporter.py | 4 ++-- jrnl/plugins/tag_exporter.py | 4 ++-- jrnl/plugins/text_exporter.py | 4 ++-- jrnl/plugins/util.py | 2 +- jrnl/plugins/xml_exporter.py | 4 ++-- jrnl/plugins/yaml_exporter.py | 6 +++--- jrnl/upgrade.py | 11 ++++++----- pyproject.toml | 2 ++ tests/unit/test_controller.py | 6 +++--- 21 files changed, 65 insertions(+), 54 deletions(-) rename jrnl/{ => journals}/DayOneJournal.py (98%) rename jrnl/{ => journals}/Entry.py (98%) rename jrnl/{ => journals}/FolderJournal.py (97%) rename jrnl/{ => journals}/Journal.py (96%) create mode 100644 jrnl/journals/__init__.py diff --git a/jrnl/color.py b/jrnl/color.py index 2aaadad5..37b7a631 100644 --- a/jrnl/color.py +++ b/jrnl/color.py @@ -11,7 +11,7 @@ import colorama from jrnl.os_compat import on_windows if TYPE_CHECKING: - from jrnl.Entry import Entry + from jrnl.journals import Entry if on_windows(): colorama.init() diff --git a/jrnl/commands.py b/jrnl/commands.py index a85e97e9..dad63db3 100644 --- a/jrnl/commands.py +++ b/jrnl/commands.py @@ -69,7 +69,7 @@ def postconfig_list(args: argparse.Namespace, config: dict, **_) -> int: @cmd_requires_valid_journal_name def postconfig_import(args: argparse.Namespace, config: dict, **_) -> int: - from jrnl.Journal import open_journal + from jrnl.journals import open_journal from jrnl.plugins import get_importer # Requires opening the journal @@ -90,7 +90,7 @@ def postconfig_encrypt( """ from jrnl.config import update_config from jrnl.install import save_config - from jrnl.Journal import open_journal + from jrnl.journals import open_journal # Open the journal journal = open_journal(args.journal_name, config) @@ -145,7 +145,7 @@ def postconfig_decrypt( """Decrypts into new file. If filename is not set, we encrypt the journal file itself.""" from jrnl.config import update_config from jrnl.install import save_config - from jrnl.Journal import open_journal + from jrnl.journals import open_journal journal = open_journal(args.journal_name, config) diff --git a/jrnl/controller.py b/jrnl/controller.py index e3a8f283..8fc4b250 100644 --- a/jrnl/controller.py +++ b/jrnl/controller.py @@ -15,8 +15,8 @@ from jrnl.config import scope_config from jrnl.editor import get_text_from_editor from jrnl.editor import get_text_from_stdin from jrnl.exception import JrnlException -from jrnl.Journal import Journal -from jrnl.Journal import open_journal +from jrnl.journals import Journal +from jrnl.journals import open_journal from jrnl.messages import Message from jrnl.messages import MsgStyle from jrnl.messages import MsgText @@ -28,7 +28,7 @@ from jrnl.path import expand_path if TYPE_CHECKING: from argparse import Namespace - from jrnl.Entry import Entry + from jrnl.journals import Entry def start(args: "Namespace"): diff --git a/jrnl/DayOneJournal.py b/jrnl/journals/DayOneJournal.py similarity index 98% rename from jrnl/DayOneJournal.py rename to jrnl/journals/DayOneJournal.py index e907ce82..ace6a5f4 100644 --- a/jrnl/DayOneJournal.py +++ b/jrnl/journals/DayOneJournal.py @@ -17,13 +17,14 @@ from xml.parsers.expat import ExpatError import tzlocal -from jrnl import Entry -from jrnl import Journal from jrnl import __title__ from jrnl import __version__ +from .Entry import Entry +from .Journal import Journal -class DayOne(Journal.Journal): + +class DayOne(Journal): """A special Journal handling DayOne files""" # InvalidFileException was added to plistlib in Python3.4 @@ -63,7 +64,7 @@ class DayOne(Journal.Journal): if timezone.key != "UTC": date = date.replace(fold=1) + timezone.utcoffset(date) - entry = Entry.Entry( + entry = Entry( self, date, text=dict_entry["Entry Text"], diff --git a/jrnl/Entry.py b/jrnl/journals/Entry.py similarity index 98% rename from jrnl/Entry.py rename to jrnl/journals/Entry.py index 78575027..4439261e 100644 --- a/jrnl/Entry.py +++ b/jrnl/journals/Entry.py @@ -9,8 +9,8 @@ from typing import TYPE_CHECKING import ansiwrap -from .color import colorize -from .color import highlight_tags_with_background_color +from jrnl.color import colorize +from jrnl.color import highlight_tags_with_background_color if TYPE_CHECKING: from .Journal import Journal diff --git a/jrnl/FolderJournal.py b/jrnl/journals/FolderJournal.py similarity index 97% rename from jrnl/FolderJournal.py rename to jrnl/journals/FolderJournal.py index 055d9408..316a10f2 100644 --- a/jrnl/FolderJournal.py +++ b/jrnl/journals/FolderJournal.py @@ -6,11 +6,12 @@ import fnmatch import os from typing import TYPE_CHECKING -from jrnl import Journal from jrnl import time +from .Journal import Journal + if TYPE_CHECKING: - from jrnl.Entry import Entry + from jrnl.journals import Entry def get_files(journal_config: str) -> list[str]: @@ -22,7 +23,7 @@ def get_files(journal_config: str) -> list[str]: return filenames -class Folder(Journal.Journal): +class Folder(Journal): """A Journal handling multiple files in a folder""" def __init__(self, name: str = "default", **kwargs): diff --git a/jrnl/Journal.py b/jrnl/journals/Journal.py similarity index 96% rename from jrnl/Journal.py rename to jrnl/journals/Journal.py index 51c0b746..472f2edd 100644 --- a/jrnl/Journal.py +++ b/jrnl/journals/Journal.py @@ -6,7 +6,6 @@ import logging import os import re -from jrnl import Entry from jrnl import time from jrnl.config import validate_journal_name from jrnl.encryption import determine_encryption_method @@ -17,6 +16,8 @@ from jrnl.output import print_msg from jrnl.path import expand_path from jrnl.prompt import yesno +from .Entry import Entry + class Tag: def __init__(self, name, count=0): @@ -184,11 +185,11 @@ class Journal: if entries: entries[-1].text = journal_txt[last_entry_pos : match.start()] last_entry_pos = match.end() - entries.append(Entry.Entry(self, date=new_date)) + entries.append(Entry(self, date=new_date)) # If no entries were found, treat all the existing text as an entry made now if not entries: - entries.append(Entry.Entry(self, date=time.parse("now"))) + entries.append(Entry(self, date=time.parse("now"))) # Fill in the text of the last entry entries[-1].text = journal_txt[last_entry_pos:] @@ -354,7 +355,7 @@ class Journal: ) if not date: # Still nothing? Meh, just live in the moment. date = time.parse("now") - entry = Entry.Entry(self, date, raw, starred=starred) + entry = Entry(self, date, raw, starred=starred) entry.modified = True self.entries.append(entry) if sort: @@ -410,7 +411,7 @@ class LegacyJournal(Journal): else: starred = False - current_entry = Entry.Entry( + current_entry = Entry( self, date=new_date, text=line[date_length + 1 :], starred=starred ) except ValueError: @@ -455,21 +456,21 @@ def open_journal(journal_name, config, legacy=False): if config["journal"].strip("/").endswith(".dayone") or "entries" in os.listdir( config["journal"] ): - from jrnl import DayOneJournal + from jrnl.journals import DayOne - return DayOneJournal.DayOne(**config).open() + return DayOne(**config).open() else: - from jrnl import FolderJournal + from jrnl.journals import Folder - return FolderJournal.Folder(journal_name, **config).open() + return Folder(journal_name, **config).open() if not config["encrypt"]: if legacy: return LegacyJournal(journal_name, **config).open() if config["journal"].endswith(os.sep): - from jrnl import FolderJournal + from jrnl.journals import Folder - return FolderJournal.Folder(journal_name, **config).open() + return Folder(journal_name, **config).open() return Journal(journal_name, **config).open() if legacy: diff --git a/jrnl/journals/__init__.py b/jrnl/journals/__init__.py new file mode 100644 index 00000000..eb3dc44f --- /dev/null +++ b/jrnl/journals/__init__.py @@ -0,0 +1,5 @@ +from .DayOneJournal import DayOne +from .Entry import Entry +from .FolderJournal import Folder +from .Journal import Journal +from .Journal import open_journal diff --git a/jrnl/plugins/dates_exporter.py b/jrnl/plugins/dates_exporter.py index 1e2ae0dc..38d101dd 100644 --- a/jrnl/plugins/dates_exporter.py +++ b/jrnl/plugins/dates_exporter.py @@ -7,8 +7,8 @@ from typing import TYPE_CHECKING from jrnl.plugins.text_exporter import TextExporter if TYPE_CHECKING: - from jrnl.Entry import Entry - from jrnl.Journal import Journal + from jrnl.journals import Entry + from jrnl.journals import Journal class DatesExporter(TextExporter): diff --git a/jrnl/plugins/fancy_exporter.py b/jrnl/plugins/fancy_exporter.py index 4b800754..447f1347 100644 --- a/jrnl/plugins/fancy_exporter.py +++ b/jrnl/plugins/fancy_exporter.py @@ -13,8 +13,8 @@ from jrnl.messages import MsgText from jrnl.plugins.text_exporter import TextExporter if TYPE_CHECKING: - from jrnl.Entry import Entry - from jrnl.Journal import Journal + from jrnl.journals import Entry + from jrnl.journals import Journal class FancyExporter(TextExporter): diff --git a/jrnl/plugins/jrnl_importer.py b/jrnl/plugins/jrnl_importer.py index b5bc0490..8c326182 100644 --- a/jrnl/plugins/jrnl_importer.py +++ b/jrnl/plugins/jrnl_importer.py @@ -11,7 +11,7 @@ from jrnl.messages import MsgText from jrnl.output import print_msg if TYPE_CHECKING: - from jrnl.Journal import Journal + from jrnl.journals import Journal class JRNLImporter: diff --git a/jrnl/plugins/json_exporter.py b/jrnl/plugins/json_exporter.py index 3a7c56ac..66d2bcc3 100644 --- a/jrnl/plugins/json_exporter.py +++ b/jrnl/plugins/json_exporter.py @@ -8,8 +8,8 @@ from jrnl.plugins.text_exporter import TextExporter from jrnl.plugins.util import get_tags_count if TYPE_CHECKING: - from jrnl.Entry import Entry - from jrnl.Journal import Journal + from jrnl.journals import Entry + from jrnl.journals import Journal class JSONExporter(TextExporter): diff --git a/jrnl/plugins/markdown_exporter.py b/jrnl/plugins/markdown_exporter.py index 9335847f..1512903d 100644 --- a/jrnl/plugins/markdown_exporter.py +++ b/jrnl/plugins/markdown_exporter.py @@ -12,8 +12,8 @@ from jrnl.output import print_msg from jrnl.plugins.text_exporter import TextExporter if TYPE_CHECKING: - from jrnl.Entry import Entry - from jrnl.Journal import Journal + from jrnl.journals import Entry + from jrnl.journals import Journal class MarkdownExporter(TextExporter): diff --git a/jrnl/plugins/tag_exporter.py b/jrnl/plugins/tag_exporter.py index 5702978d..b8b5eb79 100644 --- a/jrnl/plugins/tag_exporter.py +++ b/jrnl/plugins/tag_exporter.py @@ -7,8 +7,8 @@ from jrnl.plugins.text_exporter import TextExporter from jrnl.plugins.util import get_tags_count if TYPE_CHECKING: - from jrnl.Entry import Entry - from jrnl.Journal import Journal + from jrnl.journals import Entry + from jrnl.journals import Journal class TagExporter(TextExporter): diff --git a/jrnl/plugins/text_exporter.py b/jrnl/plugins/text_exporter.py index a839ee88..0a514da1 100644 --- a/jrnl/plugins/text_exporter.py +++ b/jrnl/plugins/text_exporter.py @@ -13,8 +13,8 @@ from jrnl.messages import MsgText from jrnl.output import print_msg if TYPE_CHECKING: - from jrnl.Entry import Entry - from jrnl.Journal import Journal + from jrnl.journals import Entry + from jrnl.journals import Journal class TextExporter: diff --git a/jrnl/plugins/util.py b/jrnl/plugins/util.py index 86eb5b17..ceaa0b04 100644 --- a/jrnl/plugins/util.py +++ b/jrnl/plugins/util.py @@ -4,7 +4,7 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: - from jrnl.Journal import Journal + from jrnl.journals import Journal def get_tags_count(journal: "Journal") -> set[tuple[int, str]]: diff --git a/jrnl/plugins/xml_exporter.py b/jrnl/plugins/xml_exporter.py index 3bda38e6..a0349af9 100644 --- a/jrnl/plugins/xml_exporter.py +++ b/jrnl/plugins/xml_exporter.py @@ -8,8 +8,8 @@ from jrnl.plugins.json_exporter import JSONExporter from jrnl.plugins.util import get_tags_count if TYPE_CHECKING: - from jrnl.Entry import Entry - from jrnl.Journal import Journal + from jrnl.journals import Entry + from jrnl.journals import Journal class XMLExporter(JSONExporter): diff --git a/jrnl/plugins/yaml_exporter.py b/jrnl/plugins/yaml_exporter.py index 2927243e..d960ef8a 100644 --- a/jrnl/plugins/yaml_exporter.py +++ b/jrnl/plugins/yaml_exporter.py @@ -13,8 +13,8 @@ from jrnl.output import print_msg from jrnl.plugins.text_exporter import TextExporter if TYPE_CHECKING: - from jrnl.Entry import Entry - from jrnl.Journal import Journal + from jrnl.journals import Entry + from jrnl.journals import Journal class YAMLExporter(TextExporter): @@ -34,7 +34,7 @@ class YAMLExporter(TextExporter): body = body_wrapper + entry.body tagsymbols = entry.journal.config["tagsymbols"] - # see also Entry.Entry.rag_regex + # see also Entry.rag_regex multi_tag_regex = re.compile(rf"(?u)^\s*([{tagsymbols}][-+*#/\w]+\s*)+$") """Increase heading levels in body text""" diff --git a/jrnl/upgrade.py b/jrnl/upgrade.py index d390844c..1b6e500d 100644 --- a/jrnl/upgrade.py +++ b/jrnl/upgrade.py @@ -4,12 +4,13 @@ import logging import os -from jrnl import Journal from jrnl import __version__ from jrnl.config import is_config_json from jrnl.config import load_config from jrnl.config import scope_config from jrnl.exception import JrnlException +from jrnl.journals import Journal +from jrnl.journals import open_journal from jrnl.messages import Message from jrnl.messages import MsgStyle from jrnl.messages import MsgText @@ -129,14 +130,14 @@ def upgrade_jrnl(config_path: str) -> None: ) backup(path, binary=True) - old_journal = Journal.open_journal( + old_journal = open_journal( journal_name, scope_config(config, journal_name), legacy=True ) logging.debug(f"Clearing encryption method for '{journal_name}' journal") # Update the encryption method - new_journal = Journal.Journal.from_journal(old_journal) + new_journal = Journal.from_journal(old_journal) new_journal.config["encrypt"] = "jrnlv2" new_journal._get_encryption_method() # Copy over password (jrnlv1 only supported password-based encryption) @@ -156,10 +157,10 @@ def upgrade_jrnl(config_path: str) -> None: ) backup(path) - old_journal = Journal.open_journal( + old_journal = open_journal( journal_name, scope_config(config, journal_name), legacy=True ) - all_journals.append(Journal.Journal.from_journal(old_journal)) + all_journals.append(Journal.from_journal(old_journal)) # loop through lists to validate failed_journals = [j for j in all_journals if not j.validate_parsing()] diff --git a/pyproject.toml b/pyproject.toml index 715180a5..da931d0b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -157,6 +157,8 @@ pycodestyle = [ "flake8-*" = ["+*"] flake8-black = ["-BLK901"] +[tool.flakeheaven.exceptions."jrnl/journals/__init__.py"] +pyflakes = ["-F401"] [build-system] requires = ["poetry-core>=1.0.0"] diff --git a/tests/unit/test_controller.py b/tests/unit/test_controller.py index 4862dc8e..d60cd2d6 100644 --- a/tests/unit/test_controller.py +++ b/tests/unit/test_controller.py @@ -19,10 +19,10 @@ def random_string(): @pytest.mark.parametrize("export_format", ["pretty", "short"]) @mock.patch("builtins.print") -@mock.patch("jrnl.Journal.Journal.pprint") +@mock.patch("jrnl.controller.Journal.pprint") def test_display_search_results_pretty_short(mock_pprint, mock_print, export_format): mock_args = parse_args(["--format", export_format]) - test_journal = mock.Mock(wraps=jrnl.Journal.Journal) + test_journal = mock.Mock(wraps=jrnl.journals.Journal) _display_search_results(mock_args, test_journal) @@ -40,7 +40,7 @@ def test_display_search_results_builtin_plugins( test_filename = random_string mock_args = parse_args(["--format", export_format, "--file", test_filename]) - test_journal = mock.Mock(wraps=jrnl.Journal.Journal) + test_journal = mock.Mock(wraps=jrnl.journals.Journal) mock_export = mock.Mock() mock_exporter.return_value.export = mock_export