Tue, 06 Dec 2022 16:00:06 +0100
Removed an obsolete TODO marker.
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 | |
8881
54e42bc2437a
Updated copyright for 2022.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
3 | # Copyright (c) 2014 - 2022 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 | 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 | 13 | from PyQt6.QtCore import QCoreApplication |
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 | 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 | 29 | if mimetype is None: |
30 | return ( | |
31 | Preferences.getUI("LoadUnknownMimeTypeFiles") | |
32 | or any( | |
33 | fnmatch.fnmatch(filename, pat) | |
34 | for pat in Preferences.getUI("TextFilePatterns") | |
35 | ) | |
36 | or EricMessageBox.yesNo( | |
37 | None, | |
38 | QCoreApplication.translate("MimeTypes", "Open File"), | |
39 | QCoreApplication.translate( | |
40 | "MimeTypes", | |
41 | "<p>Is the file <b>{0}</b> a text file to be opened in eric?</p>" | |
42 | "<p><b>Note:</b> You may suppress this question by adding a pattern" | |
43 | " to the list of known text files on the <b>MimeTypes</b>" | |
9564
f413aee05c4d
Updated translations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9563
diff
changeset
|
44 | " configuration page.</p>" |
9563 | 45 | ).format(filename), |
46 | ) | |
47 | ) | |
48 | else: | |
49 | return ( | |
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
|
50 | mimetype.split("/")[0] == "text" |
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
|
51 | or mimetype in Preferences.getUI("TextMimeTypes") |
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
|
52 | ) |
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
|
53 | |
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
|
54 | |
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
|
55 | 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
|
56 | """ |
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
|
57 | 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
|
58 | |
8205
4a0f1f896341
Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
59 | @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
|
60 | @type str |
4a0f1f896341
Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
61 | @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
|
62 | @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
|
63 | """ |
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
|
64 | return mimetypes.guess_type(filename)[0] |