diff options
author | toonn <toonn@toonn.io> | 2021-07-15 21:13:48 +0200 |
---|---|---|
committer | toonn <toonn@toonn.io> | 2021-07-15 21:18:30 +0200 |
commit | 63b64ad22d323020e2473e9253ce8464a23e07d9 (patch) | |
tree | 48645ed773fc93c05b65f4671daf4c522dae2e5b | |
parent | 1e29bb4ad3b36bef238c156ffa46b7759fc2e190 (diff) | |
download | ranger-63b64ad22d323020e2473e9253ce8464a23e07d9.tar.gz |
rifle.py: Using with where possible
-rwxr-xr-x | ranger/ext/rifle.py | 61 |
1 files changed, 35 insertions, 26 deletions
diff --git a/ranger/ext/rifle.py b/ranger/ext/rifle.py index af75ac41..29ef3bf4 100755 --- a/ranger/ext/rifle.py +++ b/ranger/ext/rifle.py @@ -80,9 +80,12 @@ except ImportError: return False if pid == 0: os.setsid() - kwargs['stdin'] = open(os.devnull, 'r') - kwargs['stdout'] = kwargs['stderr'] = open(os.devnull, 'w') - Popen(*args, **kwargs) + with open(os.devnull, "r") as null_r, open( + os.devnull, "w" + ) as null_w: + kwargs["stdin"] = null_r + kwargs["stdout"] = kwargs["stderr"] = null_w + Popen(*args, **kwargs) os._exit(0) # pylint: disable=protected-access return True @@ -159,20 +162,19 @@ class Rifle(object): # pylint: disable=too-many-instance-attributes """Replace the current configuration with the one in config_file""" if config_file is None: config_file = self.config_file - fobj = open(config_file, 'r') - self.rules = [] - for line in fobj: - line = line.strip() - if line.startswith('#') or line == '': - continue - if self.delimiter1 not in line: - raise ValueError("Line without delimiter") - tests, command = line.split(self.delimiter1, 1) - tests = tests.split(self.delimiter2) - tests = tuple(tuple(f.strip().split(None, 1)) for f in tests) - command = command.strip() - self.rules.append((command, tests)) - fobj.close() + with open(config_file, "r") as fobj: + self.rules = [] + for line in fobj: + line = line.strip() + if line.startswith('#') or line == '': + continue + if self.delimiter1 not in line: + raise ValueError("Line without delimiter") + tests, command = line.split(self.delimiter1, 1) + tests = tests.split(self.delimiter2) + tests = tuple(tuple(f.strip().split(None, 1)) for f in tests) + command = command.strip() + self.rules.append((command, tests)) def _eval_condition(self, condition, files, label): # Handle the negation of conditions starting with an exclamation mark, @@ -256,14 +258,19 @@ class Rifle(object): # pylint: disable=too-many-instance-attributes self._mimetype, _ = mimetypes.guess_type(fname) if not self._mimetype: - process = Popen(["file", "--mime-type", "-Lb", fname], stdout=PIPE, stderr=PIPE) - mimetype, _ = process.communicate() + with Popen( + ["file", "--mime-type", "-Lb", fname], stdout=PIPE, stderr=PIPE + ) as process: + mimetype, _ = process.communicate() self._mimetype = mimetype.decode(ENCODING).strip() if self._mimetype == 'application/octet-stream': try: - process = Popen(["mimetype", "--output-format", "%m", fname], - stdout=PIPE, stderr=PIPE) - mimetype, _ = process.communicate() + with Popen( + ["mimetype", "--output-format", "%m", fname], + stdout=PIPE, + stderr=PIPE, + ) as process: + mimetype, _ = process.communicate() self._mimetype = mimetype.decode(ENCODING).strip() except OSError: pass @@ -437,8 +444,10 @@ class Rifle(object): # pylint: disable=too-many-instance-attributes if 'f' in flags or 't' in flags: Popen_forked(cmd, env=self.hook_environment(os.environ)) else: - process = Popen(cmd, env=self.hook_environment(os.environ)) - process.wait() + with Popen( + cmd, env=self.hook_environment(os.environ) + ) as process: + process.wait() finally: self.hook_after_executing(command, self._mimetype, self._app_flags) @@ -518,8 +527,8 @@ def main(): # pylint: disable=too-many-locals label = options.p if options.w is not None and not options.l: - process = Popen([options.w] + list(positional)) - process.wait() + with Popen([options.w] + list(positional)) as process: + process.wait() else: # Start up rifle rifle = Rifle(conf_path) |