From 827a598dd870854820fac0f81ed92bda98e07cce Mon Sep 17 00:00:00 2001 From: Peter Schmidbauer Date: Fri, 1 Nov 2019 21:48:04 +0100 Subject: [PATCH] Drop use of codecs package Use builtins.open() instead --- jrnl/Journal.py | 9 ++++----- jrnl/export.py | 5 ++--- jrnl/plugins/jrnl_importer.py | 3 +-- jrnl/plugins/text_exporter.py | 5 ++--- jrnl/upgrade.py | 3 +-- jrnl/util.py | 5 ++--- 6 files changed, 12 insertions(+), 18 deletions(-) diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 9ecc2515..f130823d 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -5,7 +5,6 @@ from . import util from . import time import os import sys -import codecs import re from datetime import datetime import logging @@ -268,15 +267,15 @@ class Journal: class PlainJournal(Journal): @classmethod def _create(cls, filename): - with codecs.open(filename, "a", "utf-8"): + with open(filename, "a", encoding="utf-8"): pass def _load(self, filename): - with codecs.open(filename, "r", "utf-8") as f: + with open(filename, "r", encoding="utf-8") as f: return f.read() def _store(self, filename, text): - with codecs.open(filename, 'w', "utf-8") as f: + with open(filename, 'w', encoding="utf-8") as f: f.write(text) @@ -285,7 +284,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 _load(self, filename): - with codecs.open(filename, "r", "utf-8") as f: + with open(filename, "r", encoding="utf-8") as f: return f.read() def _parse(self, journal_txt): diff --git a/jrnl/export.py b/jrnl/export.py index 399f2db0..1ee4e6ff 100644 --- a/jrnl/export.py +++ b/jrnl/export.py @@ -4,7 +4,6 @@ from .util import ERROR_COLOR, RESET_COLOR from .util import slugify from .plugins.template import Template import os -import codecs class Exporter: @@ -32,7 +31,7 @@ class Exporter: def write_file(self, journal, path): """Exports a journal into a single file.""" try: - with codecs.open(path, "w", "utf-8") as f: + with open(path, "w", encoding="utf-8") as f: f.write(self.export_journal(journal)) return f"[Journal exported to {path}]" except OSError as e: @@ -46,7 +45,7 @@ class Exporter: for entry in journal.entries: try: full_path = os.path.join(path, self.make_filename(entry)) - with codecs.open(full_path, "w", "utf-8") as f: + with open(full_path, "w", encoding="utf-8") as f: f.write(self.export_entry(entry)) except OSError as e: return f"[{ERROR_COLOR}ERROR{RESET_COLOR}: {e.filename} {e.strerror}]" diff --git a/jrnl/plugins/jrnl_importer.py b/jrnl/plugins/jrnl_importer.py index e2145232..83341cd9 100644 --- a/jrnl/plugins/jrnl_importer.py +++ b/jrnl/plugins/jrnl_importer.py @@ -1,7 +1,6 @@ #!/usr/bin/env python # encoding: utf-8 -import codecs import sys from .. import util @@ -16,7 +15,7 @@ class JRNLImporter: old_cnt = len(journal.entries) old_entries = journal.entries if input: - with codecs.open(input, "r", "utf-8") as f: + with open(input, "r", encoding="utf-8") as f: other_journal_txt = f.read() else: try: diff --git a/jrnl/plugins/text_exporter.py b/jrnl/plugins/text_exporter.py index 687de750..ce2e71de 100644 --- a/jrnl/plugins/text_exporter.py +++ b/jrnl/plugins/text_exporter.py @@ -1,7 +1,6 @@ #!/usr/bin/env python # encoding: utf-8 -import codecs from ..util import slugify import os from ..util import ERROR_COLOR, RESET_COLOR @@ -26,7 +25,7 @@ class TextExporter: def write_file(cls, journal, path): """Exports a journal into a single file.""" try: - with codecs.open(path, "w", "utf-8") as f: + with open(path, "w", encoding="utf-8") as f: f.write(cls.export_journal(journal)) return f"[Journal exported to {path}]" except IOError as e: @@ -42,7 +41,7 @@ class TextExporter: for entry in journal.entries: try: full_path = os.path.join(path, cls.make_filename(entry)) - with codecs.open(full_path, "w", "utf-8") as f: + with open(full_path, "w", encoding="utf-8") as f: f.write(cls.export_entry(entry)) except IOError as e: return "[{2}ERROR{3}: {0} {1}]".format(e.filename, e.strerror, ERROR_COLOR, RESET_COLOR) diff --git a/jrnl/upgrade.py b/jrnl/upgrade.py index d1b83809..216987a0 100644 --- a/jrnl/upgrade.py +++ b/jrnl/upgrade.py @@ -6,7 +6,6 @@ from . import util from .EncryptedJournal import EncryptedJournal from .util import UserAbort import os -import codecs def backup(filename, binary=False): @@ -19,7 +18,7 @@ def backup(filename, binary=False): def upgrade_jrnl_if_necessary(config_path): - with codecs.open(config_path, "r", "utf-8") as f: + with open(config_path, "r", encoding="utf-8") as f: config_file = f.read() if not config_file.strip().startswith("{"): return diff --git a/jrnl/util.py b/jrnl/util.py index d2bdf9c4..1290adc8 100644 --- a/jrnl/util.py +++ b/jrnl/util.py @@ -10,7 +10,6 @@ if "win32" in sys.platform: import re import tempfile import subprocess -import codecs import unicodedata import shlex import logging @@ -106,14 +105,14 @@ def scope_config(config, journal_name): def get_text_from_editor(config, template=""): filehandle, tmpfile = tempfile.mkstemp(prefix="jrnl", text=True, suffix=".txt") - with codecs.open(tmpfile, 'w', "utf-8") as f: + with open(tmpfile, 'w', encoding="utf-8") as f: if template: f.write(template) try: subprocess.call(shlex.split(config['editor'], posix="win" not in sys.platform) + [tmpfile]) except AttributeError: subprocess.call(config['editor'] + [tmpfile]) - with codecs.open(tmpfile, "r", "utf-8") as f: + with open(tmpfile, "r", encoding="utf-8") as f: raw = f.read() os.close(filehandle) os.remove(tmpfile)