/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: Curtis Hovey
  • Date: 2012-02-06 14:44:29 UTC
  • mfrom: (774.1.1 gtksourceview)
  • Revision ID: sinzui.is@verizon.net-20120206144429-o3pnxw7qgdztagj3
Remove unneeded GtkSourceView1 support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 
11
11
from cStringIO import StringIO
12
12
 
13
 
import os
14
 
import re
15
13
import sys
16
14
import inspect
17
 
try:
18
 
    from xml.etree.ElementTree import (
19
 
        Element,
20
 
        SubElement,
21
 
        tostring,
22
 
        )
23
 
    Element, SubElement, tostring  # Hush PEP8 redefinition.
24
 
except ImportError:
25
 
    from elementtree.ElementTree import (
26
 
        Element,
27
 
        SubElement,
28
 
        tostring,
29
 
        )
30
15
 
31
16
from gi.repository import Gtk
32
17
from gi.repository import Pango
35
20
    have_gtksourceview = True
36
21
except ImportError:
37
22
    have_gtksourceview = False
38
 
try:
39
 
    from gi.repository import GConf
40
 
    have_gconf = True
41
 
except ImportError:
42
 
    have_gconf = False
43
23
 
44
24
from bzrlib import (
45
25
    errors,
50
30
    )
51
31
from bzrlib.diff import show_diff_trees
52
32
from bzrlib.patches import parse_patches
53
 
