about summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorSamuli Thomasson <samuli.thomasson@paivola.fi>2020-09-08 22:28:54 +0200
committerSamuli Thomasson <samuli.thomasson@paivola.fi>2020-09-08 22:28:54 +0200
commitf8b150f76b1cf06ea4e97e146545c8e3de02d809 (patch)
tree00c0516bd41620639c0ba646bf51ad4388de32e6 /ranger
parent240d8a0ea1b82690372ba067c24a3f581a29c782 (diff)
downloadranger-f8b150f76b1cf06ea4e97e146545c8e3de02d809.tar.gz
Fix mimetypes initialization #1744
Diffstat (limited to 'ranger')
-rw-r--r--ranger/core/filter_stack.py5
-rw-r--r--ranger/core/fm.py6
-rwxr-xr-xranger/ext/rifle.py6
3 files changed, 8 insertions, 9 deletions
diff --git a/ranger/core/filter_stack.py b/ranger/core/filter_stack.py
index 58ce1a1c..66ab95c3 100644
--- a/ranger/core/filter_stack.py
+++ b/ranger/core/filter_stack.py
@@ -6,7 +6,6 @@
 from __future__ import (absolute_import, division, print_function)
 
 import re
-import mimetypes
 # pylint: disable=invalid-name
 try:
     from itertools import izip_longest as zip_longest
@@ -59,13 +58,13 @@ class NameFilter(BaseFilter):
 
 
 @stack_filter("mime")
-class MimeFilter(BaseFilter):
+class MimeFilter(BaseFilter, FileManagerAware):
     def __init__(self, pattern):
         self.pattern = pattern
         self.regex = re.compile(pattern)
 
     def __call__(self, fobj):
-        mimetype, _ = mimetypes.guess_type(fobj.relative_path)
+        mimetype, _ = self.fm.mimetypes.guess_type(fobj.relative_path)
         if mimetype is None:
             return False
         return self.regex.search(mimetype)
diff --git a/ranger/core/fm.py b/ranger/core/fm.py
index da8b27c3..cef9394d 100644
--- a/ranger/core/fm.py
+++ b/ranger/core/fm.py
@@ -73,9 +73,11 @@ class FM(Actions,  # pylint: disable=too-many-instance-attributes
         self.hostname = socket.gethostname()
         self.home_path = os.path.expanduser('~')
 
-        mimetypes.knownfiles.append(os.path.expanduser('~/.mime.types'))
-        mimetypes.knownfiles.append(self.relpath('data/mime.types'))
         self.mimetypes = mimetypes.MimeTypes()
+        extra_files = [self.relpath('data/mime.types'), os.path.expanduser("~/.mime.types")]
+        for path in mimetypes.knownfiles + extra_files:
+            if os.path.isfile(path):
+                self.mimetypes.read(path)
 
     def initialize(self):  # pylint: disable=too-many-statements
         """If ui/bookmarks are None, they will be initialized here."""
diff --git a/ranger/ext/rifle.py b/ranger/ext/rifle.py
index e0773324..af75ac41 100755
--- a/ranger/ext/rifle.py
+++ b/ranger/ext/rifle.py
@@ -144,7 +144,6 @@ class Rifle(object):  # pylint: disable=too-many-instance-attributes
         self.config_file = config_file
         self._app_flags = ''
         self._app_label = None
-        self._initialized_mimetypes = False
         self._mimetype = None
         self._skip = None
         self.rules = None
@@ -252,9 +251,8 @@ class Rifle(object):  # pylint: disable=too-many-instance-attributes
             return self._mimetype
 
         import mimetypes
-        for path in self._mimetype_known_files:
-            if path not in mimetypes.knownfiles:
-                mimetypes.knownfiles.append(path)
+        if not mimetypes.inited:
+            mimetypes.init(mimetypes.knownfiles + self._mimetype_known_files)
         self._mimetype, _ = mimetypes.guess_type(fname)
 
         if not self._mimetype: