summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2012-08-20 22:09:58 +0200
committerhut <hut@lavabit.com>2012-08-20 22:09:58 +0200
commit9d1f7a09a382169342dd644c59f47465c987eb52 (patch)
tree8dfcdd7f09eaf9c396e784a7e0ce1908e414f8fa /ranger
parente8db9e7da48f266e4860924196b7a47860612251 (diff)
downloadranger-9d1f7a09a382169342dd644c59f47465c987eb52.tar.gz
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
Diffstat (limited to 'ranger')
-rwxr-xr-xranger/ext/rifle.py25
1 files 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):