mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-12 09:28:31 +02:00
Add remaining type hints
This commit is contained in:
parent
c87af82366
commit
029ba446d8
11 changed files with 52 additions and 29 deletions
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 = {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 = ""
|
||||
|
|
|
@ -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."""
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Add table
Reference in a new issue