Skip to content

Commit

Permalink
Add remaining type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
outa committed Nov 1, 2022
1 parent 7b9b619 commit 8aa6e5d
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 29 deletions.
6 changes: 4 additions & 2 deletions jrnl/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright © 2012-2022 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html

from typing import Type

from jrnl.plugins.dates_exporter import DatesExporter
from jrnl.plugins.fancy_exporter import FancyExporter
from jrnl.plugins.jrnl_importer import JRNLImporter
Expand Down Expand Up @@ -32,14 +34,14 @@
IMPORT_FORMATS = sorted(__importer_types.keys())


def get_exporter(format):
def get_exporter(format: str) -> Type[TextExporter] | None:
for exporter in __exporters:
if hasattr(exporter, "names") and format in exporter.names:
return exporter
return None


def get_importer(format):
def get_importer(format: str) -> Type[JRNLImporter] | None:
for importer in __importers:
if hasattr(importer, "names") and format in importer.names:
return importer
Expand Down
6 changes: 4 additions & 2 deletions jrnl/plugins/dates_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from collections import Counter

from jrnl.Entry import Entry
from jrnl.Journal import Journal
from jrnl.plugins.text_exporter import TextExporter


Expand All @@ -13,11 +15,11 @@ class DatesExporter(TextExporter):
extension = "dates"

@classmethod
def export_entry(cls, entry):
def export_entry(cls, entry: Entry):
raise NotImplementedError

@classmethod
def export_journal(cls, journal):
def export_journal(cls, journal: Journal) -> str:
"""Returns dates and their frequencies for an entire journal."""
date_counts = Counter()
for entry in journal.entries:
Expand Down
8 changes: 5 additions & 3 deletions jrnl/plugins/fancy_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import os
from textwrap import TextWrapper

from jrnl.Entry import Entry
from jrnl.exception import JrnlException
from jrnl.Journal import Journal
from jrnl.messages import Message
from jrnl.messages import MsgStyle
from jrnl.messages import MsgText
Expand Down Expand Up @@ -35,7 +37,7 @@ class FancyExporter(TextExporter):
border_m = "┘"

@classmethod
def export_entry(cls, entry):
def export_entry(cls, entry: Entry) -> str:
"""Returns a fancy unicode representation of a single entry."""
date_str = entry.date.strftime(entry.journal.config["timeformat"])

Expand Down Expand Up @@ -95,12 +97,12 @@ def export_entry(cls, entry):
return "\n".join(card)

@classmethod
def export_journal(cls, journal):
def export_journal(cls, journal) -> str:
"""Returns a unicode representation of an entire journal."""
return "\n".join(cls.export_entry(entry) for entry in journal)


