cq: Format with black and isort
This commit is contained in:
@@ -20,11 +20,10 @@ from typing import TYPE_CHECKING
|
||||
|
||||
from gi.repository import Gtk
|
||||
|
||||
from gajim.gtk.settings import SettingsDialog
|
||||
from gajim.gtk.const import Setting
|
||||
from gajim.gtk.const import SettingKind
|
||||
from gajim.gtk.const import SettingType
|
||||
|
||||
from gajim.gtk.settings import SettingsDialog
|
||||
from gajim.plugins.plugins_i18n import _
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -32,52 +31,56 @@ if TYPE_CHECKING:
|
||||
|
||||
|
||||
class LengthNotifierConfigDialog(SettingsDialog):
|
||||
def __init__(self,
|
||||
plugin: LengthNotifierPlugin,
|
||||
parent: Gtk.Window
|
||||
) -> None:
|
||||
def __init__(self, plugin: LengthNotifierPlugin, parent: Gtk.Window) -> None:
|
||||
|
||||
self.plugin = plugin
|
||||
jids = self.plugin.config['JIDS'] or ''
|
||||
jids = self.plugin.config["JIDS"] or ""
|
||||
if isinstance(jids, list):
|
||||
# Gajim 1.0 stored this as list[str]
|
||||
jids = ','.join(jids)
|
||||
jids = ",".join(jids)
|
||||
|
||||
settings = [
|
||||
Setting(SettingKind.SPIN,
|
||||
_('Message Length'),
|
||||
SettingType.VALUE,
|
||||
str(self.plugin.config['MESSAGE_WARNING_LENGTH']),
|
||||
callback=self._on_setting,
|
||||
data='MESSAGE_WARNING_LENGTH',
|
||||
desc=_('Message length at which the highlight is shown'),
|
||||
props={'range_': (1, 1000, 1)},
|
||||
),
|
||||
Setting(SettingKind.COLOR,
|
||||
_('Color'),
|
||||
SettingType.VALUE,
|
||||
self.plugin.config['WARNING_COLOR'],
|
||||
callback=self._on_setting,
|
||||
data='WARNING_COLOR',
|
||||
desc=_('Highlight color for the message input'),
|
||||
),
|
||||
Setting(SettingKind.ENTRY,
|
||||
_('Selected Addresses'),
|
||||
SettingType.VALUE,
|
||||
jids,
|
||||
callback=self._on_setting,
|
||||
data='JIDS',
|
||||
desc=_('Enable the plugin for selected XMPP addresses '
|
||||
'only (comma separated)'),
|
||||
),
|
||||
]
|
||||
Setting(
|
||||
SettingKind.SPIN,
|
||||
_("Message Length"),
|
||||
SettingType.VALUE,
|
||||
str(self.plugin.config["MESSAGE_WARNING_LENGTH"]),
|
||||
callback=self._on_setting,
|
||||
data="MESSAGE_WARNING_LENGTH",
|
||||
desc=_("Message length at which the highlight is shown"),
|
||||
props={"range_": (1, 1000, 1)},
|
||||
),
|
||||
Setting(
|
||||
SettingKind.COLOR,
|
||||
_("Color"),
|
||||
SettingType.VALUE,
|
||||
self.plugin.config["WARNING_COLOR"],
|
||||
callback=self._on_setting,
|
||||
data="WARNING_COLOR",
|
||||
desc=_("Highlight color for the message input"),
|
||||
),
|
||||
Setting(
|
||||
SettingKind.ENTRY,
|
||||
_("Selected Addresses"),
|
||||
SettingType.VALUE,
|
||||
jids,
|
||||
callback=self._on_setting,
|
||||
data="JIDS",
|
||||
desc=_(
|
||||
"Enable the plugin for selected XMPP addresses "
|
||||
"only (comma separated)"
|
||||
),
|
||||
),
|
||||
]
|
||||
|
||||
SettingsDialog.__init__(self,
|
||||
parent,
|
||||
_('Length Notifier Configuration'),
|
||||
Gtk.DialogFlags.MODAL,
|
||||
settings,
|
||||
'')
|
||||
SettingsDialog.__init__(
|
||||
self,
|
||||
parent,
|
||||
_("Length Notifier Configuration"),
|
||||
Gtk.DialogFlags.MODAL,
|
||||
settings,
|
||||
"",
|
||||
)
|
||||
|
||||
def _on_setting(self, value: Any, data: Any) -> None:
|
||||
if isinstance(value, str):
|
||||
|
||||
@@ -12,14 +12,14 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
'''
|
||||
"""
|
||||
Message length notifier plugin.
|
||||
|
||||
:author: Mateusz Biliński <mateusz@bilinski.it>
|
||||
:since: 1st June 2008
|
||||
:copyright: Copyright (2008) Mateusz Biliński <mateusz@bilinski.it>
|
||||
:license: GPL
|
||||
'''
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
@@ -31,48 +31,50 @@ from functools import partial
|
||||
from gi.repository import Gdk
|
||||
from gi.repository import GObject
|
||||
from gi.repository import Gtk
|
||||
|
||||
from nbxmpp.protocol import JID
|
||||
|
||||
from gajim.common import app
|
||||
from gajim.common import types
|
||||
|
||||
from gajim.gtk.message_actions_box import MessageActionsBox
|
||||
from gajim.gtk.message_input import MessageInputTextView
|
||||
|
||||
from gajim.plugins import GajimPlugin
|
||||
from gajim.plugins.gajimplugin import GajimPluginConfig
|
||||
from gajim.plugins.plugins_i18n import _
|
||||
|
||||
from length_notifier.config_dialog import LengthNotifierConfigDialog
|
||||
|
||||
log = logging.getLogger('gajim.p.length_notifier')
|
||||
log = logging.getLogger("gajim.p.length_notifier")
|
||||
|
||||
|
||||
class LengthNotifierPlugin(GajimPlugin):
|
||||
def init(self) -> None:
|
||||
self.description = _('Highlights the chat window’s message input if '
|
||||
'a specified message length is exceeded.')
|
||||
self.description = _(
|
||||
"Highlights the chat window’s message input if "
|
||||
"a specified message length is exceeded."
|
||||
)
|
||||
|
||||
self.config_dialog = partial(LengthNotifierConfigDialog, self)
|
||||
|
||||
self.gui_extension_points = {
|
||||
'message_actions_box': (self._message_actions_box_created, None),
|
||||
'switch_contact': (self._on_switch_contact, None)
|
||||
"message_actions_box": (self._message_actions_box_created, None),
|
||||
"switch_contact": (self._on_switch_contact, None),
|
||||
}
|
||||
|
||||
self.config_default_values = {
|
||||
'MESSAGE_WARNING_LENGTH': (
|
||||
"MESSAGE_WARNING_LENGTH": (
|
||||
140,
|
||||
'Message length at which the highlight is shown'),
|
||||
'WARNING_COLOR': (
|
||||
'rgb(240, 220, 60)',
|
||||
'Highlight color for the message input'),
|
||||
'JIDS': (
|
||||
'',
|
||||
'Enable the plugin for selected XMPP addresses '
|
||||
'only (comma separated)')
|
||||
}
|
||||
"Message length at which the highlight is shown",
|
||||
),
|
||||
"WARNING_COLOR": (
|
||||
"rgb(240, 220, 60)",
|
||||
"Highlight color for the message input",
|
||||
),
|
||||
"JIDS": (
|
||||
"",
|
||||
"Enable the plugin for selected XMPP addresses "
|
||||
"only (comma separated)",
|
||||
),
|
||||
}
|
||||
|
||||
self._message_action_box = None
|
||||
self._actions_box_widget = None
|
||||
@@ -92,14 +94,12 @@ class LengthNotifierPlugin(GajimPlugin):
|
||||
def _create_counter(self) -> None:
|
||||
assert self._message_action_box is not None
|
||||
assert self._actions_box_widget is not None
|
||||
self._counter = Counter(self._message_action_box.msg_textview,
|
||||
self.config)
|
||||
self._counter = Counter(self._message_action_box.msg_textview, self.config)
|
||||
self._actions_box_widget.pack_end(self._counter, False, False, 0)
|
||||
|
||||
def _message_actions_box_created(self,
|
||||
message_actions_box: MessageActionsBox,
|
||||
gtk_box: Gtk.Box
|
||||
) -> None:
|
||||
def _message_actions_box_created(
|
||||
self, message_actions_box: MessageActionsBox, gtk_box: Gtk.Box
|
||||
) -> None:
|
||||
|
||||
self._message_action_box = message_actions_box
|
||||
self._actions_box_widget = gtk_box
|
||||
@@ -117,14 +117,13 @@ class LengthNotifierPlugin(GajimPlugin):
|
||||
|
||||
|
||||
class Counter(Gtk.Label):
|
||||
def __init__(self,
|
||||
message_input: MessageInputTextView,
|
||||
config: GajimPluginConfig
|
||||
) -> None:
|
||||
def __init__(
|
||||
self, message_input: MessageInputTextView, config: GajimPluginConfig
|
||||
) -> None:
|
||||
|
||||
Gtk.Label.__init__(self)
|
||||
self.set_tooltip_text(_('Number of typed characters'))
|
||||
self.get_style_context().add_class('dim-label')
|
||||
self.set_tooltip_text(_("Number of typed characters"))
|
||||
self.get_style_context().add_class("dim-label")
|
||||
|
||||
self._config = config
|
||||
|
||||
@@ -135,47 +134,48 @@ class Counter(Gtk.Label):
|
||||
self._inverted_color = None
|
||||
|
||||
self._textview = message_input
|
||||
self._signal_id = self._textview.connect('buffer-changed', self._update)
|
||||
self._signal_id = self._textview.connect("buffer-changed", self._update)
|
||||
self._provider = None
|
||||
|
||||
self._parse_config()
|
||||
self._set_css()
|
||||
|
||||
self.connect('destroy', self._on_destroy)
|
||||
self.connect("destroy", self._on_destroy)
|
||||
|
||||
def _on_destroy(self, _widget: Counter) -> None:
|
||||
self._context.remove_class('length-warning')
|
||||
self._context.remove_class("length-warning")
|
||||
assert self._signal_id is not None
|
||||
if GObject.signal_handler_is_connected(
|
||||
self._textview, self._signal_id):
|
||||
if GObject.signal_handler_is_connected(self._textview, self._signal_id):
|
||||
self._textview.disconnect(self._signal_id)
|
||||
app.check_finalize(self)
|
||||
|
||||
def _parse_config(self) -> None:
|
||||
self._max_length = cast(int, self._config['MESSAGE_WARNING_LENGTH'])
|
||||
self._max_length = cast(int, self._config["MESSAGE_WARNING_LENGTH"])
|
||||
|
||||
self._color = cast(str, self._config['WARNING_COLOR'])
|
||||
self._color = cast(str, self._config["WARNING_COLOR"])
|
||||
rgba = Gdk.RGBA()
|
||||
rgba.parse(self._color)
|
||||
red = int(255 - rgba.red * 255)
|
||||
green = int(255 - rgba.green * 255)
|
||||
blue = int(255 - rgba.blue * 255)
|
||||
self._inverted_color = f'rgb({red}, {green}, {blue})'
|
||||
self._inverted_color = f"rgb({red}, {green}, {blue})"
|
||||
|
||||
def _set_css(self) -> None:
|
||||
self._context = self._textview.get_style_context()
|
||||
if self._provider is not None:
|
||||
self._context.remove_provider(self._provider)
|
||||
css = '''
|
||||
css = """
|
||||
.length-warning > * {
|
||||
color: %s;
|
||||
background-color: %s;
|
||||
}
|
||||
''' % (self._inverted_color, self._color)
|
||||
""" % (
|
||||
self._inverted_color,
|
||||
self._color,
|
||||
)
|
||||
self._provider = Gtk.CssProvider()
|
||||
self._provider.load_from_data(bytes(css.encode()))
|
||||
self._context.add_provider(
|
||||
self._provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
|
||||
self._context.add_provider(self._provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
|
||||
|
||||
def _set_count(self, count: int) -> None:
|
||||
self.set_label(str(count))
|
||||
@@ -196,38 +196,38 @@ class Counter(Gtk.Label):
|
||||
len_text = len(text)
|
||||
self._set_count(len_text)
|
||||
if len_text > self._max_length:
|
||||
self._context.add_class('length-warning')
|
||||
self._context.add_class("length-warning")
|
||||
else:
|
||||
self._context.remove_class('length-warning')
|
||||
self._context.remove_class("length-warning")
|
||||
else:
|
||||
self._set_count(0)
|
||||
self._context.remove_class('length-warning')
|
||||
self._context.remove_class("length-warning")
|
||||
return False
|
||||
|
||||
def _jid_allowed(self, current_jid: JID) -> bool:
|
||||
jids = self._config['JIDS']
|
||||
jids = self._config["JIDS"]
|
||||
if isinstance(jids, list):
|
||||
# Gajim 1.0 stored this as list[str]
|
||||
jids = ','.join(jids)
|
||||
jids = ",".join(jids)
|
||||
|
||||
assert isinstance(jids, str)
|
||||
if not len(jids):
|
||||
# Not restricted to any JIDs
|
||||
return True
|
||||
|
||||
allowed_jids = jids.split(',')
|
||||
allowed_jids = jids.split(",")
|
||||
for allowed_jid in allowed_jids:
|
||||
try:
|
||||
address = JID.from_string(allowed_jid.strip())
|
||||
except Exception as error:
|
||||
log.error('Error parsing JID: %s (%s)' % (error, allowed_jid))
|
||||
log.error("Error parsing JID: %s (%s)" % (error, allowed_jid))
|
||||
continue
|
||||
if address.is_domain:
|
||||
if current_jid.domain == address:
|
||||
log.debug('Show counter for Domain %s' % address)
|
||||
log.debug("Show counter for Domain %s" % address)
|
||||
return True
|
||||
if current_jid == address:
|
||||
log.debug('Show counter for JID %s' % address)
|
||||
log.debug("Show counter for JID %s" % address)
|
||||
return True
|
||||
return False
|
||||
|
||||
@@ -241,6 +241,6 @@ class Counter(Gtk.Label):
|
||||
self._update()
|
||||
|
||||
def reset(self) -> None:
|
||||
self._context.remove_class('length-warning')
|
||||
self._context.remove_class("length-warning")
|
||||
self._parse_config()
|
||||
self._set_css()
|
||||
|
||||
Reference in New Issue
Block a user