syntax_highlight.py 3.96 KB
Newer Older
1 2 3
import logging
import sys

4 5 6 7 8
from gajim.plugins import GajimPlugin

from syntax_highlight.types import LineBreakOptions
from syntax_highlight.types import CodeMarkerOptions
from syntax_highlight.types import PLUGIN_INTERNAL_NONE_LEXER_ID
9 10 11 12 13
if sys.version_info >= (3, 4):
    from importlib.util import find_spec as find_module
else:
    from importlib import find_loader as find_module

14
PYGMENTS_MISSING = 'You are missing Python-Pygments.'
15

Philipp Hörist's avatar
Philipp Hörist committed
16
log = logging.getLogger('gajim.p.syntax_highlight')
17

18

19 20 21 22
def try_loading_pygments():
    success = find_module('pygments') is not None
    if success:
        try:
23 24 25 26 27 28 29 30
            from syntax_highlight.chat_syntax_highlighter import \
                ChatSyntaxHighlighter
            from syntax_highlight.plugin_config_dialog import \
                SyntaxHighlighterPluginConfiguration
            from syntax_highlight.plugin_config import SyntaxHighlighterConfig
            global SyntaxHighlighterPluginConfiguration
            global ChatSyntaxHighlighter
            global SyntaxHighlighterConfig
31 32 33 34 35 36 37 38 39 40 41 42
            success = True
            log.debug("pygments loaded.")
        except Exception as exception:
            log.error("Import Error: %s.", exception)
            success = False

    return success


class SyntaxHighlighterPlugin(GajimPlugin):
    def on_connect_with_chat_control(self, chat_control):
        account = chat_control.contact.account.name
43
        jid = chat_control.contact.jid
44 45 46
        if account not in self.ccontrol:
            self.ccontrol[account] = {}
        self.ccontrol[account][jid] = ChatSyntaxHighlighter(
47
            self.conf, chat_control.conv_textview)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

    def on_disconnect_from_chat_control(self, chat_control):
        account = chat_control.contact.account.name
        jid = chat_control.contact.jid
        del self.ccontrol[account][jid]

    def on_print_real_text(self, text_view, real_text, other_tags, graphics,
            iterator, additional):
        account = text_view.account
        for jid in self.ccontrol[account]:
            if self.ccontrol[account][jid].textview != text_view:
                continue
            self.ccontrol[account][jid].process_text(
                real_text, other_tags, graphics, iterator, additional)
            return

    def try_init(self):
        """
        Separating this part of the initialization from the init() method
        allows repeating this step again, without reloading the plugin,
        i.e. restarting Gajim for instance.
        Doing so allows resolving the dependency issues without restart :)
        """
        pygments_loaded = try_loading_pygments()
        if not pygments_loaded:
            return False

        self.activatable = True
        self.available_text = None
        self.config_dialog = SyntaxHighlighterPluginConfiguration(self)

79
        self.conf = SyntaxHighlighterConfig(self.config)
80 81 82
        # The following initialization requires pygments to be available.
        self.conf.init_pygments()

83
        self.config_dialog = SyntaxHighlighterPluginConfiguration(self)
84 85 86
        self.config_dialog.set_config(self.conf)

        self.gui_extension_points = {
87 88 89 90
            'chat_control_base': (
                self.on_connect_with_chat_control,
                self.on_disconnect_from_chat_control),
            'print_real_text': (self.on_print_real_text, None), }
91 92 93
        return True

    def init(self):
94
        self.ccontrol = {}
95 96

        self.config_default_values = {
97 98 99 100 101 102 103 104
            'default_lexer': (PLUGIN_INTERNAL_NONE_LEXER_ID, ''),
            'line_break': (LineBreakOptions.MULTILINE, ''),
            'style': ('default', ''),
            'font': ('Monospace 10', ''),
            'bgcolor': ('#ccc', ''),
            'bgcolor_override': (True, ''),
            'code_marker': (CodeMarkerOptions.AS_COMMENT, ''),
            'pygments_path': (None, ''), }
105 106 107 108 109 110 111

        is_initialized = self.try_init()

        if not is_initialized:
            self.activatable = False
            self.available_text = PYGMENTS_MISSING
            self.config_dialog = None