diff --git a/jrnl/DayOneJournal.py b/jrnl/DayOneJournal.py index 4806bdbf..e65ca8d9 100644 --- a/jrnl/DayOneJournal.py +++ b/jrnl/DayOneJournal.py @@ -1,6 +1,7 @@ # Copyright © 2012-2022 jrnl contributors # License: https://www.gnu.org/licenses/gpl-3.0.html +import contextlib import datetime import fnmatch import os @@ -75,40 +76,23 @@ class DayOne(Journal.Journal): ] """Extended DayOne attributes""" - try: + # just ignore it if the keys don't exist + with contextlib.suppress(KeyError): entry.creator_device_agent = dict_entry["Creator"][ "Device Agent" ] - except: # noqa: E722 - pass - try: - entry.creator_generation_date = dict_entry["Creator"][ - "Generation Date" - ] - except: # noqa: E722 - entry.creator_generation_date = date - try: entry.creator_host_name = dict_entry["Creator"]["Host Name"] - except: # noqa: E722 - pass - try: entry.creator_os_agent = dict_entry["Creator"]["OS Agent"] - except: # noqa: E722 - pass - try: entry.creator_software_agent = dict_entry["Creator"][ "Software Agent" ] - except: # noqa: E722 - pass - try: entry.location = dict_entry["Location"] - except: # noqa: E722 - pass - try: entry.weather = dict_entry["Weather"] - except: # noqa: E722 - pass + + entry.creator_generation_date = dict_entry.get("Creator", {}).get( + "Generation Date", date + ) + self.entries.append(entry) self.sort() return self diff --git a/jrnl/EncryptedJournal.py b/jrnl/EncryptedJournal.py index 8b742d29..74fdbdf8 100644 --- a/jrnl/EncryptedJournal.py +++ b/jrnl/EncryptedJournal.py @@ -2,6 +2,7 @@ # License: https://www.gnu.org/licenses/gpl-3.0.html import base64 +import contextlib import hashlib import logging import os @@ -202,10 +203,9 @@ def set_keychain(journal_name, password): import keyring if password is None: - try: + cm = contextlib.suppress(keyring.errors.KeyringError) + with cm: keyring.delete_password("jrnl", journal_name) - except keyring.errors.KeyringError: - pass else: try: keyring.set_password("jrnl", journal_name, password) diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 2fa1d465..3521fbaf 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -120,10 +120,7 @@ class Journal: def validate_parsing(self): """Confirms that the jrnl is still parsed correctly after being dumped to text.""" new_entries = self._parse(self._to_text()) - for i, entry in enumerate(self.entries): - if entry != new_entries[i]: - return False - return True + return all(entry == new_entries[i] for i, entry in enumerate(self.entries)) @staticmethod def create_file(filename): diff --git a/jrnl/install.py b/jrnl/install.py index a37e51f9..b5ec636e 100644 --- a/jrnl/install.py +++ b/jrnl/install.py @@ -1,6 +1,7 @@ # Copyright © 2012-2022 jrnl contributors # License: https://www.gnu.org/licenses/gpl-3.0.html +import contextlib import glob import logging import os @@ -128,10 +129,8 @@ def install() -> dict: # If the folder doesn't exist, create it path = os.path.split(journal_path)[0] - try: + with contextlib.suppress(OSError): os.makedirs(path) - except OSError: - pass # Encrypt it? encrypt = yesno(Message(MsgText.EncryptJournalQuestion), default=False) diff --git a/jrnl/jrnl.py b/jrnl/jrnl.py index 39c25b3f..c79d46e0 100644 --- a/jrnl/jrnl.py +++ b/jrnl/jrnl.py @@ -240,7 +240,8 @@ def _get_editor_template(config: dict, **kwargs) -> str: template_path = expand_path(config["template"]) try: - template = open(template_path).read() + with open(template_path) as f: + template = f.read() logging.debug("Write mode: template loaded: %s", template) except OSError: logging.error("Write mode: template not loaded") diff --git a/jrnl/messages/__init__.py b/jrnl/messages/__init__.py index 381ee332..6bdd9a11 100644 --- a/jrnl/messages/__init__.py +++ b/jrnl/messages/__init__.py @@ -1,10 +1,10 @@ # Copyright © 2012-2022 jrnl contributors # License: https://www.gnu.org/licenses/gpl-3.0.html -from jrnl.messages.Message import Message -from jrnl.messages.MsgStyle import MsgStyle -from jrnl.messages.MsgText import MsgText +from jrnl.messages import Message +from jrnl.messages import MsgStyle +from jrnl.messages import MsgText -Message = Message -MsgStyle = MsgStyle -MsgText = MsgText +Message = Message.Message +MsgStyle = MsgStyle.MsgStyle +MsgText = MsgText.MsgText diff --git a/jrnl/plugins/markdown_exporter.py b/jrnl/plugins/markdown_exporter.py index 8f0d07b4..8c079c63 100644 --- a/jrnl/plugins/markdown_exporter.py +++ b/jrnl/plugins/markdown_exporter.py @@ -83,11 +83,11 @@ class MarkdownExporter(TextExporter): out = [] year, month = -1, -1 for e in journal.entries: - if not e.date.year == year: + if e.date.year != year: year = e.date.year out.append("# " + str(year)) out.append("") - if not e.date.month == month: + if e.date.month != month: month = e.date.month out.append("## " + e.date.strftime("%B")) out.append("") diff --git a/poetry.lock b/poetry.lock index 9b4d4c65..8c5b6616 100644 --- a/poetry.lock +++ b/poetry.lock @@ -17,6 +17,14 @@ category = "dev" optional = false python-versions = "*" +[[package]] +name = "astor" +version = "0.8.1" +description = "Read/rewrite/write Python ASTs" +category = "dev" +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7" + [[package]] name = "asttokens" version = "2.0.5" @@ -265,6 +273,18 @@ isort = ">=4.3.5,<6" [package.extras] test = ["pytest"] +[[package]] +name = "flake8-simplify" +version = "0.19.3" +description = "flake8 plugin which checks for code that can be simplified" +category = "dev" +optional = false +python-versions = ">=3.6.1" + +[package.dependencies] +astor = ">=0.1" +flake8 = ">=3.7" + [[package]] name = "flake8-type-checking" version = "2.2.0" @@ -1178,7 +1198,7 @@ testing = ["func-timeout", "jaraco.itertools", "pytest (>=6)", "pytest-black (>= [metadata] lock-version = "1.1" python-versions = ">=3.10.0, <3.13" -content-hash = "63f39baa62c8641eb6329472de340a9f06d9ffea3096a4095e90f98ce2986f91" +content-hash = "d386601320306164cb6332391f68aca47c114da7cad2dfa273d1fcb70824db16" [metadata.files] ansiwrap = [ @@ -1189,6 +1209,10 @@ appnope = [ {file = "appnope-0.1.3-py2.py3-none-any.whl", hash = "sha256:265a455292d0bd8a72453494fa24df5a11eb18373a60c7c0430889f22548605e"}, {file = "appnope-0.1.3.tar.gz", hash = "sha256:02bd91c4de869fbb1e1c50aafc4098827a7a54ab2f39d9dcba6c9547ed920e24"}, ] +astor = [ + {file = "astor-0.8.1-py2.py3-none-any.whl", hash = "sha256:070a54e890cefb5b3739d19f30f5a5ec840ffc9c50ffa7d23cc9fc1a38ebbfc5"}, + {file = "astor-0.8.1.tar.gz", hash = "sha256:6a6effda93f4e1ce9f618779b2dd1d9d84f1e32812c23a29b3fff6fd7f63fa5e"}, +] asttokens = [ {file = "asttokens-2.0.5-py2.py3-none-any.whl", hash = "sha256:0844691e88552595a6f4a4281a9f7f79b8dd45ca4ccea82e5e05b4bbdb76705c"}, {file = "asttokens-2.0.5.tar.gz", hash = "sha256:9a54c114f02c7a9480d56550932546a3f1fe71d8a02f1bc7ccd0ee3ee35cf4d5"}, @@ -1379,6 +1403,10 @@ flake8-isort = [ {file = "flake8-isort-5.0.0.tar.gz", hash = "sha256:e336f928c7edc509684930ab124414194b7f4e237c712af8fcbdf49d8747b10c"}, {file = "flake8_isort-5.0.0-py3-none-any.whl", hash = "sha256:c73f9cbd1bf209887f602a27b827164ccfeba1676801b2aa23cb49051a1be79c"}, ] +flake8-simplify = [ + {file = "flake8_simplify-0.19.3-py3-none-any.whl", hash = "sha256:1057320e9312d75849541fee822900d27bcad05b2405edc84713affee635629e"}, + {file = "flake8_simplify-0.19.3.tar.gz", hash = "sha256:2fb083bf5142a98d9c9554755cf2f56f8926eb4a33eae30c0809041b1546879e"}, +] flake8-type-checking = [ {file = "flake8_type_checking-2.2.0-py3-none-any.whl", hash = "sha256:c7d9d7adc6cd635a5a1a7859e5e0140f4f8f1705982a22db45872dd9acd49753"}, {file = "flake8_type_checking-2.2.0.tar.gz", hash = "sha256:f7972fc9102f3f632ace1f4b1c5c20b900b8b7b529f04bb6c1fe0a11801e9658"}, diff --git a/pyproject.toml b/pyproject.toml index e3c5592f..ecd5f015 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,6 +48,7 @@ flakeheaven = ">=3.0" flake8-black = ">=0.3.3" flake8-isort = ">=5.0.0" flake8-type-checking = ">=2.2.0" +flake8-simplify = ">=0.19" ipdb = "*" isort = ">=5.10" mkdocs = ">=1.0,<1.3" diff --git a/tests/unit/test_parse_args.py b/tests/unit/test_parse_args.py index 441f06e7..a420daa9 100644 --- a/tests/unit/test_parse_args.py +++ b/tests/unit/test_parse_args.py @@ -293,7 +293,7 @@ class TestDeserialization: runtime_config = make_yaml_valid_dict(input_str) assert runtime_config.__class__ == dict - assert input_str[0] in runtime_config.keys() + assert input_str[0] in runtime_config assert runtime_config[input_str[0]] == input_str[1] def test_deserialize_multiple_datatypes(self):