From 78a5a30f65303c2f28c6fb869fc9cc746f59b056 Mon Sep 17 00:00:00 2001 From: Paul Nameless Date: Sun, 16 Aug 2020 17:43:18 +0300 Subject: [PATCH 1/2] Set custom path to mailcap file --- readme.md | 4 +++- tg/config.py | 1 + tg/utils.py | 9 ++++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 73dddd8..d6a0829 100644 --- a/readme.md +++ b/readme.md @@ -186,11 +186,13 @@ KEEP_MEDIA = 7 FILE_PICKER_CMD = "ranger --choosefile={file_path}" # FILE_PICKER_CMD = "nnn -p {file_path}" + +MAILCAP_FILE = os.path.expanduser("~/.config/mailcap") ``` ### 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` diff --git a/tg/config.py b/tg/config.py index c6b783b..c9ed8c9 100644 --- a/tg/config.py +++ b/tg/config.py @@ -15,6 +15,7 @@ _linux = "Linux" CONFIG_DIR = os.path.expanduser("~/.config/tg/") CONFIG_FILE = os.path.join(CONFIG_DIR, "conf.py") FILES_DIR = os.path.expanduser("~/.cache/tg/") +MAILCAP_FILE = None LOG_LEVEL = "INFO" LOG_PATH = os.path.expanduser("~/.local/share/tg/") diff --git a/tg/utils.py b/tg/utils.py index 5995714..e8df9f2 100644 --- a/tg/utils.py +++ b/tg/utils.py @@ -72,12 +72,19 @@ def get_mime(file_path: str) -> str: return mtype.split("/")[0] +def get_mailcap(): + if config.MAILCAP_FILE: + with open(config.MAILCAP_FILE) as f: + return mailcap.readmailcapfile(f) + return mailcap.getcaps() + + def get_file_handler(file_path: str) -> str: mtype, _ = mimetypes.guess_type(file_path) if not mtype: 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) if not handler: return config.DEFAULT_OPEN.format(file_path=shlex.quote(file_path)) From 5c067ab5c797c72bf88ce45ae908d6937427380d Mon Sep 17 00:00:00 2001 From: Paul Nameless Date: Wed, 9 Sep 2020 10:40:41 +0300 Subject: [PATCH 2/2] Fix type checking --- tg/config.py | 4 ++-- tg/utils.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tg/config.py b/tg/config.py index c9ed8c9..aa9a794 100644 --- a/tg/config.py +++ b/tg/config.py @@ -5,7 +5,7 @@ overwritten by external config file import os import platform import runpy -from typing import Dict +from typing import Dict, Optional _os_name = platform.system() _darwin = "Darwin" @@ -15,7 +15,7 @@ _linux = "Linux" CONFIG_DIR = os.path.expanduser("~/.config/tg/") CONFIG_FILE = os.path.join(CONFIG_DIR, "conf.py") FILES_DIR = os.path.expanduser("~/.cache/tg/") -MAILCAP_FILE = None +MAILCAP_FILE: Optional[str] = None LOG_LEVEL = "INFO" LOG_PATH = os.path.expanduser("~/.local/share/tg/") diff --git a/tg/utils.py b/tg/utils.py index e8df9f2..3efd6bc 100644 --- a/tg/utils.py +++ b/tg/utils.py @@ -17,7 +17,7 @@ from functools import lru_cache from logging.handlers import RotatingFileHandler from subprocess import CompletedProcess from types import TracebackType -from typing import Any, Optional, Tuple, Type +from typing import Any, Dict, Optional, Tuple, Type from tg import config @@ -72,10 +72,10 @@ def get_mime(file_path: str) -> str: return mtype.split("/")[0] -def get_mailcap(): +def get_mailcap() -> Dict: if config.MAILCAP_FILE: with open(config.MAILCAP_FILE) as f: - return mailcap.readmailcapfile(f) + return mailcap.readmailcapfile(f) # type: ignore return mailcap.getcaps()