diff options
author | toonn <toonn@toonn.io> | 2021-09-01 19:09:54 +0200 |
---|---|---|
committer | toonn <toonn@toonn.io> | 2021-09-01 19:09:54 +0200 |
commit | 6e3dc3ab39a302acaea211ebafed601764c47ffb (patch) | |
tree | 9df1319b1954be72375d70d719d9fd6a6f6931cd | |
parent | 50f8971a6eccb43442bb53186fa83212d0763c01 (diff) | |
parent | 8ff8a5984fd000179189e7a3f5b8d7d372ed16d4 (diff) | |
download | ranger-6e3dc3ab39a302acaea211ebafed601764c47ffb.tar.gz |
Merge branch 'drop-open23'
-rwxr-xr-x | ranger/config/commands.py | 15 | ||||
-rw-r--r-- | ranger/container/bookmarks.py | 19 | ||||
-rw-r--r-- | ranger/container/tags.py | 10 | ||||
-rw-r--r-- | ranger/core/actions.py | 22 | ||||
-rw-r--r-- | ranger/core/fm.py | 10 | ||||
-rw-r--r-- | ranger/core/loader.py | 14 | ||||
-rw-r--r-- | ranger/core/main.py | 16 | ||||
-rw-r--r-- | ranger/core/metadata.py | 7 | ||||
-rw-r--r-- | ranger/core/runner.py | 7 | ||||
-rw-r--r-- | ranger/ext/open23.py | 50 | ||||
-rw-r--r-- | ranger/ext/popen_forked.py | 7 | ||||
-rw-r--r-- | ranger/ext/spawn.py | 4 | ||||
-rw-r--r-- | ranger/ext/vcs/vcs.py | 4 | ||||
-rw-r--r-- | ranger/gui/colorscheme.py | 4 | ||||
-rw-r--r-- | ranger/gui/widgets/console.py | 6 |
15 files changed, 82 insertions, 113 deletions
diff --git a/ranger/config/commands.py b/ranger/config/commands.py index 327be5d1..d1d84e91 100755 --- a/ranger/config/commands.py +++ b/ranger/config/commands.py @@ -94,6 +94,7 @@ from __future__ import (absolute_import, division, print_function) from collections import deque import os import re +from io import open from ranger import PY3 from ranger.api.commands import Command @@ -881,13 +882,12 @@ class load_copy_buffer(Command): copy_buffer_filename = 'copy_buffer' def execute(self): - from ranger.container.file import File - from ranger.ext.open23 import open23 from os.path import exists + from ranger.container.file import File fname = self.fm.datapath(self.copy_buffer_filename) unreadable = OSError if PY3 else IOError try: - with open23(fname, "r") as fobj: + with open(fname, "r", encoding="utf-8") as fobj: self.fm.copy_buffer = set( File(g) for g in fobj.read().split("\n") if exists(g) ) @@ -907,12 +907,11 @@ class save_copy_buffer(Command): copy_buffer_filename = 'copy_buffer' def execute(self): - from ranger.ext.open23 import open23 fname = None fname = self.fm.datapath(self.copy_buffer_filename) unwritable = OSError if PY3 else IOError try: - with open23(fname, "w") as fobj: + with open(fname, "w", encoding="utf-8") as fobj: fobj.write("\n".join(fobj.path for fobj in self.fm.copy_buffer)) except unwritable: return self.fm.notify("Cannot open %s" % @@ -958,14 +957,13 @@ class touch(Command): def execute(self): from os.path import join, expanduser, lexists, dirname from os import makedirs - from ranger.ext.open23 import open23 fname = join(self.fm.thisdir.path, expanduser(self.rest(1))) dirname = dirname(fname) if not lexists(fname): if not lexists(dirname): makedirs(dirname) - with open23(fname, 'a'): + with open(fname, 'a', encoding="utf-8"): pass # Just create the file else: self.fm.notify("file/directory exists!", bad=True) @@ -1169,7 +1167,6 @@ class bulkrename(Command): import tempfile from ranger.container.file import File from ranger.ext.shell_escape import shell_escape as esc - from ranger.ext.open23 import open23 # Create and edit the file list filenames = [f.relative_path for f in self.fm.thistab.get_selection()] @@ -1181,7 +1178,7 @@ class bulkrename(Command): else: listfile.write("\n".join(filenames)) self.fm.execute_file([File(listpath)], app='editor') - with open23( + with open( listpath, "r", encoding="utf-8", errors="surrogateescape" ) as listfile: new_filenames = listfile.read().split("\n") diff --git a/ranger/container/bookmarks.py b/ranger/container/bookmarks.py index 4407477e..293bb01a 100644 --- a/ranger/container/bookmarks.py +++ b/ranger/container/bookmarks.py @@ -6,9 +6,10 @@ from __future__ import (absolute_import, division, print_function) import string import re import os +from io import open +from ranger import PY3 from ranger.core.shared import FileManagerAware -from ranger.ext.open23 import open23 ALLOWED_KEYS = string.ascii_letters + string.digits + "`'" @@ -169,18 +170,22 @@ class Bookmarks(FileManagerAware): def save(self): """Save the bookmarks to the bookmarkfile. - This is done automatically after every modification if autosave is True.""" + This is done automatically after every modification if autosave is True. + """ self.update() if self.path is None: return path_new = self.path + '.new' try: - with open23(path_new, 'w') as fobj: + with open(path_new, 'w', encoding="utf-8") as fobj: for key, value in self.dct.items(): - if isinstance(key, str) and key in ALLOWED_KEYS \ + if key in ALLOWED_KEYS \ and key not in self.nonpersistent_bookmarks: - fobj.write("{0}:{1}\n".format(str(key), str(value))) + key_value = "{0}:{1}\n".format(key, value) + if not PY3 and isinstance(key_value, str): + key_value = key_value.decode("utf-8") + fobj.write(key_value) except OSError as ex: self.fm.notify('Bookmarks error: {0}'.format(str(ex)), bad=True) return @@ -218,14 +223,14 @@ class Bookmarks(FileManagerAware): if not os.path.exists(self.path): try: - with open23(self.path, 'w') as fobj: + with open(self.path, 'w', encoding="utf-8") as fobj: pass except OSError as ex: self.fm.notify('Bookmarks error: {0}'.format(str(ex)), bad=True) return None try: - with open23(self.path, 'r') as fobj: + with open(self.path, 'r', encoding="utf-8") as fobj: dct = {} for line in fobj: if self.load_pattern.match(line): diff --git a/ranger/container/tags.py b/ranger/container/tags.py index a8bd50b4..4d4e6c59 100644 --- a/ranger/container/tags.py +++ b/ranger/container/tags.py @@ -5,11 +5,11 @@ from __future__ import (absolute_import, division, print_function) -from os.path import exists, abspath, realpath, expanduser, sep import string +from io import open +from os.path import exists, abspath, realpath, expanduser, sep from ranger.core.shared import FileManagerAware -from ranger.ext.open23 import open23 ALLOWED_KEYS = string.ascii_letters + string.digits + string.punctuation @@ -75,7 +75,9 @@ class Tags(FileManagerAware): def sync(self): try: - with open23(self._filename, "r", errors="replace") as fobj: + with open( + self._filename, "r", encoding="utf-8", errors="replace" + ) as fobj: self.tags = self._parse(fobj) except (OSError, IOError) as err: if exists(self._filename): @@ -85,7 +87,7 @@ class Tags(FileManagerAware): def dump(self): try: - with open23(self._filename, 'w') as fobj: + with open(self._filename, 'w', encoding="utf-8") as fobj: self._compile(fobj) except OSError as err: self.fm.notify(err, bad=True) diff --git a/ranger/core/actions.py b/ranger/core/actions.py index 91a482e3..a552e328 100644 --- a/ranger/core/actions.py +++ b/ranger/core/actions.py @@ -7,17 +7,18 @@ from __future__ import (absolute_import, division, print_function) import codecs import os -from os import link, symlink, listdir, stat -from os.path import join, isdir, realpath, exists import re import shlex import shutil import string import tempfile -from inspect import cleandoc -from stat import S_IEXEC from hashlib import sha512 +from inspect import cleandoc +from io import open from logging import getLogger +from os import link, symlink, listdir, stat +from os.path import join, isdir, realpath, exists +from stat import S_IEXEC import ranger from ranger import PY3 @@ -32,7 +33,6 @@ from ranger.ext.get_executables import get_executables from ranger.ext.keybinding_parser import key_to_string, construct_keybinding from ranger.ext.macrodict import MacroDict, MACRO_FAIL, macro_val from ranger.ext.next_available_filename import next_available_filename -from ranger.ext.open23 import open23 from ranger.ext.relative_symlink import relative_symlink from ranger.ext.rifle import squash_flags, ASK_COMMAND from ranger.ext.safe_path import get_safe_path @@ -373,12 +373,14 @@ class Actions( # pylint: disable=too-many-instance-attributes,too-many-public-m filename = os.path.expanduser(filename) LOG.debug("Sourcing config file '%s'", filename) # pylint: disable=unspecified-encoding - with open(filename, 'r') as fobj: + with open(filename, 'r', encoding="utf-8") as fobj: for line in fobj: line = line.strip(" \r\n") if line.startswith("#") or not line.strip(): continue try: + if not isinstance(line, str): + line = line.encode("ascii") self.execute_console(line) except Exception as ex: # pylint: disable=broad-except if ranger.args.debug: @@ -403,7 +405,9 @@ class Actions( # pylint: disable=too-many-instance-attributes,too-many-public-m # ranger can act as a file chooser when running with --choosefile=... if mode == 0 and 'label' not in kw: if ranger.args.choosefile: - with open23(ranger.args.choosefile, 'w') as fobj: + with open( + ranger.args.choosefile, 'w', encoding="utf-8" + ) as fobj: fobj.write(self.fm.thisfile.path) if ranger.args.choosefiles: @@ -414,7 +418,9 @@ class Actions( # pylint: disable=too-many-instance-attributes,too-many-public-m paths += [fobj.path] paths += [f.path for f in self.fm.thistab.get_selection() if f.path not in paths] - with open23(ranger.args.choosefiles, 'w') as fobj: + with open( + ranger.args.choosefiles, 'w', encoding="utf-8" + ) as fobj: fobj.write('\n'.join(paths) + '\n') if ranger.args.choosefile or ranger.args.choosefiles: diff --git a/ranger/core/fm.py b/ranger/core/fm.py index a9d5df7e..5b221648 100644 --- a/ranger/core/fm.py +++ b/ranger/core/fm.py @@ -5,14 +5,15 @@ from __future__ import (absolute_import, division, print_function) -from time import time -from collections import deque import mimetypes import os.path import pwd import socket import stat import sys +from collections import deque +from io import open +from time import time import ranger.api from ranger.container import settings @@ -26,7 +27,6 @@ from ranger.core.runner import Runner from ranger.core.tab import Tab from ranger.ext import logutils from ranger.ext.img_display import get_image_displayer -from ranger.ext.open23 import open23 from ranger.ext.rifle import Rifle from ranger.ext.signals import SignalDispatcher from ranger.gui.ui import UI @@ -441,14 +441,14 @@ class FM(Actions, # pylint: disable=too-many-instance-attributes if ranger.args.choosedir and self.thisdir and self.thisdir.path: # XXX: UnicodeEncodeError: 'utf-8' codec can't encode character # '\udcf6' in position 42: surrogates not allowed - with open23(ranger.args.choosedir, 'w') as fobj: + with open(ranger.args.choosedir, 'w', encoding="utf-8") as fobj: fobj.write(self.thisdir.path) self.bookmarks.remember(self.thisdir) self.bookmarks.save() # Save tabs if not ranger.args.clean and self.settings.save_tabs_on_exit and len(self.tabs) > 1: - with open23(self.datapath('tabs'), 'a') as fobj: + with open(self.datapath('tabs'), 'a', encoding="utf-8") as fobj: # Don't save active tab since launching ranger changes the active tab fobj.write('\0'.join(v.path for t, v in self.tabs.items()) + '\0\0') diff --git a/ranger/core/loader.py b/ranger/core/loader.py index 1ecfb2eb..5c9e28a5 100644 --- a/ranger/core/loader.py +++ b/ranger/core/loader.py @@ -3,13 +3,14 @@ from __future__ import (absolute_import, division, print_function) -from collections import deque -from subprocess import Popen, PIPE -from time import time, sleep +import errno import math import os.path import select -import errno +from collections import deque +from io import open +from subprocess import Popen, PIPE +from time import time, sleep try: import chardet # pylint: disable=import-error @@ -20,7 +21,6 @@ except ImportError: from ranger import PY3 from ranger.core.shared import FileManagerAware from ranger.ext.human_readable import human_readable -from ranger.ext.open23 import open23 from ranger.ext.safe_path import get_safe_path from ranger.ext.signals import SignalDispatcher @@ -178,7 +178,9 @@ class CommandLoader( # pylint: disable=too-many-instance-attributes # pylint: disable=consider-using-with popenargs = {} if self.popenArgs is None else self.popenArgs popenargs['stdout'] = popenargs['stderr'] = PIPE - popenargs['stdin'] = PIPE if self.input else open23(os.devnull, 'r') + popenargs['stdin'] = ( + PIPE if self.input else open(os.devnull, 'r', encoding="utf-8") + ) self.process = process = Popen(self.args, **popenargs) self.signal_emit('before', process=process, loader=self) if self.input: diff --git a/ranger/core/main.py b/ranger/core/main.py index d55f871b..2556b22a 100644 --- a/ranger/core/main.py +++ b/ranger/core/main.py @@ -5,16 +5,16 @@ from __future__ import (absolute_import, division, print_function) -from logging import getLogger import atexit import locale import os.path import shutil import sys import tempfile +from io import open +from logging import getLogger from ranger import VERSION -from ranger.ext.open23 import open23 LOG = getLogger(__name__) @@ -75,7 +75,9 @@ def main( return 1 fm = FM() try: - with open23(fm.datapath('tagged'), 'r', errors='replace') as fobj: + with open( + fm.datapath('tagged'), 'r', encoding="utf-8", errors='replace' + ) as fobj: lines = fobj.readlines() except OSError as ex: print('Unable to open `tagged` data file: {0}'.format(ex), file=sys.stderr) @@ -156,11 +158,11 @@ def main( tabs_datapath = fm.datapath('tabs') if fm.settings.save_tabs_on_exit and os.path.exists(tabs_datapath) and not args.paths: try: - with open23(tabs_datapath, 'r') as fobj: + with open(tabs_datapath, 'r', encoding="utf-8") as fobj: tabs_saved = fobj.read().partition('\0\0') fm.start_paths += tabs_saved[0].split('\0') if tabs_saved[-1]: - with open23(tabs_datapath, 'w') as fobj: + with open(tabs_datapath, 'w', encoding="utf-8") as fobj: fobj.write(tabs_saved[-1]) else: os.remove(tabs_datapath) @@ -443,7 +445,9 @@ def load_settings( # pylint: disable=too-many-locals,too-many-branches,too-many if not os.path.exists(fm.confpath('plugins', '__init__.py')): LOG.debug("Creating missing '__init__.py' file in plugin folder") - with open23(fm.confpath('plugins', '__init__.py'), 'w'): + with open( + fm.confpath('plugins', '__init__.py'), 'w', encoding="utf-8" + ): # Create the file if it doesn't exist. pass diff --git a/ranger/core/metadata.py b/ranger/core/metadata.py index 833d6e3e..534c083c 100644 --- a/ranger/core/metadata.py +++ b/ranger/core/metadata.py @@ -14,8 +14,9 @@ The database is contained in a local .metadata.json file. from __future__ import (absolute_import, division, print_function) import copy +from io import open from os.path import join, dirname, exists, basename -from ranger.ext.open23 import open23 + from ranger.ext.openstruct import DefaultOpenStruct as ostruct @@ -85,7 +86,7 @@ class MetadataManager(object): self.metadata_cache[filename] = entry self.metafile_cache[metafile] = entries - with open23(metafile, "w") as fobj: + with open(metafile, "w", encoding="utf-8") as fobj: json.dump(entries, fobj, check_circular=True, indent=2) def _get_entry(self, filename): @@ -118,7 +119,7 @@ class MetadataManager(object): return self.metafile_cache[metafile] if exists(metafile): - with open23(metafile, "r") as fobj: + with open(metafile, "r", encoding="utf-8") as fobj: try: entries = json.load(fobj) except ValueError: diff --git a/ranger/core/runner.py b/ranger/core/runner.py index f785448a..1d2b91f7 100644 --- a/ranger/core/runner.py +++ b/ranger/core/runner.py @@ -27,9 +27,10 @@ from __future__ import (absolute_import, division, print_function) import logging import os import sys +from io import open from subprocess import Popen, PIPE, STDOUT + from ranger.ext.get_executables import get_executables, get_term -from ranger.ext.open23 import open23 from ranger.ext.popen_forked import Popen_forked @@ -193,8 +194,8 @@ class Runner(object): # pylint: disable=too-few-public-methods if 's' in context.flags: # Using a with-statement for these is inconvenient. # pylint: disable=consider-using-with - devnull_writable = open23(os.devnull, 'w') - devnull_readable = open23(os.devnull, 'r') + devnull_writable = open(os.devnull, 'w', encoding="utf-8") + devnull_readable = open(os.devnull, 'r', encoding="utf-8") for key in ('stdout', 'stderr'): popen_kws[key] = devnull_writable toggle_ui = False diff --git a/ranger/ext/open23.py b/ranger/ext/open23.py deleted file mode 100644 index 912f9829..00000000 --- a/ranger/ext/open23.py +++ /dev/null @@ -1,50 +0,0 @@ -# This file is part of ranger, the console file manager. -# License: GNU GPL version 3, see the file "AUTHORS" for details. - -from __future__ import absolute_import - -from contextlib import contextmanager - -from ranger import PY3 - - -# COMPAT: We use the pattern of opening a file differently depending on the -# python version in multiple places. Having calls to open in multiple -# branches makes it impossible to use a with-statement instead. This -# contextmanager hides away the lack of an errors keyword argument for -# python 2 and is now preferred. This can be safely dropped once we -# ditch python 2 support. -# TODO: The unspecified-encoding lint should only be disabled for the Python 2 -# case but Pylint is failing to parse the second disable properly. -# pylint: disable=too-many-arguments,unspecified-encoding -@contextmanager -def open23( - file, - mode="r", - buffering=-1, - encoding="UTF-8", - errors=None, - newline=None, - closefd=True, - opener=None, -): - if PY3: - fobj = open( - file=file, - mode=mode, - buffering=buffering, - encoding=encoding, - errors=errors, - newline=newline, - closefd=closefd, - opener=opener, - ) - else: - if buffering is None: - fobj = open(name=file, mode=mode) - else: - fobj = open(name=file, mode=mode, buffering=buffering) - try: - yield fobj - finally: - fobj.close() diff --git a/ranger/ext/popen_forked.py b/ranger/ext/popen_forked.py index 238e9a64..40a5b833 100644 --- a/ranger/ext/popen_forked.py +++ b/ranger/ext/popen_forked.py @@ -4,10 +4,9 @@ from __future__ import (absolute_import, division, print_function) import os +from io import open from subprocess import Popen -from ranger.ext.open23 import open23 - def Popen_forked(*args, **kwargs): # pylint: disable=invalid-name """Forks process and runs Popen with the given args and kwargs. @@ -20,7 +19,9 @@ def Popen_forked(*args, **kwargs): # pylint: disable=invalid-name return False if pid == 0: os.setsid() - with open23(os.devnull, 'r') as null_r, open23(os.devnull, 'w') as null_w: + with open(os.devnull, 'r', encoding="utf-8") as null_r, open( + os.devnull, 'w', encoding="utf-8" + ) as null_w: kwargs['stdin'] = null_r kwargs['stdout'] = kwargs['stderr'] = null_w Popen(*args, **kwargs) # pylint: disable=consider-using-with diff --git a/ranger/ext/spawn.py b/ranger/ext/spawn.py index 39d354d7..4bd0b499 100644 --- a/ranger/ext/spawn.py +++ b/ranger/ext/spawn.py @@ -3,10 +3,10 @@ from __future__ import (absolute_import, division, print_function) +from io import open from os import devnull from subprocess import PIPE, CalledProcessError -from ranger.ext.open23 import open23 from ranger.ext.popen23 import Popen23 ENCODING = 'utf-8' @@ -35,7 +35,7 @@ def check_output(popenargs, **kwargs): with Popen23(popenargs, **kwargs) as process: stdout, _ = process.communicate() else: - with open23(devnull, mode='w') as fd_devnull: + with open(devnull, mode='w', encoding="utf-8") as fd_devnull: with Popen23(popenargs, stderr=fd_devnull, **kwargs) as process: stdout, _ = process.communicate() diff --git a/ranger/ext/vcs/vcs.py b/ranger/ext/vcs/vcs.py index 490aaaf4..5619ed19 100644 --- a/ranger/ext/vcs/vcs.py +++ b/ranger/ext/vcs/vcs.py @@ -9,9 +9,9 @@ import os import subprocess import threading import time +from io import open from ranger.ext import spawn -from ranger.ext.open23 import open23 # Python 2 compatibility try: @@ -130,7 +130,7 @@ class Vcs(object): # pylint: disable=too-many-instance-attributes return output[:-1] return output else: - with open23(os.devnull, mode='w') as fd_devnull: + with open(os.devnull, mode='w', encoding="utf-8") as fd_devnull: subprocess.check_call(cmd, cwd=path, stdout=fd_devnull, stderr=fd_devnull) return None except (subprocess.CalledProcessError, OSError): diff --git a/ranger/gui/colorscheme.py b/ranger/gui/colorscheme.py index cd88ccee..859773fb 100644 --- a/ranger/gui/colorscheme.py +++ b/ranger/gui/colorscheme.py @@ -28,6 +28,7 @@ from __future__ import (absolute_import, division, print_function) import os.path from curses import color_pair +from io import open import ranger from ranger.gui.color import get_color @@ -35,7 +36,6 @@ from ranger.gui.context import Context from ranger.core.main import allow_access_to_confdir from ranger.ext.cached_function import cached_function from ranger.ext.iter_tools import flatten -from ranger.ext.open23 import open23 class ColorSchemeError(Exception): @@ -109,7 +109,7 @@ def _colorscheme_name_to_class(signal): # pylint: disable=too-many-branches if os.path.exists(signal.fm.confpath('colorschemes')): initpy = signal.fm.confpath('colorschemes', '__init__.py') if not os.path.exists(initpy): - with open23(initpy, "a"): + with open(initpy, "a", encoding="utf-8"): # Just create the file pass diff --git a/ranger/gui/widgets/console.py b/ranger/gui/widgets/console.py index a6b90a35..f6d3461f 100644 --- a/ranger/gui/widgets/console.py +++ b/ranger/gui/widgets/console.py @@ -9,11 +9,11 @@ import curses import os import re from collections import deque +from io import open from ranger import PY3 from ranger.gui.widgets import Widget from ranger.ext.direction import Direction -from ranger.ext.open23 import open23 from ranger.ext.widestring import uwid, WideString from ranger.container.history import History, HistoryEmptyException import ranger @@ -45,7 +45,7 @@ class Console(Widget): # pylint: disable=too-many-instance-attributes,too-many- self.historypath = self.fm.datapath('history') if os.path.exists(self.historypath): try: - with open23(self.historypath, "r") as fobj: + with open(self.historypath, "r", encoding="utf-8") as fobj: try: for line in fobj: self.history.add(line[:-1]) @@ -80,7 +80,7 @@ class Console(Widget): # pylint: disable=too-many-instance-attributes,too-many- return if self.historypath: try: - with open23(self.historypath, 'w') as fobj: + with open(self.historypath, 'w', encoding="utf-8") as fobj: for entry in self.history_backup: try: fobj.write(entry + '\n') |