From 029ba446d85f5fb68418895155501a87a653f7dc Mon Sep 17 00:00:00 2001 From: outa Date: Wed, 12 Oct 2022 12:51:09 +0200 Subject: [PATCH] Add remaining type hints --- jrnl/plugins/__init__.py | 6 ++++-- jrnl/plugins/dates_exporter.py | 6 ++++-- jrnl/plugins/fancy_exporter.py | 8 +++++--- jrnl/plugins/jrnl_importer.py | 3 ++- jrnl/plugins/json_exporter.py | 8 +++++--- jrnl/plugins/markdown_exporter.py | 6 ++++-- jrnl/plugins/tag_exporter.py | 6 ++++-- jrnl/plugins/text_exporter.py | 16 +++++++++------- jrnl/plugins/util.py | 6 ++++-- jrnl/plugins/xml_exporter.py | 10 +++++++--- jrnl/plugins/yaml_exporter.py | 6 ++++-- 11 files changed, 52 insertions(+), 29 deletions(-) diff --git a/jrnl/plugins/__init__.py b/jrnl/plugins/__init__.py index ebfe5d3f..f67cebaf 100644 --- a/jrnl/plugins/__init__.py +++ b/jrnl/plugins/__init__.py @@ -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 @@ -32,14 +34,14 @@ EXPORT_FORMATS = sorted(__exporter_types.keys()) 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 diff --git a/jrnl/plugins/dates_exporter.py b/jrnl/plugins/dates_exporter.py index a5032f49..778ec06f 100644 --- a/jrnl/plugins/dates_exporter.py +++ b/jrnl/plugins/dates_exporter.py @@ -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 @@ -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: diff --git a/jrnl/plugins/fancy_exporter.py b/jrnl/plugins/fancy_exporter.py index 1eaf74c0..253a0d61 100644 --- a/jrnl/plugins/fancy_exporter.py +++ b/jrnl/plugins/fancy_exporter.py @@ -5,7 +5,9 @@ import logging 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 @@ -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"]) @@ -95,12 +97,12 @@ class FancyExporter(TextExporter): 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( diff --git a/jrnl/plugins/jrnl_importer.py b/jrnl/plugins/jrnl_importer.py index c69d4673..907ece4c 100644 --- a/jrnl/plugins/jrnl_importer.py +++ b/jrnl/plugins/jrnl_importer.py @@ -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 @@ -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) diff --git a/jrnl/plugins/json_exporter.py b/jrnl/plugins/json_exporter.py index ae153b70..dd0a4a4d 100644 --- a/jrnl/plugins/json_exporter.py +++ b/jrnl/plugins/json_exporter.py @@ -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 @@ -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, @@ -49,12 +51,12 @@ class JSONExporter(TextExporter): 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 = { diff --git a/jrnl/plugins/markdown_exporter.py b/jrnl/plugins/markdown_exporter.py index c3476d51..860646ff 100644 --- a/jrnl/plugins/markdown_exporter.py +++ b/jrnl/plugins/markdown_exporter.py @@ -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 @@ -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 "" @@ -73,7 +75,7 @@ class MarkdownExporter(TextExporter): 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 diff --git a/jrnl/plugins/tag_exporter.py b/jrnl/plugins/tag_exporter.py index b9ab45b3..b9d40836 100644 --- a/jrnl/plugins/tag_exporter.py +++ b/jrnl/plugins/tag_exporter.py @@ -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 @@ -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 = "" diff --git a/jrnl/plugins/text_exporter.py b/jrnl/plugins/text_exporter.py index 4451cfef..fe85f002 100644 --- a/jrnl/plugins/text_exporter.py +++ b/jrnl/plugins/text_exporter.py @@ -6,6 +6,8 @@ import os 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 @@ -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: @@ -46,13 +48,13 @@ class TextExporter: 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 @@ -82,7 +84,7 @@ class TextExporter: ) return "" - def _slugify(string): + def _slugify(string: str) -> str: """Slugifies a string. Based on public domain code from https://github.com/zacharyvoase/slugify """ @@ -92,7 +94,7 @@ class TextExporter: 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.""" diff --git a/jrnl/plugins/util.py b/jrnl/plugins/util.py index 55ae73c5..0d6d08ed 100644 --- a/jrnl/plugins/util.py +++ b/jrnl/plugins/util.py @@ -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. @@ -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: diff --git a/jrnl/plugins/xml_exporter.py b/jrnl/plugins/xml_exporter.py index a6c2fe79..c041bbd6 100644 --- a/jrnl/plugins/xml_exporter.py +++ b/jrnl/plugins/xml_exporter.py @@ -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 @@ -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") @@ -29,7 +33,7 @@ class XMLExporter(JSONExporter): 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"): @@ -44,7 +48,7 @@ class XMLExporter(JSONExporter): 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() diff --git a/jrnl/plugins/yaml_exporter.py b/jrnl/plugins/yaml_exporter.py index c97692ad..bd815365 100644 --- a/jrnl/plugins/yaml_exporter.py +++ b/jrnl/plugins/yaml_exporter.py @@ -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 @@ -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)) @@ -124,6 +126,6 @@ class YAMLExporter(TextExporter): ) @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))