src/eric7/Utilities/MimeTypes.py

Sun, 16 Mar 2025 12:24:41 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 16 Mar 2025 12:24:41 +0100
branch
eric7
changeset 11169
7277dd5eb324
parent 11090
f5f5f5803935
permissions
-rw-r--r--

Added "application/yaml" to the default list of text mimetypes and extended the check for a file being text for existing but unknown mimetype.

3565
8e1cd7721515 Added a utility function to handle mime types other than 'text' that are editable text files as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
8e1cd7721515 Added a utility function to handle mime types other than 'text' that are editable text files as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
11090
f5f5f5803935 Updated copyright for 2025.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10929
diff changeset
3 # Copyright (c) 2014 - 2025 Detlev Offenbach <detlev@die-offenbachs.de>
3565
8e1cd7721515 Added a utility function to handle mime types other than 'text' that are editable text files as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
8e1cd7721515 Added a utility function to handle mime types other than 'text' that are editable text files as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
8e1cd7721515 Added a utility function to handle mime types other than 'text' that are editable text files as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
8e1cd7721515 Added a utility function to handle mime types other than 'text' that are editable text files as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing mimetype dependent functions.
8e1cd7721515 Added a utility function to handle mime types other than 'text' that are editable text files as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
8e1cd7721515 Added a utility function to handle mime types other than 'text' that are editable text files as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
9563
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9559
diff changeset
10 import fnmatch
3565
8e1cd7721515 Added a utility function to handle mime types other than 'text' that are editable text files as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import mimetypes
8e1cd7721515 Added a utility function to handle mime types other than 'text' that are editable text files as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
9563
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9559
diff changeset
13 from PyQt6.QtCore import QCoreApplication
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9559
diff changeset
14
9413
80c06d472826 Changed the eric7 import statements to include the package name (i.e. eric7) in order to not fiddle with sys.path.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9221
diff changeset
15 from eric7 import Preferences
9563
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9559
diff changeset
16 from eric7.EricWidgets import EricMessageBox
3565
8e1cd7721515 Added a utility function to handle mime types other than 'text' that are editable text files as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
8e1cd7721515 Added a utility function to handle mime types other than 'text' that are editable text files as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
8e1cd7721515 Added a utility function to handle mime types other than 'text' that are editable text files as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 def isTextFile(filename):
8e1cd7721515 Added a utility function to handle mime types other than 'text' that are editable text files as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 """
8e1cd7721515 Added a utility function to handle mime types other than 'text' that are editable text files as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 Function to test, if the given file is a text (i.e. editable) file.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
22
8205
4a0f1f896341 Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
23 @param filename name of the file to be checked
4a0f1f896341 Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
24 @type str
4a0f1f896341 Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
25 @return flag indicating an editable file
4a0f1f896341 Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
26 @rtype bool
3565
8e1cd7721515 Added a utility function to handle mime types other than 'text' that are editable text files as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 """
9534
5ed8445f3b31 Improved the file type determination (i.e. is it text) and added a configurable option to even load files, whose type cannot be determine as being text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
28 mimetype = mimetypes.guess_type(filename)[0]
9563
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9559
diff changeset
29 if mimetype is None:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9559
diff changeset
30 return (
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9559
diff changeset
31 Preferences.getUI("LoadUnknownMimeTypeFiles")
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9559
diff changeset
32 or any(
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9559
diff changeset
33 fnmatch.fnmatch(filename, pat)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9559
diff changeset
34 for pat in Preferences.getUI("TextFilePatterns")
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9559
diff changeset
35 )
10929
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
36 or (
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
37 Preferences.getUI("TextMimeTypesAskUser")
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
38 and EricMessageBox.yesNo(
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
39 None,
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
40 QCoreApplication.translate("MimeTypes", "Open File"),
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
41 QCoreApplication.translate(
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
42 "MimeTypes",
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
43 "<p>Is the file <b>{0}</b> a text file to be opened in eric?"
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
44 "</p><p><b>Note:</b> You may suppress this question by adding"
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
45 " a pattern to the list of known text files on the"
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
46 " <b>MimeTypes</b> configuration page.</p>",
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
47 ).format(filename),
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
48 )
9563
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9559
diff changeset
49 )
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9559
diff changeset
50 )
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9559
diff changeset
51 else:
9894
8f0da84f216f Added the mimetype 'application/sql' to the default list of text mimetypes and added a dialog in case an unknown mimetype was discovered (see issue490).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9653
diff changeset
52 return (
8f0da84f216f Added the mimetype 'application/sql' to the default list of text mimetypes and added a dialog in case an unknown mimetype was discovered (see issue490).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9653
diff changeset
53 mimetype.split("/")[0] == "text"
8f0da84f216f Added the mimetype 'application/sql' to the default list of text mimetypes and added a dialog in case an unknown mimetype was discovered (see issue490).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9653
diff changeset
54 or mimetype in Preferences.getUI("TextMimeTypes")
11169
7277dd5eb324 Added "application/yaml" to the default list of text mimetypes and extended the check for a file being text for existing but unknown mimetype.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
55 or any(
7277dd5eb324 Added "application/yaml" to the default list of text mimetypes and extended the check for a file being text for existing but unknown mimetype.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
56 fnmatch.fnmatch(filename, pat)
7277dd5eb324 Added "application/yaml" to the default list of text mimetypes and extended the check for a file being text for existing but unknown mimetype.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
57 for pat in Preferences.getUI("TextFilePatterns")
7277dd5eb324 Added "application/yaml" to the default list of text mimetypes and extended the check for a file being text for existing but unknown mimetype.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
58 )
10929
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
59 or (
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
60 Preferences.getUI("TextMimeTypesAskUser")
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
61 and EricMessageBox.yesNo(
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
62 None,
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
63 QCoreApplication.translate("MimeTypes", "Open File"),
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
64 QCoreApplication.translate(
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
65 "MimeTypes",
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
66 "<p>The file <b>{0}</b> has the mime type <b>{1}</b>. This type"
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
67 " is not recognized as being text to be opened in eric. Is this"
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
68 " an editable text file?</p>"
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
69 "<p><b>Note:</b> You may suppress this question by adding an"
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
70 " entry to the list of known text file types on the"
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
71 " <b>MimeTypes</b> configuration page.</p>",
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
72 ).format(filename, mimetype),
edfd452bb9be Added a configuration option on the "Mime Types" configuration page to ask the user, if a file type cannot be determined as being a text file to be opened in the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
73 )
9894
8f0da84f216f Added the mimetype 'application/sql' to the default list of text mimetypes and added a dialog in case an unknown mimetype was discovered (see issue490).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9653
diff changeset
74 )
9534
5ed8445f3b31 Improved the file type determination (i.e. is it text) and added a configurable option to even load files, whose type cannot be determine as being text.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
75 )
4101
68c26f72c0d1 Added the capability to show a file's mime type and to add it to the text mime types list to the project others browser and the file browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4097
diff changeset
76
68c26f72c0d1 Added the capability to show a file's mime type and to add it to the text mime types list to the project others browser and the file browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4097
diff changeset
77
68c26f72c0d1 Added the capability to show a file's mime type and to add it to the text mime types list to the project others browser and the file browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4097
diff changeset
78 def mimeType(filename):
68c26f72c0d1 Added the capability to show a file's mime type and to add it to the text mime types list to the project others browser and the file browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4097
diff changeset
79 """
68c26f72c0d1 Added the capability to show a file's mime type and to add it to the text mime types list to the project others browser and the file browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4097
diff changeset
80 Function to get the mime type of a file.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
81
8205
4a0f1f896341 Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
82 @param filename name of the file to be checked
4a0f1f896341 Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
83 @type str
4a0f1f896341 Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
84 @return mime type of the file
4a0f1f896341 Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
85 @rtype str
4101
68c26f72c0d1 Added the capability to show a file's mime type and to add it to the text mime types list to the project others browser and the file browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4097
diff changeset
86 """
68c26f72c0d1 Added the capability to show a file's mime type and to add it to the text mime types list to the project others browser and the file browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4097
diff changeset
87 return mimetypes.guess_type(filename)[0]

eric ide

mercurial