Added a utility function to handle mime types other than 'text' that are editable text files as well. 5_4_x

Tue, 13 May 2014 19:18:12 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 13 May 2014 19:18:12 +0200
branch
5_4_x
changeset 3567
d739e8de1b4f
parent 3561
1a14a490c0a2
child 3575
f44bcac6e829

Added a utility function to handle mime types other than 'text' that are editable text files as well.

APIs/Python3/eric5.api file | annotate | diff | comparison | revisions
Documentation/Help/source.qch file | annotate | diff | comparison | revisions
Documentation/Help/source.qhp file | annotate | diff | comparison | revisions
Documentation/Source/eric5.Utilities.mimetypes.html file | annotate | diff | comparison | revisions
Documentation/Source/index-eric5.Utilities.html file | annotate | diff | comparison | revisions
Project/ProjectOthersBrowser.py file | annotate | diff | comparison | revisions
UI/Browser.py file | annotate | diff | comparison | revisions
Utilities/mimetypes.py file | annotate | diff | comparison | revisions
eric5.e4p file | annotate | diff | comparison | revisions
--- a/APIs/Python3/eric5.api	Mon May 12 17:50:17 2014 +0200
+++ b/APIs/Python3/eric5.api	Tue May 13 19:18:12 2014 +0200
@@ -8370,6 +8370,8 @@
 eric5.Utilities.isinpath?4(file)
 eric5.Utilities.joinext?4(prefix, ext)
 eric5.Utilities.linesep?4()
+eric5.Utilities.mimetypes.TextMimeTypes?7
+eric5.Utilities.mimetypes.isTextFile?4(filename)
 eric5.Utilities.normabsjoinpath?4(a, *p)
 eric5.Utilities.normabspath?4(path)
 eric5.Utilities.normcaseabspath?4(path)
Binary file Documentation/Help/source.qch has changed
--- a/Documentation/Help/source.qhp	Mon May 12 17:50:17 2014 +0200
+++ b/Documentation/Help/source.qhp	Tue May 13 19:18:12 2014 +0200
@@ -959,6 +959,7 @@
             <section title="eric5.Utilities.PasswordChecker" ref="eric5.Utilities.PasswordChecker.html" />
             <section title="eric5.Utilities.__init__" ref="eric5.Utilities.__init__.html" />
             <section title="eric5.Utilities.binplistlib" ref="eric5.Utilities.binplistlib.html" />
+            <section title="eric5.Utilities.mimetypes" ref="eric5.Utilities.mimetypes.html" />
             <section title="eric5.Utilities.uic" ref="eric5.Utilities.uic.html" />
           </section>
           <section title="eric5.UtilitiesPython2" ref="index-eric5.UtilitiesPython2.html">
@@ -13004,6 +13005,7 @@
       <keyword name="isExecutable" id="isExecutable" ref="eric5.Utilities.__init__.html#isExecutable" />
       <keyword name="isLinuxPlatform" id="isLinuxPlatform" ref="eric5.Globals.__init__.html#isLinuxPlatform" />
       <keyword name="isMacPlatform" id="isMacPlatform" ref="eric5.Globals.__init__.html#isMacPlatform" />
+      <keyword name="isTextFile" id="isTextFile" ref="eric5.Utilities.mimetypes.html#isTextFile" />
       <keyword name="isWindowsPlatform" id="isWindowsPlatform" ref="eric5.Globals.__init__.html#isWindowsPlatform" />
       <keyword name="is_stream_binary_plist" id="is_stream_binary_plist" ref="eric5.Utilities.binplistlib.html#is_stream_binary_plist" />
       <keyword name="isatty" id="isatty" ref="eric5.DebugClients.Ruby.AsyncFile.html#isatty" />
@@ -13047,6 +13049,7 @@
       <keyword name="maximum_line_length" id="maximum_line_length" ref="eric5.UtilitiesPython2.pep8.html#maximum_line_length" />
       <keyword name="messageHandler" id="messageHandler" ref="eric5.E5Gui.E5ErrorMessage.html#messageHandler" />
       <keyword name="messageHandlerInstalled" id="messageHandlerInstalled" ref="eric5.E5Gui.E5ErrorMessage.html#messageHandlerInstalled" />
