[syntax_highlight] Fix deprecation warnings, fix pylint errors

This fixes some deprecation warnings related to Gtk.Color and restructures
code and formatting according to pylint suggestions
This commit is contained in:
Daniel Brötzmann
2019-05-08 11:53:13 +02:00
parent 845faff240
commit c60deaea19
8 changed files with 511 additions and 509 deletions

View File

@@ -1,12 +1,13 @@
import logging
from gi.repository import Gtk as gtk
from gi.repository import Gtk
from gi.repository import Pango
from pygments.formatter import Formatter
from gajim.plugins.helpers import log
log = logging.getLogger('gajim.p.syntax_highlight')
class GTKFormatter(Formatter):
name = 'GTK Formatter'
aliases = ['textbuffer', 'gtk']
@@ -14,14 +15,14 @@ class GTKFormatter(Formatter):
def __init__(self, **options):
super(GTKFormatter, self).__init__(**options)
#Formatter.__init__(self, **options)
# Formatter.__init__(self, **options)
self.tags = {}
self.mark = options.get('start_mark', None)
@staticmethod
def create_tag_for_token(ttype, highlighting_style):
style = highlighting_style.style_for_token(ttype)
tag = gtk.TextTag.new()
tag = Gtk.TextTag.new()
if 'bgcolor' in style and not style['bgcolor'] is None:
tag.set_property('background', '#%s' % style['bgcolor'])
if 'bold' in style and style['bold']:
@@ -44,13 +45,13 @@ class GTKFormatter(Formatter):
tag.set_property('underline', 'single')
return tag
def get_tag(self, ttype, buf):
"""
Creates, stores and returs a tag for a given token type.
This method ensures that a tag style is created only once.
Furthermore, the tag will be added to the given Gtk.TextBuffer's tag table.
Furthermore, the tag will be added to the given Gtk.TextBuffer's
tag table.
"""
tag = None
if ttype in self.tags:
@@ -65,23 +66,23 @@ class GTKFormatter(Formatter):
self.mark = mark
def format(self, tokensource, outfile):
if not isinstance(outfile, gtk.TextBuffer) or outfile is None:
if not isinstance(outfile, Gtk.TextBuffer) or outfile is None:
log.warn("Did not get a buffer to format...")
return
buf = outfile
end_iter = buf.get_end_iter()
end_iter = buf.get_end_iter()
start_mark = self.mark
start_iter = buf.get_iter_at_mark(start_mark) if not start_mark is None \
else end_iter
start_mark = self.mark
start_iter = buf.get_iter_at_mark(start_mark) if \
start_mark is not None else end_iter
last_ttype = None
last_start = start_iter
last_end = buf.get_end_iter()
last_ttype = None
last_start = start_iter
last_end = buf.get_end_iter()
last_fixed_start = last_start
reset = True
reset = True
for ttype, value in tokensource:
search = None
@@ -90,24 +91,26 @@ class GTKFormatter(Formatter):
buf.apply_tag(tag, last_fixed_start, last_end)
search = last_end.forward_search(value, gtk.TextSearchFlags.TEXT_ONLY, end_iter)
search = last_end.forward_search(
value, Gtk.TextSearchFlags.TEXT_ONLY, end_iter)
reset = True
else:
# in case last_ttype is None, i.e. first loop walkthrough:
# In case last_ttype is None, i.e. first loop walkthrough:
# last_start to end_iter is the full code block.
search = last_start.forward_search(value, gtk.TextSearchFlags.TEXT_ONLY, end_iter)
search = last_start.forward_search(
value, Gtk.TextSearchFlags.TEXT_ONLY, end_iter)
# Prepare for next iteration
last_ttype = ttype
if search is not None:
(last_start, last_end) = search
# If we've found the end of a sequence of similar type tokens or if
# we are in the first loop iteration, set the fixed point
# If we've found the end of a sequence of similar type tokens
# or if we are in the first loop iteration, set the fixed point
if reset:
last_fixed_start = last_start
reset = False
else:
# Hm... Nothing found, but tags left? Seams there's nothing we
# Hm... Nothing found, but tags left? Seems there's nothing we
# can do now.
break