meld r1327 - trunk



Author: vincele
Date: Sat Apr  4 21:18:46 2009
New Revision: 1327
URL: http://svn.gnome.org/viewvc/meld?rev=1327&view=rev

Log:
Add a *sourceview* abstraction, hiding API differences between
sourceview, gtksourceview, gtksourceview-2.2.x & gtksourceview-2.4.x


Added:
   trunk/sourceviewer.py
Modified:
   trunk/filediff.py
   trunk/meldapp.py

Modified: trunk/filediff.py
==============================================================================
--- trunk/filediff.py	(original)
+++ trunk/filediff.py	Sat Apr  4 21:18:46 2009
@@ -33,29 +33,7 @@
 import paths
 import cairo
 
-sourceview_available = 0
-
-for sourceview in "gtksourceview sourceview".split():
-    try:
-        gsv = __import__(sourceview)
-        sourceview_available = 1
-        break
-    except ImportError:
-        pass
-
-if sourceview_available:
-    def set_highlighting_enabled(buf, fname, enabled):
-        if enabled:
-            import gnomevfs
-            mime_type = gnomevfs.get_mime_type( 
-                    gnomevfs.make_uri_from_input(os.path.abspath(fname)) )
-            man = gsv.SourceLanguagesManager()
-            gsl = man.get_language_from_mime_type( mime_type )
-            if gsl:
-                buf.set_language(gsl)
-            else:
-                enabled = False
-        buf.set_highlight(enabled)
+from sourceviewer import srcviewer
 
 gdk = gtk.gdk
 
@@ -80,21 +58,17 @@
         """Start up an filediff with num_panes empty contents.
         """
         melddoc.MeldDoc.__init__(self, prefs)
-        override = {}
-        if sourceview_available:
-            override["GtkTextView"] = gsv.SourceView
-            override["GtkTextBuffer"] = gsv.SourceBuffer
-        gnomeglade.Component.__init__(self, paths.share_dir("glade2/filediff.glade"), "filediff", override)
+        gnomeglade.Component.__init__(self, paths.share_dir("glade2/filediff.glade"), "filediff", srcviewer.override)
         self.map_widgets_into_lists( ["textview", "fileentry", "diffmap", "scrolledwindow", "linkmap", "statusimage"] )
         for d in self.diffmap: d.hide()
         self._update_regexes()
         self.warned_bad_comparison = False
-        if sourceview_available:
+        if srcviewer:
             for v in self.textview:
-                v.set_buffer( gsv.SourceBuffer() )
+                v.set_buffer(srcviewer.GtkTextBuffer())
                 v.set_show_line_numbers(self.prefs.show_line_numbers)
                 v.set_insert_spaces_instead_of_tabs(self.prefs.spaces_instead_of_tabs)
-                v.set_tabs_width(self.prefs.tab_size)
+                srcviewer.set_tab_width(v, self.prefs.tab_size)
         self.keymask = 0
         self.load_font()
         self.deleted_lines_pending = -1
@@ -296,19 +270,19 @@
                 tabs.set_tab(i, pango.TAB_LEFT, i*value*self.pango_char_width)
             for i in range(3):
                 self.textview[i].set_tabs(tabs)
-            if sourceview_available:
+            if srcviewer:
                 for t in self.textview:
-                    t.set_tabs_width(value)
+                    srcviewer.set_tab_width(t, value)
         elif key == "use_custom_font" or key == "custom_font":
             self.load_font()
         elif key == "show_line_numbers":
-            if sourceview_available:
+            if srcviewer:
                 for t in self.textview:
                     t.set_show_line_numbers( value )
         elif key == "use_syntax_highlighting":
-            if sourceview_available:
+            if srcviewer:
                 for i in range(self.num_panes):
