/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to diff.py

  • Committer: Jelmer Vernooij
  • Date: 2007-07-15 15:22:29 UTC
  • Revision ID: jelmer@samba.org-20070715152229-clmlen0vpd8d2pzx
Add docstrings, remove unused code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
1
2
# -*- coding: UTF-8 -*-
2
3
"""Difference window.
3
4
 
11
12
 
12
13
from cStringIO import StringIO
13
14
 
14
 
import pygtk
15
 
pygtk.require("2.0")
16
15
import gtk
17
16
import pango
18
 
import os
19
 
import re
20
17
import sys
21
18
 
22
19
try:
24
21
    have_gtksourceview = True
25
22
except ImportError:
26
23
    have_gtksourceview = False
27
 
try:
28
 
    import gconf
29
 
    have_gconf = True
30
 
except ImportError:
31
 
    have_gconf = False
32
24
 
33
25
import bzrlib
34
26
 
35
27
from bzrlib.diff import show_diff_trees
36
28
from bzrlib.errors import NoSuchFile
37
 
from bzrlib.trace import warning
38
29
 
39
30
 
40
31
class DiffWindow(gtk.Window):
101
92
            self.buffer = gtksourceview.SourceBuffer()
102
93
            slm = gtksourceview.SourceLanguagesManager()
103
94
            gsl = slm.get_language_from_mime_type("text/x-patch")
104
 
            if have_gconf:
105
 
                self.apply_gedit_colors(gsl)
106
 
            self.apply_colordiff_colors(gsl)
107
95
            self.buffer.set_language(gsl)
108
96
            self.buffer.set_highlight(True)
109
97
 
179
167
        s = StringIO()
180
168
        show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files)
181
169
        self.buffer.set_text(s.getvalue().decode(sys.getdefaultencoding(), 'replace'))
182
 
 
183
 
    @staticmethod
184
 
    def apply_gedit_colors(lang):
185
 
        """Set style for lang to that specified in gedit configuration.
186
 
 
187
 
        This method needs the gconf module.
188
 
        
189
 
        :param lang: a gtksourceview.SourceLanguage object.
190
 
        """
191
 
        GEDIT_SYNTAX_PATH = '/apps/gedit-2/preferences/syntax_highlighting'
192
 
        GEDIT_LANG_PATH = GEDIT_SYNTAX_PATH + '/' + lang.get_id()
193
 
 
194
 
        client = gconf.client_get_default()
195
 
        client.add_dir(GEDIT_LANG_PATH, gconf.CLIENT_PRELOAD_NONE)
196
 
 
197
 
        for tag in lang.get_tags():
198
 
            tag_id = tag.get_id()
199
 
            gconf_key = GEDIT_LANG_PATH + '/' + tag_id
200
 
            style_string = client.get_string(gconf_key)
201
 
 
202
 
            if style_string is None:
203
 
                continue
204
 
 
205
 
            # function to get a bool from a string that's either '0' or '1'
206
 
            string_bool = lambda x: bool(int(x))
207
 
 
208
 
            # style_string is a string like "2/#FFCCAA/#000000/0/1/0/0"
209
 
            # values are: mask, fg, bg, italic, bold, underline, strike
210
 
            # this packs them into (str_value, attr_name, conv_func) tuples
211
 
            items = zip(style_string.split('/'), ['mask', 'foreground',
212
 
                'background', 'italic', 'bold', 'underline', 'strikethrough' ],
213
 
                [ int, gtk.gdk.color_parse, gtk.gdk.color_parse, string_bool,
214
 
                    string_bool, string_bool, string_bool ]
215
 
            )
216
 
 
217
 
            style = gtksourceview.SourceTagStyle()
218
 
 
219
 
            # XXX The mask attribute controls whether the present values of
220
 
            # foreground and background color should in fact be used. Ideally
221
 
            # (and that's what gedit does), one could set all three attributes,
222
 
            # and let the TagStyle object figure out which colors to use.
223
 
            # However, in the GtkSourceview python bindings, the mask attribute
224
 
            # is read-only, and it's derived instead from the colors being
225
 
            # set or not. This means that we have to sometimes refrain from
226
 
            # setting fg or bg colors, depending on the value of the mask.
227
 
            # This code could go away if mask were writable.
228
 
            mask = int(items[0][0])
229
 
            if not (mask & 1): # GTK_SOURCE_TAG_STYLE_USE_BACKGROUND
230
 
                items[2:3] = []
231
 
            if not (mask & 2): # GTK_SOURCE_TAG_STYLE_USE_FOREGROUND
232
 
                items[1:2] = []
233
 
            items[0:1] = [] # skip the mask unconditionally
234
 
 
235
 
            for value, attr, func in items:
236
 
                try:
237
 
                    value = func(value)
238
 
                except ValueError:
239
 
                    warning('gconf key %s contains an invalid value: %s'
240
 
                            % gconf_key, value)
241
 
                else:
242
 
                    setattr(style, attr, value)
243
 
 
244
 
            lang.set_tag_style(tag_id, style)
245
 
 
246
 
    @staticmethod
247
 
    def apply_colordiff_colors(lang):
248
 
        """Set style colors for lang using the colordiff configuration file.
