From 9d1f7a09a382169342dd644c59f47465c987eb52 Mon Sep 17 00:00:00 2001 From: hut Date: Mon, 20 Aug 2012 22:09:58 +0200 Subject: ext.rifle: Use python mimetypes module again, file as fallback. file is unreliable, often simple file-extension based recognition yields better results. See: https://github.com/hut/ranger/issues/44 https://bbs.archlinux.org/viewtopic.php?pid=1147719#p1147719 --- ranger/ext/rifle.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/ranger/ext/rifle.py b/ranger/ext/rifle.py index 10cbdd89..157b1efb 100755 --- a/ranger/ext/rifle.py +++ b/ranger/ext/rifle.py @@ -142,6 +142,15 @@ class Rifle(object): self.config_file = config_file self._app_flags = '' self._app_label = None + self._initialized_mimetypes = False + + # get paths for mimetype files + self._mimetype_known_files = [ + os.path.expanduser("~/.mime.types")] + if __file__.endswith("ranger/ext/rifle.py"): + # Add ranger's default mimetypes when run from ranger directory + self._mimetype_known_files.append( + __file__.replace("ext/rifle.py", "data/mime.types")) def reload_config(self, config_file=None): """Replace the current configuration with the one in config_file""" @@ -229,10 +238,18 @@ class Rifle(object): # Spawn "file" to determine the mime-type of the given file. if self._mimetype: return self._mimetype - process = Popen(["file", "--mime-type", "-Lb", fname], - stdout=PIPE, stderr=PIPE) - mimetype, _ = process.communicate() - self._mimetype = mimetype.decode(ENCODING).strip() + + import mimetypes + for path in self._mimetype_known_files: + if path not in mimetypes.knownfiles: + mimetypes.knownfiles.append(path) + self._mimetype, encoding = mimetypes.guess_type(fname) + + if not self._mimetype: + process = Popen(["file", "--mime-type", "-Lb", fname], + stdout=PIPE, stderr=PIPE) + mimetype, _ = process.communicate() + self._mimetype = mimetype.decode(ENCODING).strip() return self._mimetype def _build_command(self, files, action, flags): -- cgit 1.4.1-2-gfad0