<reponame>djmattyg007/journald-notify
from systemd import journal


class Monitor(object):
    def __init__(self, notifier, filters):
        self._notifier = notifier
        self._filters = filters

    def _scan(self, reader):
        for entry in reader:
            for f in self._filters:
                m = f.match(entry["MESSAGE"], entry["SYSLOG_IDENTIFIER"].lower())
                if not m:
                    continue
                title, body = m()
                self._notifier.notify(title, body, limit=f.notifiers)

    def monitor(self, reader_timeout=None):
        reader = journal.Reader()
        reader.seek_tail()
        while True:
            reader.wait(reader_timeout)
            self._scan(reader)

    def scan(self):
        reader = journal.Reader()
        reader.this_boot()
        self._scan(reader)

from systemd import journal


class Monitor(object):
    def __init__(self, notifier, filters):
        self._notifier = notifier
        self._filters = filters

    def _scan(self, reader):
        for entry in reader:
            for f in self._filters:
                m = f.match(entry["MESSAGE"], entry["SYSLOG_IDENTIFIER"].lower())
                if not m:
                    continue
                title, body = m()
                self._notifier.notify(title, body, limit=f.notifiers)

    def monitor(self, reader_timeout=None):
        reader = journal.Reader()
        reader.seek_tail()
        while True:
            reader.wait(reader_timeout)
            self._scan(reader)

    def scan(self):
        reader = journal.Reader()
        reader.this_boot()
        self._scan(reader)