Drop use of codecs package

Use builtins.open() instead
This commit is contained in:
Peter Schmidbauer 2019-11-01 21:48:04 +01:00
parent 65adb92ed4
commit 827a598dd8
6 changed files with 12 additions and 18 deletions

View file

@ -5,7 +5,6 @@ from . import util
from . import time from . import time
import os import os
import sys import sys
import codecs
import re import re
from datetime import datetime from datetime import datetime
import logging import logging
@ -268,15 +267,15 @@ class Journal:
class PlainJournal(Journal): class PlainJournal(Journal):
@classmethod @classmethod
def _create(cls, filename): def _create(cls, filename):
with codecs.open(filename, "a", "utf-8"): with open(filename, "a", encoding="utf-8"):
pass pass
def _load(self, filename): 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() return f.read()
def _store(self, filename, text): 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) f.write(text)
@ -285,7 +284,7 @@ class LegacyJournal(Journal):
standard. Main difference here is that in 1.x, timestamps were not cuddled 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.""" by square brackets. You'll not be able to save these journals anymore."""
def _load(self, filename): 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() return f.read()
def _parse(self, journal_txt): def _parse(self, journal_txt):

View file

@ -4,7 +4,6 @@ from .util import ERROR_COLOR, RESET_COLOR
from .util import slugify from .util import slugify
from .plugins.template import Template from .plugins.template import Template
import os import os
import codecs
class Exporter: class Exporter:
@ -32,7 +31,7 @@ class Exporter:
def write_file(self, journal, path): def write_file(self, journal, path):
"""Exports a journal into a single file.""" """Exports a journal into a single file."""
try: 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)) f.write(self.export_journal(journal))
return f"[Journal exported to {path}]" return f"[Journal exported to {path}]"
except OSError as e: except OSError as e:
@ -46,7 +45,7 @@ class Exporter:
for entry in journal.entries: for entry in journal.entries:
try: try:
full_path = os.path.join(path, self.make_filename(entry)) 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)) f.write(self.export_entry(entry))
except OSError as e: except OSError as e:
return f"[{ERROR_COLOR}ERROR{RESET_COLOR}: {e.filename} {e.strerror}]" return f"[{ERROR_COLOR}ERROR{RESET_COLOR}: {e.filename} {e.strerror}]"

View file

@ -1,7 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
import codecs
import sys import sys
from .. import util from .. import util
@ -16,7 +15,7 @@ class JRNLImporter:
old_cnt = len(journal.entries) old_cnt = len(journal.entries)
old_entries = journal.entries old_entries = journal.entries
if input: 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() other_journal_txt = f.read()
else: else:
try: try:

View file

@ -1,7 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
import codecs
from ..util import slugify from ..util import slugify
import os import os
from ..util import ERROR_COLOR, RESET_COLOR from ..util import ERROR_COLOR, RESET_COLOR
@ -26,7 +25,7 @@ class TextExporter:
def write_file(cls, journal, path): def write_file(cls, journal, path):
"""Exports a journal into a single file.""" """Exports a journal into a single file."""
try: 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)) f.write(cls.export_journal(journal))
return f"[Journal exported to {path}]" return f"[Journal exported to {path}]"
except IOError as e: except IOError as e:
@ -42,7 +41,7 @@ class TextExporter:
for entry in journal.entries: for entry in journal.entries:
try: try:
full_path = os.path.join(path, cls.make_filename(entry)) 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)) f.write(cls.export_entry(entry))
except IOError as e: except IOError as e:
return "[{2}ERROR{3}: {0} {1}]".format(e.filename, e.strerror, ERROR_COLOR, RESET_COLOR) return "[{2}ERROR{3}: {0} {1}]".format(e.filename, e.strerror, ERROR_COLOR, RESET_COLOR)

View file

@ -6,7 +6,6 @@ from . import util
from .EncryptedJournal import EncryptedJournal from .EncryptedJournal import EncryptedJournal
from .util import UserAbort from .util import UserAbort
import os import os
import codecs
def backup(filename, binary=False): def backup(filename, binary=False):
@ -19,7 +18,7 @@ def backup(filename, binary=False):
def upgrade_jrnl_if_necessary(config_path): 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() config_file = f.read()
if not config_file.strip().startswith("{"): if not config_file.strip().startswith("{"):
return return

View file

@ -10,7 +10,6 @@ if "win32" in sys.platform:
import re import re
import tempfile import tempfile
import subprocess import subprocess
import codecs
import unicodedata import unicodedata
import shlex import shlex
import logging import logging
@ -106,14 +105,14 @@ def scope_config(config, journal_name):
def get_text_from_editor(config, template=""): def get_text_from_editor(config, template=""):
filehandle, tmpfile = tempfile.mkstemp(prefix="jrnl", text=True, suffix=".txt") 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: if template:
f.write(template) f.write(template)
try: try:
subprocess.call(shlex.split(config['editor'], posix="win" not in sys.platform) + [tmpfile]) subprocess.call(shlex.split(config['editor'], posix="win" not in sys.platform) + [tmpfile])
except AttributeError: except AttributeError:
subprocess.call(config['editor'] + [tmpfile]) 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() raw = f.read()
os.close(filehandle) os.close(filehandle)
os.remove(tmpfile) os.remove(tmpfile)