rules: add rule condition for hostname

This commit is contained in:
Peter F. Patel-Schneider 2023-08-25 17:58:15 -04:00
parent d3649b8011
commit fc38862e8b
2 changed files with 58 additions and 1 deletions

View File

@ -1040,6 +1040,29 @@ class Device(Condition):
return {'Device': self.devID}
class Host(Condition):
def __init__(self, host, warn=True):
if not (isinstance(host, str)):
if warn:
_log.warn('rule Host Name argument not a string: %s', host)
self.host = ''
self.host = host
def __str__(self):
return 'Host: ' + str(self.host)
def evaluate(self, feature, notification, device, status, last_result):
if _log.isEnabledFor(_DEBUG):
_log.debug('evaluate condition: %s', self)
import socket
hostname = socket.getfqdn()
return hostname.startswith(self.host)
def data(self):
return {'Host': self.host}
class Action(RuleComponent):
def __init__(self, *args):
@ -1338,6 +1361,7 @@ COMPONENTS = {
'MouseGesture': MouseGesture,
'Active': Active,
'Device': Device,
'Host': Host,
'KeyPress': KeyPress,
'MouseScroll': MouseScroll,
'MouseClick': MouseClick,

View File

@ -521,6 +521,7 @@ class DiversionDialog:
(_('KeyIsDown'), _DIV.KeyIsDown, ''),
(_('Active'), _DIV.Active, ''),
(_('Device'), _DIV.Device, ''),
(_('Host'), _DIV.Host, ''),
(_('Setting'), _DIV.Setting, [None, '', None]),
(_('Test'), _DIV.Test, next(iter(_DIV.TESTS))),
(_('Test bytes'), _DIV.TestBytes, [0, 1, 0]),
@ -2252,13 +2253,44 @@ class ActiveUI(_DeviceUI, ConditionUI):
class DeviceUI(_DeviceUI, ConditionUI):
CLASS = _DIV.Device
label_text = _('Device originated the current notification.')
label_text = _('Device that originated the current notification.')
@classmethod
def left_label(cls, component):
return _('Device')
class HostUI(ConditionUI):
CLASS = _DIV.Host
def create_widgets(self):
self.widgets = {}
self.label = Gtk.Label(valign=Gtk.Align.CENTER, hexpand=True)
self.label.set_text(_('Name of host computer.'))
self.widgets[self.label] = (0, 0, 1, 1)
self.field = Gtk.Entry(halign=Gtk.Align.CENTER, valign=Gtk.Align.CENTER, hexpand=True)
self.field.set_size_request(600, 0)
self.field.connect('changed', self._on_update)
self.widgets[self.field] = (0, 1, 1, 1)
def show(self, component, editable):
super().show(component, editable)
with self.ignore_changes():
self.field.set_text(component.host)
def collect_value(self):
return self.field.get_text()
@classmethod
def left_label(cls, component):
return _('Host')
@classmethod
def right_label(cls, component):
return str(component.host)
class _SettingWithValueUI:
ALL_SETTINGS = _all_settings()
@ -2624,6 +2656,7 @@ COMPONENT_UI = {
_DIV.MouseProcess: MouseProcessUI,
_DIV.Active: ActiveUI,
_DIV.Device: DeviceUI,
_DIV.Host: HostUI,
_DIV.Feature: FeatureUI,
_DIV.Report: ReportUI,
_DIV.Modifiers: ModifiersUI,