5 |
5 |
6 """ |
6 """ |
7 Module implementing mimetype dependent functions. |
7 Module implementing mimetype dependent functions. |
8 """ |
8 """ |
9 |
9 |
|
10 import fnmatch |
10 import mimetypes |
11 import mimetypes |
11 |
12 |
|
13 from PyQt6.QtCore import QCoreApplication |
|
14 |
12 from eric7 import Preferences |
15 from eric7 import Preferences |
|
16 from eric7.EricWidgets import EricMessageBox |
13 |
17 |
14 |
18 |
15 def isTextFile(filename): |
19 def isTextFile(filename): |
16 """ |
20 """ |
17 Function to test, if the given file is a text (i.e. editable) file. |
21 Function to test, if the given file is a text (i.e. editable) file. |
20 @type str |
24 @type str |
21 @return flag indicating an editable file |
25 @return flag indicating an editable file |
22 @rtype bool |
26 @rtype bool |
23 """ |
27 """ |
24 mimetype = mimetypes.guess_type(filename)[0] |
28 mimetype = mimetypes.guess_type(filename)[0] |
25 # TODO: add a dialog to ask if the file is a text file in case mimetype is None |
|
26 # and option is not set |
|
27 # TODO: add capability to define additional text file patterns for fnmatch test |
29 # TODO: add capability to define additional text file patterns for fnmatch test |
28 return (mimetype is None and Preferences.getUI("LoadUnknownMimeTypeFiles")) or ( |
30 if mimetype is None: |
29 mimetype is not None |
31 return ( |
30 and ( |
32 Preferences.getUI("LoadUnknownMimeTypeFiles") |
|
33 or any( |
|
34 fnmatch.fnmatch(filename, pat) |
|
35 for pat in Preferences.getUI("TextFilePatterns") |
|
36 ) |
|
37 or EricMessageBox.yesNo( |
|
38 None, |
|
39 QCoreApplication.translate("MimeTypes", "Open File"), |
|
40 QCoreApplication.translate( |
|
41 "MimeTypes", |
|
42 "<p>Is the file <b>{0}</b> a text file to be opened in eric?</p>" |
|
43 "<p><b>Note:</b> You may suppress this question by adding a pattern" |
|
44 " to the list of known text files on the <b>MimeTypes</b>" |
|
45 " configuration page." |
|
46 ).format(filename), |
|
47 ) |
|
48 ) |
|
49 else: |
|
50 return ( |
31 mimetype.split("/")[0] == "text" |
51 mimetype.split("/")[0] == "text" |
32 or mimetype in Preferences.getUI("TextMimeTypes") |
52 or mimetype in Preferences.getUI("TextMimeTypes") |
33 ) |
53 ) |
34 ) |
|
35 |
54 |
36 |
55 |
37 def mimeType(filename): |
56 def mimeType(filename): |
38 """ |
57 """ |
39 Function to get the mime type of a file. |
58 Function to get the mime type of a file. |