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

Tue, 13 May 2014 19:07:55 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 13 May 2014 19:07:55 +0200
changeset 3565
8e1cd7721515
parent 3564
4bccb8dd0bbd
child 3566
b0307dc42e99

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

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/Project/ProjectOthersBrowser.py	Tue May 13 19:06:23 2014 +0200
+++ b/Project/ProjectOthersBrowser.py	Tue May 13 19:07:55 2014 +0200
@@ -10,8 +10,6 @@
 
 from __future__ import unicode_literals
 
-import mimetypes
-
 from PyQt4.QtCore import QModelIndex, pyqtSignal, QUrl
 from PyQt4.QtGui import QDesktopServices, QDialog, QMenu
 
@@ -20,6 +18,8 @@
     ProjectBrowserOthersType
 from .ProjectBaseBrowser import ProjectBaseBrowser
 
+import Utilities.mimetypes
+
 
 class ProjectOthersBrowser(ProjectBaseBrowser):
     """
@@ -236,8 +236,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	Tue May 13 19:06:23 2014 +0200
+++ b/UI/Browser.py	Tue May 13 19:07:55 2014 +0200
@@ -10,7 +10,6 @@
 from __future__ import unicode_literals
 
 import os
-import mimetypes
 
 from PyQt4.QtCore import QModelIndex, pyqtSignal, QUrl, Qt, qVersion
 from PyQt4.QtGui import QTreeView, QDesktopServices, QItemSelectionModel, \
@@ -27,6 +26,7 @@
 import UI.PixmapCache
 import Preferences
 import Utilities
+import Utilities.mimetypes
 
 
 class Browser(QTreeView):
@@ -423,8 +423,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:07:55 2014 +0200
@@ -0,0 +1,131 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2014 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing mimetype dependent functions.
+"""
+
+from __future__ import unicode_literals
+
+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	Tue May 13 19:06:23 2014 +0200
+++ b/eric5.e4p	Tue May 13 19:07:55 2014 +0200
@@ -1138,6 +1138,7 @@
     <Source>Plugins/VcsPlugins/vcsMercurial/HgBookmarkRenameDialog.py</Source>
     <Source>Plugins/VcsPlugins/vcsMercurial/HgBookmarksInOutDialog.py</Source>
     <Source>Plugins/VcsPlugins/vcsMercurial/HgBookmarksListDialog.py</Source>
+    <Source>Utilities/mimetypes.py</Source>
   </Sources>
   <Forms>
     <Form>PyUnit/UnittestDialog.ui</Form>

eric ide

mercurial