src/eric7/Utilities/MimeTypes.py

Sat, 31 Dec 2022 16:23:21 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 31 Dec 2022 16:23:21 +0100
branch
eric7
changeset 9653
e67609152c5e
parent 9576
be9f8e7e42e0
child 9894
8f0da84f216f
permissions
-rw-r--r--

Updated copyright for 2023.

# -*- coding: utf-8 -*-

# Copyright (c) 2014 - 2023 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing mimetype dependent functions.
"""

import fnmatch
import mimetypes

from PyQt6.QtCore import QCoreApplication

from eric7 import Preferences
from eric7.EricWidgets import EricMessageBox


def isTextFile(filename):
    """
    Function to test, if the given file is a text (i.e. editable) file.

    @param filename name of the file to be checked
    @type str
    @return flag indicating an editable file
    @rtype bool
    """
    mimetype = mimetypes.guess_type(filename)[0]
    if mimetype is None:
        return (
            Preferences.getUI("LoadUnknownMimeTypeFiles")
            or any(
                fnmatch.fnmatch(filename, pat)
                for pat in Preferences.getUI("TextFilePatterns")
            )
            or EricMessageBox.yesNo(
                None,
                QCoreApplication.translate("MimeTypes", "Open File"),
                QCoreApplication.translate(
                    "MimeTypes",
                    "<p>Is the file <b>{0}</b> a text file to be opened in eric?</p>"
                    "<p><b>Note:</b> You may suppress this question by adding a pattern"
                    " to the list of known text files on the <b>MimeTypes</b>"
                    " configuration page.</p>",
                ).format(filename),
            )
        )
    else:
        return mimetype.split("/")[0] == "text" or mimetype in Preferences.getUI(
            "TextMimeTypes"
        )


def mimeType(filename):
    """
    Function to get the mime type of a file.

    @param filename name of the file to be checked
    @type str
    @return mime type of the file
    @rtype str
    """
    return mimetypes.guess_type(filename)[0]

eric ide

mercurial