+      <keyword name="mimetypes (Module)" id="mimetypes (Module)" ref="eric5.Utilities.mimetypes.html" />
       <keyword name="missing_newline" id="missing_newline" ref="eric5.Plugins.CheckerPlugins.CodeStyleChecker.pep8.html#missing_newline" />
       <keyword name="missing_newline" id="missing_newline" ref="eric5.UtilitiesPython2.pep8.html#missing_newline" />
       <keyword name="missing_whitespace" id="missing_whitespace" ref="eric5.Plugins.CheckerPlugins.CodeStyleChecker.pep8.html#missing_whitespace" />
@@ -14042,8 +14045,7 @@
       <file>eric5.Utilities.crypto.__init__.html</file>
       <file>eric5.Utilities.crypto.py3AES.html</file>
       <file>eric5.Utilities.crypto.py3PBKDF2.html</file>
-      <file>eric5.Utilities.py3flakes.checker.html</file>
-      <file>eric5.Utilities.py3flakes.messages.html</file>
+      <file>eric5.Utilities.mimetypes.html</file>
       <file>eric5.Utilities.uic.html</file>
       <file>eric5.UtilitiesPython2.CodeStyleChecker.html</file>
       <file>eric5.UtilitiesPython2.DocStyleCheckerPy2.html</file>
@@ -14054,8 +14056,6 @@
       <file>eric5.UtilitiesPython2.TabnannyChecker.html</file>
       <file>eric5.UtilitiesPython2.Tools.html</file>
       <file>eric5.UtilitiesPython2.pep8.html</file>
-      <file>eric5.UtilitiesPython2.py2flakes.checker.html</file>
-      <file>eric5.UtilitiesPython2.py2flakes.messages.html</file>
       <file>eric5.VCS.CommandOptionsDialog.html</file>
       <file>eric5.VCS.ProjectBrowserHelper.html</file>
       <file>eric5.VCS.ProjectHelper.html</file>
@@ -14192,9 +14192,7 @@
       <file>index-eric5.Utilities.ClassBrowsers.html</file>
       <file>index-eric5.Utilities.crypto.html</file>
       <file>index-eric5.Utilities.html</file>
-      <file>index-eric5.Utilities.py3flakes.html</file>
       <file>index-eric5.UtilitiesPython2.html</file>
-      <file>index-eric5.UtilitiesPython2.py2flakes.html</file>
       <file>index-eric5.VCS.html</file>
       <file>index-eric5.ViewManager.html</file>
       <file>index-eric5.html</file>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Documentation/Source/eric5.Utilities.mimetypes.html	Tue May 13 19:18:12 2014 +0200
