Improving MIME type detection for some common file types when they are missing from the system MIME database
Also allow the user to specify their own database via mime.types in the kitty config directory. See #3056
This commit is contained in:
+2
-2
@@ -394,10 +394,10 @@ def complete_files_and_dirs(
|
||||
|
||||
|
||||
def complete_icat_args(ans: Completions, opt: Optional[OptionDict], prefix: str, unknown_args: Delegate) -> None:
|
||||
from mimetypes import guess_type
|
||||
from .guess_mime_type import guess_type
|
||||
|
||||
def icat_file_predicate(filename: str) -> bool:
|
||||
mt = guess_type(filename)[0]
|
||||
mt = guess_type(filename)
|
||||
if mt and mt.startswith('image/'):
|
||||
return True
|
||||
return False
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env python
|
||||
# vim:fileencoding=utf-8
|
||||
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
import os
|
||||
from contextlib import suppress
|
||||
from typing import Optional
|
||||
|
||||
known_extensions = {
|
||||
'asciidoc': 'text/asciidoctor',
|
||||
'conf': 'text/config',
|
||||
'md': 'text/markdown',
|
||||
'pyj': 'text/rapydscript-ng',
|
||||
'recipe': 'text/python',
|
||||
'rst': 'text/restructured-text',
|
||||
'toml': 'text/toml',
|
||||
'vim': 'text/vim',
|
||||
'yaml': 'text/yaml',
|
||||
}
|
||||
|
||||
|
||||
def is_rc_file(path: str) -> bool:
|
||||
name = os.path.basename(path)
|
||||
return '.' not in name and name.endswith('rc')
|
||||
|
||||
|
||||
def guess_type(path: str) -> Optional[str]:
|
||||
if not hasattr(guess_type, 'inited'):
|
||||
setattr(guess_type, 'inited', True)
|
||||
from mimetypes import init
|
||||
init(None)
|
||||
from kitty.constants import config_dir
|
||||
local_defs = os.path.join(config_dir, 'mime.types')
|
||||
if os.path.exists(local_defs):
|
||||
init((local_defs,))
|
||||
from mimetypes import guess_type as stdlib_guess_type
|
||||
mt = None
|
||||
with suppress(Exception):
|
||||
mt = stdlib_guess_type(path)[0]
|
||||
if not mt:
|
||||
ext = path.rpartition('.')[-1].lower()
|
||||
mt = known_extensions.get(ext)
|
||||
if not mt and is_rc_file(path):
|
||||
mt = 'text/plain'
|
||||
return mt
|
||||
@@ -17,6 +17,7 @@ from .config import KeyAction, parse_key_action
|
||||
from .constants import config_dir
|
||||
from .typing import MatchType
|
||||
from .utils import expandvars, log_error
|
||||
from .guess_mime_type import guess_type
|
||||
|
||||
|
||||
class MatchCriteria(NamedTuple):
|
||||
@@ -75,12 +76,8 @@ def url_matches_criterion(purl: 'ParseResult', url: str, unquoted_path: str, mc:
|
||||
|
||||
if mc.type == 'mime':
|
||||
import fnmatch
|
||||
from mimetypes import guess_type
|
||||
try:
|
||||
mt = guess_type(unquoted_path)[0]
|
||||
except Exception:
|
||||
return False
|
||||
if mt is None:
|
||||
mt = guess_type(unquoted_path)
|
||||
if not mt:
|
||||
return False
|
||||
mt = mt.lower()
|
||||
for mpat in mc.value.split(','):
|
||||
|
||||
Reference in New Issue
Block a user