-                    set_highlighting_enabled(
+                    srcviewer.set_highlighting_enabled_from_file(
                         self.textbuffer[i],
                         self.bufferdata[i].filename,
                         self.prefs.use_syntax_highlighting )
@@ -318,7 +292,7 @@
             for t in self.textview:
                 t.set_wrap_mode(self.prefs.edit_wrap_lines)
         elif key == "spaces_instead_of_tabs":
-            if sourceview_available:
+            if srcviewer:
                 for t in self.textview:
                     t.set_insert_spaces_instead_of_tabs(value)
 
@@ -615,10 +589,10 @@
         lenseq = [len(d) for d in self.linediffer.diffs]
         self.scheduler.add_task( self._update_highlighting( (0,lenseq[0]), (0,lenseq[1]) ).next )
         self._connect_buffer_handlers()
-        if sourceview_available:
+        if srcviewer:
             for i in range(len(files)):
                 if files[i]:
-                    set_highlighting_enabled(self.textbuffer[i], files[i], self.prefs.use_syntax_highlighting)
+                    srcviewer.set_highlighting_enabled_from_file(self.textbuffer[i], files[i], self.prefs.use_syntax_highlighting)
         yield 0
 
     def _update_highlighting(self, range0, range1):
@@ -796,11 +770,7 @@
 
     def make_patch(self, *extra):
         fontdesc = pango.FontDescription(self.prefs.get_current_font())
-        override = {}
-        if sourceview_available:
-            override["GtkTextView"] = gsv.SourceView
-            override["GtkTextBuffer"] = gsv.SourceBuffer
-        dialog = gnomeglade.Component( paths.share_dir("glade2/filediff.glade"), "patchdialog", override)
+        dialog = gnomeglade.Component(paths.share_dir("glade2/filediff.glade"), "patchdialog", srcviewer.override)
         dialog.widget.set_transient_for( self.widget.get_toplevel() )
         texts = [b.get_text(*b.get_bounds()).split("\n") for b in self.textbuffer]
         texts[0] = [l+"\n" for l in texts[0]]
@@ -808,20 +778,16 @@
         names = [self._get_pane_label(i) for i in range(2)]
         prefix = os.path.commonprefix( names )
         names = [n[prefix.rfind("/") + 1:] for n in names]
-        if sourceview_available:
-            dialog.textview.set_buffer( gsv.SourceBuffer() )
+        if srcviewer:
+            dialog.textview.set_buffer(srcviewer.GtkTextBuffer())
         dialog.textview.modify_font(fontdesc)
         buf = dialog.textview.get_buffer()
         lines = []
         for line in difflib.unified_diff(texts[0], texts[1], names[0], names[1]):
             buf.insert( buf.get_end_iter(), line )
             lines.append(line)
-        if sourceview_available:
-            man = gsv.SourceLanguagesManager()
-            gsl = man.get_language_from_mime_type("text/x-diff")
-            if gsl:
-                buf.set_language(gsl)
-                buf.set_highlight(True)
+        if srcviewer:
+            srcviewer.set_highlighting_enabled_from_mimetype(buf, "text/x-diff", True)
         result = dialog.widget.run()
         dialog.widget.destroy()
         if result >= 0:

Modified: trunk/meldapp.py
==============================================================================
--- trunk/meldapp.py	(original)
+++ trunk/meldapp.py	Sat Apr  4 21:18:46 2009
@@ -37,16 +37,7 @@
 import dirdiff
 import task
 
-# optional
-sourceview_available = 0
-
-for sourceview in "gtksourceview sourceview".split():
-    try:
-        __import__(sourceview)
-        sourceview_available = 1
-        break
-    except ImportError:
-        pass
+from sourceviewer import srcviewer
 
 version = "1.2.1-svn"
 
@@ -211,7 +202,7 @@
             self.radiobutton_gnome_font.set_active(1)
         self.fontpicker.set_font_name( self.prefs.custom_font )
         self.spinbutton_tabsize.set_value( self.prefs.tab_size )
-        if sourceview_available:
+        if srcviewer:
             self.checkbutton_spaces_instead_of_tabs.set_active( self.prefs.spaces_instead_of_tabs )
             self.checkbutton_show_line_numbers.set_active( self.prefs.show_line_numbers )
             self.checkbutton_use_syntax_highlighting.set_active( self.prefs.use_syntax_highlighting )

Added: trunk/sourceviewer.py
==============================================================================
--- (empty file)
+++ trunk/sourceviewer.py	Sat Apr  4 21:18:46 2009
@@ -0,0 +1,178 @@
+### Copyright (C) 2009 Vincent Legoll <vincent legoll gmail com>
+
+### This program is free software; you can redistribute it and/or modify
+### it under the terms of the GNU General Public License as published by
+### the Free Software Foundation; either version 2 of the License, or
+### (at your option) any later version.
+
+### This program is distributed in the hope that it will be useful,
+### but WITHOUT ANY WARRANTY; without even the implied warranty of
+### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+### GNU General Public License for more details.
+
+### You should have received a copy of the GNU General Public License
+### along with this program; if not, write to the Free Software
+### Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+'''Abstraction from sourceview version API incompatibilities
+'''
+
+import os
+
+class _srcviewer():
+    srcviewer_module = None
+
+    def __init__(self):
+        self.gsv = __import__(self.srcviewer_module)
+        self.glm = None
+        self.version_check()
+        self.GtkTextView = None
+        self.GtkTextBuffer = None
+        self.overrides()
+        self.override = {}
+        self.override["GtkTextView"] = self.GtkTextView
+        self.override["GtkTextBuffer"] = self.GtkTextBuffer
+
+    def version_check(self):
+        raise NotImplementedError
+    def overrides(self):
+        raise NotImplementedError
+    def GtkLanguageManager(self):
+        raise NotImplementedError
+    def get_language_from_file(self, filename):
+        raise NotImplementedError
+    def set_highlight(self, buf, enabled):
+        raise NotImplementedError
+    def set_tab_width(self, tab, tab_size):
+        raise NotImplementedError
+    def get_language_from_mime_type(self, mimetype):
+        raise NotImplementedError
+
+    def get_language_manager(self):
+        if self.glm is None:
+            self.glm = self.GtkLanguageManager()
+        return self.glm
+
+    def set_highlighting_enabled(self, buf, gsl, enabled):
+        if enabled:
+            if gsl:
+                buf.set_language(gsl)
+            else:
+                enabled = False
+        self.set_highlight(buf, enabled)
+
+    def set_highlighting_enabled_from_mimetype(self, buf, mimetype, enabled):
+        self.set_highlighting_enabled(buf, self.get_language_from_mime_type(mimetype), enabled)
+
+    def set_highlighting_enabled_from_file(self, buf, fname, enabled):
+        self.set_highlighting_enabled(buf, self.get_language_from_file(os.path.abspath(fname)), enabled)
+
+class sourceview(_srcviewer):
+    srcviewer_module = "sourceview"
+
+    def version_check(self):
+        pass
+
+    def overrides(self):
+        self.GtkTextView = self.gsv.SourceView
+        self.GtkTextBuffer = self.gsv.SourceBuffer
+
+    def GtkLanguageManager(self):
+        return self.gsv.SourceLanguagesManager()
+
+    def set_tab_width(self, tab, tab_size):
+        return tab.set_tabs_width(tab_size)
+
+    def set_highlight(self, buf, enabled):
+        return buf.set_highlight(enabled)
+
+    def get_language_from_mime_type(self, mime_type):
+        return self.get_language_manager().get_language_from_mime_type(mime_type)
+
+    def get_language_from_file(self, filename):
+        import gnomevfs
+        mime_type = gnomevfs.get_mime_type(
+                gnomevfs.make_uri_from_input(os.path.abspath(filename)))
+        return self.get_language_from_mime_type(mime_type)
+
+class gtksourceview(sourceview):
+    srcviewer_module = "gtksourceview"
+
+class _gtksourceview2(_srcviewer):
+    srcviewer_module = "gtksourceview2"
+
+    def version_check(self):
+        raise NotImplementedError
+
+    def overrides(self):
+        self.GtkTextView = self.gsv.View
+        self.GtkTextBuffer = self.gsv.Buffer
+
+    def GtkLanguageManager(self):
+        return self.gsv.LanguageManager()
+
+    def set_tab_width(self, tab, tab_size):
+        return tab.set_tab_width(tab_size)
+
+    def set_highlight(self, buf, enabled):
+        return buf.set_highlight_syntax(enabled)
+
+    def get_language_from_file(self, filename):
+        raise NotImplementedError
+
+    def get_language_from_mime_type(self, mime_type):
+        for idl in self.get_language_manager().get_language_ids():
+            lang = self.get_language_manager().get_language(idl)
+            for mimetype in lang.get_mime_types():
+                if mime_type == mimetype:
+                    return lang
+        return None
+
+class gtksourceview22(_gtksourceview2):
+
+    def version_check(self):
+        if self.gsv.pygtksourceview2_version[1] > 2:
+            raise ImportError
+
+    def get_language_from_file(self, filename):
+        from fnmatch import fnmatch
+        for idl in self.get_language_manager().get_language_ids():
+            lang = self.get_language_manager().get_language(idl)
+            for aglob in lang.get_globs():
+                if fnmatch(filename, aglob):
+                    return lang
+        return None
+
+class gtksourceview24(_gtksourceview2):
+
+    def version_check(self):
+        if self.gsv.pygtksourceview2_version[1] < 4:
+            raise ImportError
+
+    def get_language_from_file(self, filename):
+        return self.get_language_manager().guess_language(filename)
+
+class nullsourceview(object):
+    """Implement the sourceviewer API when no real one is available
+    """
+
+    def __init__(self):
+        self.override = {}
+
+    def __nonzero__(self):
+        # This is only defined so that we can use this module
+        # without needing a separate boolean, like the following:
+        # from sourceviewer import srcviewer
+        # if srcviewer:
+        #     srcviewer.set_tab_width(tab, self.prefs.tab_size)
+        return False
+
+def _get_srcviewer():
+    for srcv in (gtksourceview24, gtksourceview22, gtksourceview, sourceview):
+        try:
+            return srcv()
+        except ImportError:
+            pass
+    return nullsourceview()
+
+srcviewer = _get_srcviewer()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]