@@ -0,0 +1,60 @@
+<!DOCTYPE html>
+<html><head>
+<title>eric5.Utilities.mimetypes</title>
+<meta charset="UTF-8">
+<style>
+body {
+    background: #EDECE6;
+    margin: 0em 1em 10em 1em;
+    color: black;
+}
+
+h1 { color: white; background: #85774A; }
+h2 { color: white; background: #85774A; }
+h3 { color: white; background: #9D936E; }
+h4 { color: white; background: #9D936E; }
+    
+a { color: #BA6D36; }
+
+</style>
+</head>
+<body><a NAME="top" ID="top"></a>
+<h1>eric5.Utilities.mimetypes</h1>
+<p>
+Module implementing mimetype dependent functions.
+</p>
+<h3>Global Attributes</h3>
+<table>
+<tr><td>TextMimeTypes</td></tr>
+</table>
+<h3>Classes</h3>
+<table>
+<tr><td>None</td></tr>
+</table>
+<h3>Functions</h3>
+<table>
+<tr>
+<td><a href="#isTextFile">isTextFile</a></td>
+<td>Function to test, if the given file is a text (i.e.</td>
+</tr>
+</table>
+<hr /><hr />
+<a NAME="isTextFile" ID="isTextFile"></a>
+<h2>isTextFile</h2>
+<b>isTextFile</b>(<i>filename</i>)
+<p>
+    Function to test, if the given file is a text (i.e. editable) file.
+</p><dl>
+<dt><i>filename</i></dt>
+<dd>
+name of the file to be checked (string)
+</dd>
+</dl><dl>
+<dt>Returns:</dt>
+<dd>
+flag indicating an editable file (boolean)
+</dd>
+</dl>
+<div align="right"><a href="#top">Up</a></div>
+<hr />
+</body></html>
\ No newline at end of file
--- a/Documentation/Source/index-eric5.Utilities.html	Mon May 12 17:50:17 2014 +0200
+++ b/Documentation/Source/index-eric5.Utilities.html	Tue May 13 19:18:12 2014 +0200
@@ -56,6 +56,9 @@
 <td><a href="eric5.Utilities.binplistlib.html">binplistlib</a></td>
 <td>Module implementing a library for reading and writing binary property list files.</td>
 </tr><tr>
+<td><a href="eric5.Utilities.mimetypes.html">mimetypes</a></td>
+<td>Module implementing mimetype dependent functions.</td>
+</tr><tr>
 <td><a href="eric5.Utilities.uic.html">uic</a></td>
 <td>Module implementing a function to compile all user interface files of a directory or directory tree.</td>
 </tr>
--- a/Project/ProjectOthersBrowser.py	Mon May 12 17:50:17 2014 +0200
+++ b/Project/ProjectOthersBrowser.py	Tue May 13 19:18:12 2014 +0200
@@ -8,8 +8,6 @@
 don't fit the other categories.
 """
 
-import mimetypes
-
 from PyQt4.QtCore import QModelIndex, pyqtSignal, QUrl
 from PyQt4.QtGui import QDesktopServices, QDialog, QMenu
 
@@ -18,6 +16,8 @@
     ProjectBrowserOthersType
 from .ProjectBaseBrowser import ProjectBaseBrowser
 
+import Utilities.mimetypes
+
 
 class ProjectOthersBrowser(ProjectBaseBrowser):
     """
@@ -234,8 +234,7 @@
                 elif itm.isSvgFile():
                     self.svgFile.emit(itm.fileName())
                 else:
-                    type_ = mimetypes.guess_type(itm.fileName())[0]
-                    if type_ is None or type_.split("/")[0] == "text":
+                    if Utilities.mimetypes.isTextFile(itm.fileName()):
                         self.sourceFile.emit(itm.fileName())
                     else:
                         QDesktopServices.openUrl(QUrl(itm.fileName()))
--- a/UI/Browser.py	Mon May 12 17:50:17 2014 +0200
+++ b/UI/Browser.py	Tue May 13 19:18:12 2014 +0200
@@ -8,7 +8,6 @@
 """
 
 import os
-import mimetypes
 
 from PyQt4.QtCore import QModelIndex, pyqtSignal, QUrl, Qt, qVersion
 from PyQt4.QtGui import QTreeView, QDesktopServices, QItemSelectionModel, \
@@ -25,6 +24,7 @@
 import UI.PixmapCache
 import Preferences
 import Utilities
+import Utilities.mimetypes
 
 
 class Browser(QTreeView):
@@ -421,8 +421,7 @@
                     elif itm.isPixmapFile():
                         self.pixmapFile.emit(itm.fileName())
                     else:
-                        type_ = mimetypes.guess_type(itm.fileName())[0]
-                        if type_ is None or type_.split("/")[0] == "text":
+                        if Utilities.mimetypes.isTextFile(itm.fileName()):
                             self.sourceFile[str].emit(itm.fileName())
                         else:
                             QDesktopServices.openUrl(QUrl(itm.fileName()))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Utilities/mimetypes.py	Tue May 13 19:18:12 2014 +0200
@@ -0,0 +1,129 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2014 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing mimetype dependent functions.
+"""
+
+import mimetypes
+
+TextMimeTypes = [
+    "application/bookmarks.xbel",
+    "application/opensearchdescription+xml",
+    "application/x-actionscript",
+    "application/x-actionscript3",
+    "application/x-awk",
+    "application/x-sh",
+    "application/x-shellscript",
+    "application/x-shell-session",
+    "application/x-dos-batch",
+    "application/x-befunge",
+    "application/x-brainfuck",
+    "application/x-javascript+cheetah",
+    "application/x-javascript+spitfire",
+    "application/x-cheetah",
+    "application/x-spitfire",
+    "application/xml+cheetah",
+    "application/xml+spitfire",
+    "application/x-clojure",
+    "application/x-coldfusion",
+    "application/x-cython",
+    "application/x-django-templating",
+    "application/x-jinja",
+    "application/xml-dtd",
+    "application/x-ecl",
+    "application/x-ruby-templating",
+    "application/x-evoque",
+    "application/xml+evoque",
+    "application/x-fantom",
+    "application/x-genshi",
+    "application/x-kid",
+    "application/x-genshi-text",
+    "application/x-gettext",
+    "application/x-troff",
+    "application/xhtml+xml",
+    "application/x-php",
+    "application/x-httpd-php",
+    "application/x-httpd-php3",
+    "application/x-httpd-php4",
+    "application/x-httpd-php5",
+    "application/x-hybris",
+    "application/x-javascript+django",
+    "application/x-javascript+jinja",
+    "application/x-javascript+ruby",
+    "application/x-javascript+genshi",
+    "application/javascript",
+    "application/x-javascript",
+    "application/x-javascript+php",
+    "application/x-javascript+smarty",
+    "application/json",
+    "application/x-jsp",
+    "application/x-julia",
+    "application/x-httpd-lasso",
+    "application/x-httpd-lasso[89]",
+    "application/x-httpd-lasso8",
+    "application/x-httpd-lasso9",
+    "application/x-javascript+lasso",
+    "application/xml+lasso",
+    "application/x-lua",
+    "application/x-javascript+mako",
+    "application/x-mako",
+    "application/xml+mako",
+    "application/x-gooddata-maql",
+    "application/x-mason",
+    "application/x-moonscript",
+    "application/x-javascript+myghty",
+    "application/x-myghty",
+    "application/xml+myghty",
+    "application/x-newlisp",
+    "application/x-openedge",
+    "application/x-perl",
+    "application/postscript",
+    "application/x-pypylog",
+    "application/x-python3",
+    "application/x-python",
+    "application/x-qml",
+    "application/x-racket",
+    "application/x-pygments-tokens",
+    "application/x-ruby",
+    "application/x-standardml",
+    "application/x-scheme",
+    "application/x-sh-session",
+    "application/x-smarty",
+    "application/x-ssp",
+    "application/x-tcl",
+    "application/x-csh",
+    "application/x-urbiscript",
+    "application/xml+velocity",
+    "application/xquery",
+    "application/xml+django",
+    "application/xml+jinja",
+    "application/xml+ruby",
+    "application/xml",
+    "application/rss+xml",
+    "application/atom+xml",
+    "application/xml+php",
+    "application/xml+smarty",
+    "application/xsl+xml",
+    "application/xslt+xml",
+    
+    "image/svg+xml",
+]
+
+
+def isTextFile(filename):
+    """
+    Function to test, if the given file is a text (i.e. editable) file.
+    
+    @param filename name of the file to be checked (string)
+    @return flag indicating an editable file (boolean)
+    """
+    type_ = mimetypes.guess_type(filename)[0]
+    if (type_ is None or
+        type_.split("/")[0] == "text" or
+            type_ in TextMimeTypes):
+        return True
+    else:
+        return False
--- a/eric5.e4p	Mon May 12 17:50:17 2014 +0200
+++ b/eric5.e4p	Tue May 13 19:18:12 2014 +0200
@@ -1111,6 +1111,7 @@
     <Source>Helpviewer/OpenSearch/DefaultSearchEngines/DefaultSearchEngines_rc.py</Source>
     <Source>Helpviewer/UserAgent/UserAgentDefaults_rc.py</Source>
     <Source>E5Gui/E5ProgressDialog.py</Source>
+    <Source>Utilities/mimetypes.py</Source>
   </Sources>
   <Forms>
     <Form>PyUnit/UnittestDialog.ui</Form>

eric ide

mercurial