Sun, 04 Dec 2022 11:58:37 +0100
Project Viewer
- removed the CORBA and Protobuf viewers to make them available as plugins
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 | |
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
|
10 | 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
|
11 | |
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
|
12 | from eric7 import Preferences |
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
|
13 | |
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
|
14 | |
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
|
15 | 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
|
16 | """ |
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 | 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
|
18 | |
8205
4a0f1f896341
Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
19 | @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
|
20 | @type str |
4a0f1f896341
Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
21 | @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
|
22 | @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
|
23 | """ |
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
|
24 | mimetype = mimetypes.guess_type(filename)[0] |
9559
34fc53e6159d
Project Viewer
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9534
diff
changeset
|
25 | # TODO: add a dialog to ask if the file is a text file in case mimetype is None |
34fc53e6159d
Project Viewer
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9534
diff
changeset
|
26 | # and option is not set |
34fc53e6159d
Project Viewer
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9534
diff
changeset
|
27 | # TODO: add capability to define additional text file patterns for fnmatch test |
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 | return (mimetype is None and Preferences.getUI("LoadUnknownMimeTypeFiles")) or ( |
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
|
29 | mimetype is not None |
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
|
30 | and ( |
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
|
31 | 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
|
32 | 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
|
33 | ) |
8205
4a0f1f896341
Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
34 | ) |
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
|
35 | |
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
|
36 | |
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
|
37 | 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
|
38 | """ |
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
|
39 | 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
|
40 | |
8205
4a0f1f896341
Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
41 | @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
|
42 | @type str |
4a0f1f896341
Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
43 | @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
|
44 | @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
|
45 | """ |
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
|
46 | return mimetypes.guess_type(filename)[0] |