Merge pull request #177 from paul-nameless/custom-mailcap

Set custom path to mailcap file
This commit is contained in:
Nameless 2020-09-09 10:41:36 +03:00 committed by GitHub
commit f78818554e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 4 deletions

View file

@ -186,11 +186,13 @@ KEEP_MEDIA = 7
FILE_PICKER_CMD = "ranger --choosefile={file_path}" FILE_PICKER_CMD = "ranger --choosefile={file_path}"
# FILE_PICKER_CMD = "nnn -p {file_path}" # FILE_PICKER_CMD = "nnn -p {file_path}"
MAILCAP_FILE = os.path.expanduser("~/.config/mailcap")
``` ```
### Mailcap file ### Mailcap file
Mailcap file is used for deciding how to open telegram files (docs, pics, voice notes, etc.). Mailcap file is used for deciding how to open telegram files (docs, pics, voice notes, etc.). Path to the file can be overriden with `MAILCAP_FILE` in config file.
Example: `~/.mailcap` Example: `~/.mailcap`

View file

@ -5,7 +5,7 @@ overwritten by external config file
import os import os
import platform import platform
import runpy import runpy
from typing import Dict from typing import Dict, Optional
_os_name = platform.system() _os_name = platform.system()
_darwin = "Darwin" _darwin = "Darwin"
@ -15,6 +15,7 @@ _linux = "Linux"
CONFIG_DIR = os.path.expanduser("~/.config/tg/") CONFIG_DIR = os.path.expanduser("~/.config/tg/")
CONFIG_FILE = os.path.join(CONFIG_DIR, "conf.py") CONFIG_FILE = os.path.join(CONFIG_DIR, "conf.py")
FILES_DIR = os.path.expanduser("~/.cache/tg/") FILES_DIR = os.path.expanduser("~/.cache/tg/")
MAILCAP_FILE: Optional[str] = None
LOG_LEVEL = "INFO" LOG_LEVEL = "INFO"
LOG_PATH = os.path.expanduser("~/.local/share/tg/") LOG_PATH = os.path.expanduser("~/.local/share/tg/")

View file

@ -17,7 +17,7 @@ from functools import lru_cache
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
from subprocess import CompletedProcess from subprocess import CompletedProcess
from types import TracebackType from types import TracebackType
from typing import Any, Optional, Tuple, Type from typing import Any, Dict, Optional, Tuple, Type
from tg import config from tg import config
@ -72,12 +72,19 @@ def get_mime(file_path: str) -> str:
return mtype.split("/")[0] return mtype.split("/")[0]
def get_mailcap() -> Dict:
if config.MAILCAP_FILE:
with open(config.MAILCAP_FILE) as f:
return mailcap.readmailcapfile(f) # type: ignore
return mailcap.getcaps()
def get_file_handler(file_path: str) -> str: def get_file_handler(file_path: str) -> str:
mtype, _ = mimetypes.guess_type(file_path) mtype, _ = mimetypes.guess_type(file_path)
if not mtype: if not mtype:
return config.DEFAULT_OPEN.format(file_path=shlex.quote(file_path)) return config.DEFAULT_OPEN.format(file_path=shlex.quote(file_path))
caps = mailcap.getcaps() caps = get_mailcap()
handler, view = mailcap.findmatch(caps, mtype, filename=file_path) handler, view = mailcap.findmatch(caps, mtype, filename=file_path)
if not handler: if not handler:
return config.DEFAULT_OPEN.format(file_path=shlex.quote(file_path)) return config.DEFAULT_OPEN.format(file_path=shlex.quote(file_path))