Skip to content

Commit

Permalink
Refactor flow for easier access to some files (avoid things like `jrn…
Browse files Browse the repository at this point in the history
…l.Journal.Journal` and `jrnl.jrnl` co-existing) (#1662)

* run format

* rename cli.py to main.py

* rename jrnl.py to controller.py

* move journal class files into journals dir

* rename start -> run in controller.py
  • Loading branch information
wren committed Jan 14, 2023
1 parent 7be67ac commit fff05eb
Show file tree
Hide file tree
Showing 26 changed files with 84 additions and 69 deletions.
4 changes: 2 additions & 2 deletions jrnl/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import sys

from jrnl.cli import cli
from jrnl.main import run

if __name__ == "__main__":
sys.exit(cli())
sys.exit(run())
2 changes: 1 addition & 1 deletion jrnl/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
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()
Expand Down
6 changes: 3 additions & 3 deletions jrnl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions jrnl/jrnl.py → jrnl/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
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
Expand All @@ -28,7 +28,7 @@
if TYPE_CHECKING:
from argparse import Namespace

from jrnl.Entry import Entry
from jrnl.journals import Entry


def run(args: "Namespace"):
Expand Down
4 changes: 2 additions & 2 deletions jrnl/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def upgrade_config(config_data: dict, alt_config_path: str | None = None) -> Non
if missing_keys:
for key in missing_keys:
config_data[key] = default_config[key]
different_version = (config_data["version"] != __version__)

different_version = config_data["version"] != __version__
if different_version:
config_data["version"] = __version__

Expand Down
19 changes: 12 additions & 7 deletions jrnl/DayOneJournal.py → jrnl/journals/DayOneJournal.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@

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
Expand Down Expand Up @@ -63,7 +64,7 @@ def open(self) -> "DayOne":
if timezone.key != "UTC":
date = date.replace(fold=1) + timezone.utcoffset(date)

entry = Entry.Entry(
entry = Entry(
self,
date,
text=dict_entry["Entry Text"],
Expand All @@ -74,7 +75,8 @@ def open(self) -> "DayOne":
self.config["tagsymbols"][0] + tag.lower()
for tag in dict_entry.get("Tags", [])
]
if entry._tags: entry._tags.sort()
if entry._tags:
entry._tags.sort()

"""Extended DayOne attributes"""
# just ignore it if the keys don't exist
Expand Down Expand Up @@ -196,7 +198,8 @@ def parse_editable_str(self, edited: str) -> None:

for entry in entries_from_editor:
entry = self._get_and_remove_uuid_from_entry(entry)
if entry._tags: entry._tags.sort()
if entry._tags:
entry._tags.sort()

# Remove deleted entries
edited_uuids = [e.uuid for e in entries_from_editor]
Expand All @@ -207,7 +210,9 @@ def parse_editable_str(self, edited: str) -> None:
for old_entry in self.entries:
if entry.uuid == old_entry.uuid:
if old_entry._tags:
tags_not_in_body = [tag for tag in old_entry._tags if(tag not in entry._body)]
tags_not_in_body = [
tag for tag in old_entry._tags if (tag not in entry._body)
]
if tags_not_in_body:
entry._tags.extend(tags_not_in_body.sort())
self._update_old_entry(old_entry, entry)
Expand Down
4 changes: 2 additions & 2 deletions jrnl/Entry.py → jrnl/journals/Entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

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
Expand Down
7 changes: 4 additions & 3 deletions jrnl/FolderJournal.py → jrnl/journals/FolderJournal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
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]:
Expand All @@ -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):
Expand Down
23 changes: 12 additions & 11 deletions jrnl/Journal.py → jrnl/journals/Journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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
Expand All @@ -17,6 +16,8 @@
from jrnl.path import expand_path
from jrnl.prompt import yesno

from .Entry import Entry


class Tag:
def __init__(self, name, count=0):
Expand Down Expand Up @@ -184,11 +185,11 @@ def _parse(self, journal_txt: str) -> list[Entry]:
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:]
Expand Down Expand Up @@ -354,7 +355,7 @@ def new_entry(self, raw: str, date=None, sort: bool = True) -> Entry:
)
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:
Expand Down Expand Up @@ -410,7 +411,7 @@ def _parse(self, journal_txt: str) -> list[Entry]:
else:
starred = False

current_entry = Entry.Entry(
current_entry = Entry(
self, date=new_date, text=line[date_length + 1 :], starred=starred
)
except ValueError:
Expand Down Expand Up @@ -455,21 +456,21 @@ def open_journal(journal_name: str, config: dict, legacy: bool = False) -> Journ
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:
Expand Down
5 changes: 5 additions & 0 deletions jrnl/journals/__init__.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions jrnl/cli.py → jrnl/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

from rich.logging import RichHandler

from jrnl import controller
from jrnl.args import parse_args
from jrnl.exception import JrnlException
from jrnl.jrnl import run
from jrnl.messages import Message
from jrnl.messages import MsgStyle
from jrnl.messages import MsgText
Expand All @@ -32,7 +32,7 @@ def configure_logger(debug: bool = False) -> None:
logging.debug("Logging start")


def cli(manual_args: list[str] | None = None) -> int:
def run(manual_args: list[str] | None = None) -> int:
try:
if manual_args is None:
manual_args = sys.argv[1:]
Expand All @@ -41,7 +41,7 @@ def cli(manual_args: list[str] | None = None) -> int:
configure_logger(args.debug)
logging.debug("Parsed args:\n%s", args)

status_code = run(args)
status_code = controller.run(args)

except JrnlException as e:
status_code = 1
Expand Down
4 changes: 2 additions & 2 deletions jrnl/plugins/dates_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
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):
Expand Down
4 changes: 2 additions & 2 deletions jrnl/plugins/fancy_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
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):
Expand Down
2 changes: 1 addition & 1 deletion jrnl/plugins/jrnl_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from jrnl.output import print_msg

if TYPE_CHECKING:
from jrnl.Journal import Journal
from jrnl.journals import Journal


class JRNLImporter:
Expand Down
4 changes: 2 additions & 2 deletions jrnl/plugins/json_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
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):
Expand Down
4 changes: 2 additions & 2 deletions jrnl/plugins/markdown_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
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):
Expand Down
4 changes: 2 additions & 2 deletions jrnl/plugins/tag_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
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):
Expand Down
4 changes: 2 additions & 2 deletions jrnl/plugins/text_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
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:
Expand Down
2 changes: 1 addition & 1 deletion jrnl/plugins/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Expand Down
4 changes: 2 additions & 2 deletions jrnl/plugins/xml_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
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):
Expand Down
6 changes: 3 additions & 3 deletions jrnl/plugins/yaml_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
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):
Expand All @@ -34,7 +34,7 @@ def export_entry(cls, entry: "Entry", to_multifile: bool = True) -> str:
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"""
Expand Down
Loading

0 comments on commit fff05eb

Please sign in to comment.