def check_provided_linewrap_viability(linewrap, card, journal):
def check_provided_linewrap_viability(linewrap: int, card: list[str], journal: Journal):
if len(card[0]) > linewrap:
width_violation = len(card[0]) - linewrap
raise JrnlException(
Expand Down
3 changes: 2 additions & 1 deletion jrnl/plugins/jrnl_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys

from jrnl.exception import JrnlException
from jrnl.Journal import Journal
from jrnl.messages import Message
from jrnl.messages import MsgStyle
from jrnl.messages import MsgText
Expand All @@ -16,7 +17,7 @@ class JRNLImporter:
names = ["jrnl"]

@staticmethod
def import_(journal, input=None):
def import_(journal: Journal, input: str | None = None) -> None:
"""Imports from an existing file if input is specified, and
standard input otherwise."""
old_cnt = len(journal.entries)
Expand Down
8 changes: 5 additions & 3 deletions jrnl/plugins/json_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import json

from jrnl.Entry import Entry
from jrnl.Journal import Journal
from jrnl.plugins.text_exporter import TextExporter
from jrnl.plugins.util import get_tags_count

Expand All @@ -14,7 +16,7 @@ class JSONExporter(TextExporter):
extension = "json"

@classmethod
def entry_to_dict(cls, entry):
def entry_to_dict(cls, entry: Entry) -> dict:
entry_dict = {
"title": entry.title,
"body": entry.body,
Expand Down Expand Up @@ -49,12 +51,12 @@ def entry_to_dict(cls, entry):
return entry_dict

@classmethod
def export_entry(cls, entry):
def export_entry(cls, entry: Entry) -> str:
"""Returns a json representation of a single entry."""
return json.dumps(cls.entry_to_dict(entry), indent=2) + "\n"

@classmethod
def export_journal(cls, journal):
def export_journal(cls, journal: Journal) -> str:
"""Returns a json representation of an entire journal."""
tags = get_tags_count(journal)
result = {
Expand Down
6 changes: 4 additions & 2 deletions jrnl/plugins/markdown_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import os
import re

from jrnl.Entry import Entry
from jrnl.Journal import Journal
from jrnl.messages import Message
from jrnl.messages import MsgStyle
from jrnl.messages import MsgText
Expand All @@ -18,7 +20,7 @@ class MarkdownExporter(TextExporter):
extension = "md"

@classmethod
def export_entry(cls, entry, to_multifile=True):
def export_entry(cls, entry: Entry, to_multifile: bool = True) -> str:
"""Returns a markdown representation of a single entry."""
date_str = entry.date.strftime(entry.journal.config["timeformat"])
body_wrapper = "\n" if entry.body else ""
Expand Down Expand Up @@ -73,7 +75,7 @@ def export_entry(cls, entry, to_multifile=True):
return f"{heading} {date_str} {entry.title}\n{newbody} "

@classmethod
def export_journal(cls, journal):
def export_journal(cls, journal: Journal) -> str:
"""Returns a Markdown representation of an entire journal."""
out = []
year, month = -1, -1
Expand Down
6 changes: 4 additions & 2 deletions jrnl/plugins/tag_exporter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright © 2012-2022 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html

from jrnl.Entry import Entry
from jrnl.Journal import Journal
from jrnl.plugins.text_exporter import TextExporter
from jrnl.plugins.util import get_tags_count

Expand All @@ -12,12 +14,12 @@ class TagExporter(TextExporter):
extension = "tags"

@classmethod
def export_entry(cls, entry):
def export_entry(cls, entry: Entry) -> str:
"""Returns a list of tags for a single entry."""
return ", ".join(entry.tags)

@classmethod
def export_journal(cls, journal):
def export_journal(cls, journal: Journal) -> str:
"""Returns a list of tags and their frequency for an entire journal."""
tag_counts = get_tags_count(journal)
result = ""
Expand Down
16 changes: 9 additions & 7 deletions jrnl/plugins/text_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import re
import unicodedata

from jrnl.Entry import Entry
from jrnl.Journal import Journal
from jrnl.messages import Message
from jrnl.messages import MsgStyle
from jrnl.messages import MsgText
Expand All @@ -19,17 +21,17 @@ class TextExporter:
extension = "txt"

@classmethod
def export_entry(cls, entry):
def export_entry(cls, entry: Entry) -> str:
"""Returns a string representation of a single entry."""
return str(entry)

@classmethod
def export_journal(cls, journal):
def export_journal(cls, journal: Journal) -> str:
"""Returns a string representation of an entire journal."""
return "\n".join(cls.export_entry(entry) for entry in journal)

@classmethod
def write_file(cls, journal, path):
def write_file(cls, journal: Journal, path: str) -> str:
"""Exports a journal into a single file."""
export_str = cls.export_journal(journal)
with open(path, "w", encoding="utf-8") as f:
Expand All @@ -46,13 +48,13 @@ def write_file(cls, journal, path):
return ""

@classmethod
def make_filename(cls, entry):
def make_filename(cls, entry: Entry) -> str:
return entry.date.strftime("%Y-%m-%d") + "_{}.{}".format(
cls._slugify(str(entry.title)), cls.extension
)

@classmethod
def write_files(cls, journal, path):
def write_files(cls, journal: Journal, path: str) -> str:
"""Exports a journal into individual files for each entry."""
for entry in journal.entries:
entry_is_written = False
Expand Down Expand Up @@ -82,7 +84,7 @@ def write_files(cls, journal, path):
)
return ""

def _slugify(string):
def _slugify(string: str) -> str:
"""Slugifies a string.
Based on public domain code from https://github.com/zacharyvoase/slugify
"""
Expand All @@ -92,7 +94,7 @@ def _slugify(string):
return slug

@classmethod
def export(cls, journal, output=None):
def export(cls, journal: Journal, output: str | None = None) -> str:
"""Exports to individual files if output is an existing path, or into
a single file if output is a file name, or returns the exporter's
representation as string if output is None."""
Expand Down
6 changes: 4 additions & 2 deletions jrnl/plugins/util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright © 2012-2022 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html

from jrnl.Journal import Journal

def get_tags_count(journal):

def get_tags_count(journal: Journal) -> set[tuple[int, str]]:
"""Returns a set of tuples (count, tag) for all tags present in the journal."""
# Astute reader: should the following line leave you as puzzled as me the first time
# I came across this construction, worry not and embrace the ensuing moment of enlightment.
Expand All @@ -12,7 +14,7 @@ def get_tags_count(journal):
return tag_counts


def oxford_list(lst):
def oxford_list(lst: list) -> str:
"""Return Human-readable list of things obeying the object comma)"""
lst = sorted(lst)
if not lst:
Expand Down
10 changes: 7 additions & 3 deletions jrnl/plugins/xml_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from xml.dom import minidom

from jrnl.Entry import Entry
from jrnl.Journal import Journal
from jrnl.plugins.json_exporter import JSONExporter
from jrnl.plugins.util import get_tags_count

Expand All @@ -14,7 +16,9 @@ class XMLExporter(JSONExporter):
extension = "xml"

@classmethod
def export_entry(cls, entry, doc=None):
def export_entry(
cls, entry: Entry, doc: minidom.Document | None = None
) -> minidom.Element | str:
"""Returns an XML representation of a single entry."""
doc_el = doc or minidom.Document()
entry_el = doc_el.createElement("entry")
Expand All @@ -29,7 +33,7 @@ def export_entry(cls, entry, doc=None):
return entry_el

@classmethod
def entry_to_xml(cls, entry, doc):
def entry_to_xml(cls, entry: Entry, doc: minidom.Document) -> minidom.Element:
entry_el = doc.createElement("entry")
entry_el.setAttribute("date", entry.date.isoformat())
if hasattr(entry, "uuid"):
Expand All @@ -44,7 +48,7 @@ def entry_to_xml(cls, entry, doc):
return entry_el

@classmethod
def export_journal(cls, journal):
def export_journal(cls, journal: Journal) -> str:
"""Returns an XML representation of an entire journal."""
tags = get_tags_count(journal)
doc = minidom.Document()
Expand Down
6 changes: 4 additions & 2 deletions jrnl/plugins/yaml_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import os
import re

from jrnl.Entry import Entry
from jrnl.exception import JrnlException
from jrnl.Journal import Journal
from jrnl.messages import Message
from jrnl.messages import MsgStyle
from jrnl.messages import MsgText
Expand All @@ -19,7 +21,7 @@ class YAMLExporter(TextExporter):
extension = "md"

@classmethod
def export_entry(cls, entry, to_multifile=True):
def export_entry(cls, entry: Entry, to_multifile: bool = True) -> str:
"""Returns a markdown representation of a single entry, with YAML front matter."""
if to_multifile is False:
raise JrnlException(Message(MsgText.YamlMustBeDirectory, MsgStyle.ERROR))
Expand Down Expand Up @@ -124,6 +126,6 @@ def export_entry(cls, entry, to_multifile=True):
)

@classmethod
def export_journal(cls, journal):
def export_journal(cls, journal: Journal):
"""Returns an error, as YAML export requires a directory as a target."""
raise JrnlException(Message(MsgText.YamlMustBeDirectory, MsgStyle.ERROR))

0 comments on commit 8aa6e5d

Please sign in to comment.