from bzrlib.trace import warning
54
33
from bzrlib.plugins.gtk.dialog import (
55
34
    error_dialog,
56
35
    info_dialog,
76
55
class DiffFileView(Gtk.ScrolledWindow):
77
56
    """Window for displaying diffs from a diff file"""
78
57
 
 
58
    SHOW_WIDGETS = True
 
59
 
79
60
    def __init__(self):
80
61
        super(DiffFileView, self).__init__()
81
62
        self.construct()
89
70
            self.buffer = GtkSource.Buffer()
90
71
            lang_manager = GtkSource.LanguageManager.get_default()
91
72
            language = lang_manager.guess_language(None, "text/x-patch")
92
 
            if have_gconf:
93
 
                self.apply_gedit_colors(self.buffer)
94
 
            self.apply_colordiff_colors(self.buffer)
95
73
            self.buffer.set_language(language)
96
74
            self.buffer.set_highlight_syntax(True)
97
 
 
98
75
            self.sourceview = GtkSource.View(buffer=self.buffer)
99
76
        else:
100
77
            self.buffer = Gtk.TextBuffer()
101
78
            self.sourceview = Gtk.TextView(self.buffer)
102
79
 
103
80
        self.sourceview.set_editable(False)
104
 
        self.sourceview.modify_font(Pango.FontDescription("Monospace"))
 
81
        self.sourceview.override_font(Pango.FontDescription("Monospace"))
105
82
        self.add(self.sourceview)
106
 
        self.sourceview.show()
107
 
 
108
 
    @staticmethod
109
 
    def apply_gedit_colors(buf):
110
 
        """Set style to that specified in gedit configuration.
111
 
 
112
 
        This method needs the gconf module.
113
 
 
114
 
        :param buf: a GtkSource.Buffer object.
115
 
        """
116
 
        GEDIT_SCHEME_PATH = '/apps/gedit-2/preferences/editor/colors/scheme'
117
 
        GEDIT_USER_STYLES_PATH = os.path.expanduser('~/.gnome2/gedit/styles')
118
 
 
119
 
        client = GConf.Client.get_default()
120
 
        style_scheme_name = client.get_string(GEDIT_SCHEME_PATH)
121
 
        if style_scheme_name is not None:
122
 
            style_scheme_mgr = GtkSource.StyleSchemeManager()
123
 
            style_scheme_mgr.append_search_path(GEDIT_USER_STYLES_PATH)
124
 
 
125
 
            style_scheme = style_scheme_mgr.get_scheme(style_scheme_name)
126
 
 
127
 
            if style_scheme is not None:
128
 
                buf.set_style_scheme(style_scheme)
129
 
 
130
 
    @classmethod
131
 
    def apply_colordiff_colors(klass, buf):
132
 
        """Set style colors for lang using the colordiff configuration file.
133
 
 
134
 
        Both ~/.colordiffrc and ~/.colordiffrc.bzr-gtk are read.
135
 
 
136
 
        :param buf: a "Diff" GtkSource.Buffer object.
137
 
        """
138
 
        scheme_manager = GtkSource.StyleSchemeManager()
139
 
        style_scheme = scheme_manager.get_scheme('colordiff')
140
 
 
141
 
        # if style scheme not found, we'll generate it from colordiffrc
142
 
        # TODO: reload if colordiffrc has changed.
143
 
        if style_scheme is None:
144
 
            colors = {}
145
 
 
146
 
            for f in ('~/.colordiffrc', '~/.colordiffrc.bzr-gtk'):
147
 
                f = os.path.expanduser(f)
148
 
                if os.path.exists(f):
149
 
                    try:
150
 
                        f = file(f)
151
 
                    except IOError, e:
152
 
                        warning('could not open file %s: %s' % (f, str(e)))
153
 
                    else:
154
 
                        colors.update(klass.parse_colordiffrc(f))
155
 
                        f.close()
156
 
 
157
 
            if not colors:
158
 
                # ~/.colordiffrc does not exist
159
 
                return
160
 
 
161
 
            mapping = {
162
 
                # map GtkSourceView2 scheme styles to colordiff names
163
 
                # since GSV is richer, accept new names for extra bits,
164
 
                # defaulting to old names if they're not present
165
 
                'diff:added-line': ['newtext'],
166
 
                'diff:removed-line': ['oldtext'],
167
 
                'diff:location': ['location', 'diffstuff'],
168
 
                'diff:file': ['file', 'diffstuff'],
169
 
                'diff:special-case': ['specialcase', 'diffstuff'],
170
 
            }
171
 
 
172
 
            converted_colors = {}
173
 
            for name, values in mapping.items():
174
 
                color = None
175
 
                for value in values:
176
 
                    color = colors.get(value, None)
177
 
                    if color is not None:
178
 
                        break
179
 
                if color is None:
180
 
                    continue
181
 
                converted_colors[name] = color
182
 
 
183
 
            # some xml magic to produce needed style scheme description
184
 
            e_style_scheme = Element('style-scheme')
185
 
            e_style_scheme.set('id', 'colordiff')
186
 
            e_style_scheme.set('_name', 'ColorDiff')
187
 
            e_style_scheme.set('version', '1.0')
188
 
            for name, color in converted_colors.items():
189
 
                style = SubElement(e_style_scheme, 'style')
190
 
                style.set('name', name)
191
 
                style.set('foreground', '#%s' % color)
192
 
 
193
 
            scheme_xml = tostring(e_style_scheme, 'UTF-8')
194
 
            if not os.path.exists(os.path.expanduser(
195
 
                '~/.local/share/gtksourceview-2.0/styles')):
196
 
                os.makedirs(os.path.expanduser(
197
 
                    '~/.local/share/gtksourceview-2.0/styles'))
198
 
            file(os.path.expanduser(
199
 
                '~/.local/share/gtksourceview-2.0/styles/colordiff.xml'),
200
 
                'w').write(scheme_xml)
201
 
 
202
 
            scheme_manager.force_rescan()
203
 
            style_scheme = scheme_manager.get_scheme('colordiff')
204
 
 
205
 
        buf.set_style_scheme(style_scheme)
206
 
 
207
 
    @staticmethod
208
 
    def parse_colordiffrc(fileobj):
209
 
        """Parse fileobj as a colordiff configuration file.
210
 
 
211
 
        :return: A dict with the key -> value pairs.
212
 
        """
213
 
        colors = {}
214
 
        for line in fileobj:
215
 
            if re.match(r'^\s*#', line):
216
 
                continue
217
 
            if '=' not in line:
218
 
                continue
219
 
            key, val = line.split('=', 1)
220
 
            colors[key.strip()] = val.strip()
221
 
        return colors
 
83
        if self.SHOW_WIDGETS:
 
84
            self.sourceview.show()
222
85
 
223
86
    def set_trees(self, rev_tree, parent_tree):
224
87
        self.rev_tree = rev_tree