mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 08:38:32 +02:00
Add more type hints (#1642)
This commit is contained in:
parent
9547411390
commit
a13726d4c5
5 changed files with 44 additions and 43 deletions
|
@ -48,33 +48,33 @@ class Entry:
|
|||
self._tags = list(self._parse_tags())
|
||||
|
||||
@property
|
||||
def title(self):
|
||||
def title(self) -> str:
|
||||
if self._title is None:
|
||||
self._parse_text()
|
||||
return self._title
|
||||
|
||||
@title.setter
|
||||
def title(self, x):
|
||||
def title(self, x: str):
|
||||
self._title = x
|
||||
|
||||
@property
|
||||
def body(self):
|
||||
def body(self) -> str:
|
||||
if self._body is None:
|
||||
self._parse_text()
|
||||
return self._body
|
||||
|
||||
@body.setter
|
||||
def body(self, x):
|
||||
def body(self, x: str):
|
||||
self._body = x
|
||||
|
||||
@property
|
||||
def tags(self):
|
||||
def tags(self) -> list[str]:
|
||||
if self._tags is None:
|
||||
self._parse_text()
|
||||
return self._tags
|
||||
|
||||
@tags.setter
|
||||
def tags(self, x):
|
||||
def tags(self, x: list[str]):
|
||||
self._tags = x
|
||||
|
||||
@staticmethod
|
||||
|
@ -218,7 +218,7 @@ class Entry:
|
|||
return False
|
||||
return True
|
||||
|
||||
def __ne__(self, other):
|
||||
def __ne__(self, other: "Entry"):
|
||||
return not self.__eq__(other)
|
||||
|
||||
|
||||
|
|
|
@ -59,7 +59,7 @@ class Journal:
|
|||
return (entry for entry in self.entries)
|
||||
|
||||
@classmethod
|
||||
def from_journal(cls, other):
|
||||
def from_journal(cls, other: "Journal") -> "Journal":
|
||||
"""Creates a new journal by copying configuration and entries from
|
||||
another journal object"""
|
||||
new_journal = cls(other.name, **other.config)
|
||||
|
@ -72,7 +72,7 @@ class Journal:
|
|||
)
|
||||
return new_journal
|
||||
|
||||
def import_(self, other_journal_txt):
|
||||
def import_(self, other_journal_txt: str) -> None:
|
||||
imported_entries = self._parse(other_journal_txt)
|
||||
for entry in imported_entries:
|
||||
entry.modified = True
|
||||
|
@ -96,7 +96,7 @@ class Journal:
|
|||
|
||||
return self.encryption_method.encrypt(text)
|
||||
|
||||
def open(self, filename=None):
|
||||
def open(self, filename: str | None = None) -> "Journal":
|
||||
"""Opens the journal file defined in the config and parses it into a list of Entries.
|
||||
Entries have the form (date, title, body)."""
|
||||
filename = filename or self.config["journal"]
|
||||
|
@ -130,35 +130,35 @@ class Journal:
|
|||
logging.debug("opened %s with %d entries", self.__class__.__name__, len(self))
|
||||
return self
|
||||
|
||||
def write(self, filename=None):
|
||||
def write(self, filename: str | None = None) -> None:
|
||||
"""Dumps the journal into the config file, overwriting it"""
|
||||
filename = filename or self.config["journal"]
|
||||
text = self._to_text()
|
||||
text = self._encrypt(text)
|
||||
self._store(filename, text)
|
||||
|
||||
def validate_parsing(self):
|
||||
def validate_parsing(self) -> bool:
|
||||
"""Confirms that the jrnl is still parsed correctly after being dumped to text."""
|
||||
new_entries = self._parse(self._to_text())
|
||||
return all(entry == new_entries[i] for i, entry in enumerate(self.entries))
|
||||
|
||||
@staticmethod
|
||||
def create_file(filename):
|
||||
def create_file(filename: str) -> None:
|
||||
with open(filename, "w"):
|
||||
pass
|
||||
|
||||
def _to_text(self):
|
||||
def _to_text(self) -> str:
|
||||
return "\n".join([str(e) for e in self.entries])
|
||||
|
||||
def _load(self, filename):
|
||||
def _load(self, filename: str) -> bytes:
|
||||
with open(filename, "rb") as f:
|
||||
return f.read()
|
||||
|
||||
def _store(self, filename, text):
|
||||
def _store(self, filename: str, text: bytes) -> None:
|
||||
with open(filename, "wb") as f:
|
||||
f.write(text)
|
||||
|
||||
def _parse(self, journal_txt):
|
||||
def _parse(self, journal_txt: str) -> list[Entry]:
|
||||
"""Parses a journal that's stored in a string and returns a list of entries"""
|
||||
|
||||
# Return empty array if the journal is blank
|
||||
|
@ -197,7 +197,7 @@ class Journal:
|
|||
entry._parse_text()
|
||||
return entries
|
||||
|
||||
def pprint(self, short=False):
|
||||
def pprint(self, short: bool = False) -> str:
|
||||
"""Prettyprints the journal's entries"""
|
||||
return "\n".join([e.pprint(short=short) for e in self.entries])
|
||||
|
||||
|
@ -207,17 +207,17 @@ class Journal:
|
|||
def __repr__(self):
|
||||
return f"<Journal with {len(self.entries)} entries>"
|
||||
|
||||
def sort(self):
|
||||
def sort(self) -> None:
|
||||
"""Sorts the Journal's entries by date"""
|
||||
self.entries = sorted(self.entries, key=lambda entry: entry.date)
|
||||
|
||||
def limit(self, n=None):
|
||||
def limit(self, n: int | None = None) -> None:
|
||||
"""Removes all but the last n entries"""
|
||||
if n:
|
||||
self.entries = self.entries[-n:]
|
||||
|
||||
@property
|
||||
def tags(self):
|
||||
def tags(self) -> list[Tag]:
|
||||
"""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.
|
||||
|
@ -228,16 +228,16 @@ class Journal:
|
|||
|
||||
def filter(
|
||||
self,
|
||||
tags=[],
|
||||
month=None,
|
||||
day=None,
|
||||
year=None,
|
||||
start_date=None,
|
||||
end_date=None,
|
||||
starred=False,
|
||||
strict=False,
|
||||
contains=None,
|
||||
exclude=[],
|
||||
tags: list = [],
|
||||
month: str | int | None = None,
|
||||
day: str | int | None = None,
|
||||
year: str | None = None,
|
||||
start_date: str | None = None,
|
||||
end_date: str | None = None,
|
||||
starred: bool = False,
|
||||
strict: bool = False,
|
||||
contains: bool = None,
|
||||
exclude: list = [],
|
||||
):
|
||||
"""Removes all entries from the journal that don't match the filter.
|
||||
|
||||
|
@ -293,19 +293,19 @@ class Journal:
|
|||
|
||||
self.entries = result
|
||||
|
||||
def delete_entries(self, entries_to_delete):
|
||||
def delete_entries(self, entries_to_delete: list[Entry]) -> None:
|
||||
"""Deletes specific entries from a journal."""
|
||||
for entry in entries_to_delete:
|
||||
self.entries.remove(entry)
|
||||
|
||||
def change_date_entries(self, date):
|
||||
def change_date_entries(self, date: datetime.datetime | None) -> None:
|
||||
"""Changes entry dates to given date."""
|
||||
date = time.parse(date)
|
||||
|
||||
for entry in self.entries:
|
||||
entry.date = date
|
||||
|
||||
def prompt_action_entries(self, msg: MsgText):
|
||||
def prompt_action_entries(self, msg: MsgText) -> list[Entry]:
|
||||
"""Prompts for action for each entry in a journal, using given message.
|
||||
Returns the entries the user wishes to apply the action on."""
|
||||
to_act = []
|
||||
|
@ -325,7 +325,7 @@ class Journal:
|
|||
|
||||
return to_act
|
||||
|
||||
def new_entry(self, raw, date=None, sort=True):
|
||||
def new_entry(self, raw: str, date=None, sort: bool = True) -> Entry:
|
||||
"""Constructs a new entry from some raw text input.
|
||||
If a date is given, it will parse and use this, otherwise scan for a date in the input first."""
|
||||
|
||||
|
@ -361,12 +361,12 @@ class Journal:
|
|||
self.sort()
|
||||
return entry
|
||||
|
||||
def editable_str(self):
|
||||
def editable_str(self) -> str:
|
||||
"""Turns the journal into a string of entries that can be edited
|
||||
manually and later be parsed with self.parse_editable_str."""
|
||||
return "\n".join([str(e) for e in self.entries])
|
||||
|
||||
def parse_editable_str(self, edited):
|
||||
def parse_editable_str(self, edited: str) -> None:
|
||||
"""Parses the output of self.editable_str and updates it's entries."""
|
||||
mod_entries = self._parse(edited)
|
||||
# Match those entries that can be found in self.entries and set
|
||||
|
@ -382,7 +382,7 @@ class LegacyJournal(Journal):
|
|||
standard. Main difference here is that in 1.x, timestamps were not cuddled
|
||||
by square brackets. You'll not be able to save these journals anymore."""
|
||||
|
||||
def _parse(self, journal_txt):
|
||||
def _parse(self, journal_txt: str) -> list[Entry]:
|
||||
"""Parses a journal that's stored in a string and returns a list of entries"""
|
||||
# Entries start with a line that looks like 'date title' - let's figure out how
|
||||
# long the date will be by constructing one
|
||||
|
@ -429,7 +429,7 @@ class LegacyJournal(Journal):
|
|||
return entries
|
||||
|
||||
|
||||
def open_journal(journal_name, config, legacy=False):
|
||||
def open_journal(journal_name: str, config: dict, legacy: bool = False) -> Journal:
|
||||
"""
|
||||
Creates a normal, encrypted or DayOne journal based on the passed config.
|
||||
If legacy is True, it will open Journals with legacy classes build for
|
||||
|
|
|
@ -28,7 +28,7 @@ from jrnl.messages import MsgText
|
|||
from jrnl.output import print_msg
|
||||
|
||||
|
||||
def preconfig_diagnostic(_):
|
||||
def preconfig_diagnostic(_) -> None:
|
||||
from jrnl import __title__
|
||||
from jrnl import __version__
|
||||
|
||||
|
@ -39,7 +39,7 @@ def preconfig_diagnostic(_):
|
|||
)
|
||||
|
||||
|
||||
def preconfig_version(_):
|
||||
def preconfig_version(_) -> None:
|
||||
import textwrap
|
||||
|
||||
from jrnl import __title__
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
from enum import Enum
|
||||
from importlib import import_module
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import Type
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .BaseEncryption import BaseEncryption
|
||||
|
@ -18,7 +19,7 @@ class EncryptionMethods(str, Enum):
|
|||
JRNLV2 = "Jrnlv2Encryption"
|
||||
|
||||
|
||||
def determine_encryption_method(config: str | bool) -> "BaseEncryption":
|
||||
def determine_encryption_method(config: str | bool) -> Type["BaseEncryption"]:
|
||||
ENCRYPTION_METHODS = {
|
||||
True: EncryptionMethods.JRNLV2, # the default
|
||||
False: EncryptionMethods.NONE,
|
||||
|
|
|
@ -18,7 +18,7 @@ def get_keyring_password(journal_name: str = "default") -> str | None:
|
|||
return None
|
||||
|
||||
|
||||
def set_keyring_password(password: str, journal_name: str = "default"):
|
||||
def set_keyring_password(password: str, journal_name: str = "default") -> None:
|
||||
try:
|
||||
return keyring.set_password("jrnl", journal_name, password)
|
||||
except keyring.errors.KeyringError as e:
|
||||
|
|
Loading…
Add table
Reference in a new issue