diff options
author | toonn <toonn@toonn.io> | 2021-07-17 22:50:56 +0200 |
---|---|---|
committer | toonn <toonn@toonn.io> | 2021-07-20 23:17:45 +0200 |
commit | 6db53d9a8a6c9d30b63330bd9edb2f749c8c02f7 (patch) | |
tree | bf93659b03de0a4e463e6bdc32ad3a2f3a4861ea /ranger | |
parent | 33dc0952e7bc366b70eb989dc7d540760009e1a7 (diff) | |
download | ranger-6db53d9a8a6c9d30b63330bd9edb2f749c8c02f7.tar.gz |
config.commands: Refactor to use with
This required adding another argument to `open23` so I just implemented all the arguments the open builtin has.
Diffstat (limited to 'ranger')
-rwxr-xr-x | ranger/config/commands.py | 29 | ||||
-rw-r--r-- | ranger/ext/open23.py | 29 |
2 files changed, 42 insertions, 16 deletions
diff --git a/ranger/config/commands.py b/ranger/config/commands.py index 484dc0ad..7978111b 100755 --- a/ranger/config/commands.py +++ b/ranger/config/commands.py @@ -886,14 +886,14 @@ class load_copy_buffer(Command): fname = self.fm.datapath(self.copy_buffer_filename) unreadable = OSError if PY3 else IOError try: - fobj = open(fname, 'r') + with open(fname, "r") as fobj: + self.fm.copy_buffer = set( + File(g) for g in fobj.read().split("\n") if exists(g) + ) except unreadable: return self.fm.notify( "Cannot open %s" % (fname or self.copy_buffer_filename), bad=True) - self.fm.copy_buffer = set(File(g) - for g in fobj.read().split("\n") if exists(g)) - fobj.close() self.fm.ui.redraw_main_column() return None @@ -910,12 +910,11 @@ class save_copy_buffer(Command): fname = self.fm.datapath(self.copy_buffer_filename) unwritable = OSError if PY3 else IOError try: - fobj = open(fname, 'w') + with open(fname, "w") as fobj: + fobj.write("\n".join(fobj.path for fobj in self.fm.copy_buffer)) except unwritable: return self.fm.notify("Cannot open %s" % (fname or self.copy_buffer_filename), bad=True) - fobj.write("\n".join(fobj.path for fobj in self.fm.copy_buffer)) - fobj.close() return None @@ -963,7 +962,8 @@ class touch(Command): if not lexists(fname): if not lexists(dirname): makedirs(dirname) - open(fname, 'a').close() + with open(fname, 'a'): + pass # Just create the file else: self.fm.notify("file/directory exists!", bad=True) @@ -1166,6 +1166,7 @@ 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()] @@ -1177,8 +1178,9 @@ class bulkrename(Command): else: listfile.write("\n".join(filenames)) self.fm.execute_file([File(listpath)], app='editor') - with (open(listpath, 'r', encoding="utf-8", errors="surrogateescape") if - PY3 else open(listpath, 'r')) as listfile: + with open23( + listpath, "r", encoding="utf-8", errors="surrogateescape" + ) as listfile: new_filenames = listfile.read().split("\n") os.unlink(listpath) if all(a == b for a, b in zip(filenames, new_filenames)): @@ -1980,9 +1982,10 @@ class yank(Command): new_clipboard_contents = "\n".join(selection) for command in clipboard_commands: - process = subprocess.Popen(command, universal_newlines=True, - stdin=subprocess.PIPE) - process.communicate(input=new_clipboard_contents) + with subprocess.Popen( + command, universal_newlines=True, stdin=subprocess.PIPE + ) as process: + process.communicate(input=new_clipboard_contents) def get_selection_attr(self, attr): return [getattr(item, attr) for item in diff --git a/ranger/ext/open23.py b/ranger/ext/open23.py index ad83ca78..de60d516 100644 --- a/ranger/ext/open23.py +++ b/ranger/ext/open23.py @@ -7,18 +7,41 @@ 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. +# pylint: disable=too-many-arguments @contextmanager -def open23(file, mode="r", errors=None): +def open23( + file, + mode="r", + buffering=-1, + encoding=None, + errors=None, + newline=None, + closefd=True, + opener=None, +): if PY3: - fobj = open(file, mode, errors) + fobj = open( + file=file, + mode=mode, + buffering=buffering, + encoding=encoding, + errors=errors, + newline=newline, + closefd=closefd, + opener=opener, + ) else: - fobj = open(file, mode) + if buffering is None: + fobj = open(name=file, mode=mode) + else: + fobj = open(name=file, mode=mode, buffering=buffering) try: yield fobj finally: |