From f623ef5be7a00474ba2098ff2d23adfff0370c79 Mon Sep 17 00:00:00 2001 From: hut Date: Thu, 13 May 2010 19:38:44 +0200 Subject: Added ranger.ext.spawn which runs programs and returns their stdout --- ranger/ext/spawn.py | 27 +++++++++++++++++++++++++++ ranger/fsobject/fsobject.py | 5 ++--- 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 ranger/ext/spawn.py diff --git a/ranger/ext/spawn.py b/ranger/ext/spawn.py new file mode 100644 index 00000000..9723c1ed --- /dev/null +++ b/ranger/ext/spawn.py @@ -0,0 +1,27 @@ +# Copyright (C) 2009, 2010 Roman Zimbelmann +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from subprocess import Popen, PIPE +ENCODING = 'utf-8' + +def spawn(*args): + """Runs a program, waits for its termination and returns its stdout""" + if len(args) == 1: + popen_arguments = args[0] + else: + popen_arguments = args + process = Popen(popen_arguments, stdout=PIPE) + stdout, stderr = process.communicate() + return stdout.decode(ENCODING) diff --git a/ranger/fsobject/fsobject.py b/ranger/fsobject/fsobject.py index 3a2f6012..66b726ec 100644 --- a/ranger/fsobject/fsobject.py +++ b/ranger/fsobject/fsobject.py @@ -24,11 +24,11 @@ import os from stat import S_ISLNK, S_ISCHR, S_ISBLK, S_ISSOCK, S_ISFIFO, \ S_ISDIR, S_IXUSR from time import time -from subprocess import Popen, PIPE from os.path import abspath, basename, dirname, realpath from . import BAD_INFO from ranger.shared import MimeTypeAware, FileManagerAware from ranger.ext.shell_escape import shell_escape +from ranger.ext.spawn import spawn from ranger.ext.human_readable import human_readable class FileSystemObject(MimeTypeAware, FileManagerAware): @@ -104,8 +104,7 @@ class FileSystemObject(MimeTypeAware, FileManagerAware): def filetype(self): if self._filetype is None: try: - got = Popen(["file", '-Lb', '--mime-type', self.path], - stdout=PIPE).communicate()[0] + got = spawn(["file", '-Lb', '--mime-type', self.path]) except OSError: self._filetype = '' else: -- cgit 1.4.1-2-gfad0