src/eric7/Utilities/MimeTypes.py

Fri, 25 Nov 2022 14:15:48 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 25 Nov 2022 14:15:48 +0100
branch
eric7
changeset 9534
5ed8445f3b31
parent 9413
80c06d472826
child 9559
34fc53e6159d
permissions
-rw-r--r--

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.

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]
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
25 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
26 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
27 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
28 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
29 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
30 )
8205
4a0f1f896341 Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
31 )
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
32
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
33
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
34 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
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 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
37
8205
4a0f1f896341 Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
38 @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
39 @type str
4a0f1f896341 Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
40 @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
41 @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
42 """
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
43 return mimetypes.guess_type(filename)[0]

eric ide

mercurial