249
 
 
250
 
        Both ~/.colordiffrc and ~/.colordiffrc.bzr-gtk are read.
251
 
 
252
 
        :param lang: a "Diff" gtksourceview.SourceLanguage object.
253
 
        """
254
 
        colors = {}
255
 
 
256
 
        for f in ('~/.colordiffrc', '~/.colordiffrc.bzr-gtk'):
257
 
            f = os.path.expanduser(f)
258
 
            if os.path.exists(f):
259
 
                try:
260
 
                    f = file(f)
261
 
                except IOError, e:
262
 
                    warning('could not open file %s: %s' % (f, str(e)))
263
 
                else:
264
 
                    colors.update(DiffWindow.parse_colordiffrc(f))
265
 
                    f.close()
266
 
 
267
 
        if not colors:
268
 
            # ~/.colordiffrc does not exist
269
 
            return
270
 
 
271
 
        mapping = {
272
 
                # map GtkSourceView tags to colordiff names
273
 
                # since GSV is richer, accept new names for extra bits,
274
 
                # defaulting to old names if they're not present
275
 
                'Added@32@line': ['newtext'],
276
 
                'Removed@32@line': ['oldtext'],
277
 
                'Location': ['location', 'diffstuff'],
278
 
                'Diff@32@file': ['file', 'diffstuff'],
279
 
                'Special@32@case': ['specialcase', 'diffstuff'],
280
 
        }
281
 
 
282
 
        for tag in lang.get_tags():
283
 
            tag_id = tag.get_id()
284
 
            keys = mapping.get(tag_id, [])
285
 
            color = None
286
 
 
287
 
            for key in keys:
288
 
                color = colors.get(key, None)
289
 
                if color is not None:
290
 
                    break
291
 
 
292
 
            if color is None:
293
 
                continue
294
 
 
295
 
            style = gtksourceview.SourceTagStyle()
296
 
            try:
297
 
                style.foreground = gtk.gdk.color_parse(color)
298
 
            except ValueError:
299
 
                warning('not a valid color: %s' % color)
300
 
            else:
301
 
                lang.set_tag_style(tag_id, style)
302
 
 
303
 
    @staticmethod
304
 
    def parse_colordiffrc(fileobj):
305
 
        """Parse fileobj as a colordiff configuration file.
306
 
        
307
 
        :return: A dict with the key -> value pairs.
308
 
        """
309
 
        colors = {}
310
 
        for line in fileobj:
311
 
            if re.match(r'^\s*#', line):
312
 
                continue
313
 
            if '=' not in line:
314
 
                continue
315
 
            key, val = line.split('=', 1)
316
 
            colors[key.strip()] = val.strip()
317
 
        return colors
318