<commit_before>#!/usr/bin/env python3

import logging
import mpd
import select
import time

from . import _Hook

logger = logging.getLogger("barython")


class MPDHook(_Hook):
    """
    Listen on MPD events
    """
    def parse_event(self, event=None, run=True):
        """
        Parse event and return a kwargs meant be used by notify() then
        """
        return {"event": event, "run": run}

    def run(self):
        # killed will be here to track the last state of the connection. We
        # start at True to force the first print.
        killed = True
        while not self._stop_event.is_set():
            try:
                if killed:
                    # test the connection and notify to force printing the
                    # current song, as idle() will wait for any change in mpd
                    self._mpdclient.ping()
                    self._mpdclient.send_idle()
                    try:
                        self.notify(**self.parse_event(run=True))
                    except Exception as e:
                        logger.error(e)
                    killed = False
                else:
                    # wait for any change in mpd and then notify
                    changes = select.select(
                        [self._mpdclient], [], [], self.refresh
                    )
                    if not changes[0]:
                        continue
                    self._mpdclient.noidle()
                    notify_kwargs = self.parse_event(
                        self._mpdclient.fetch_idle()
                    )
                    try:
                        self.notify(**notify_kwargs)
                    except Exception as e:
                        logger.error(e)
                    self._mpdclient.send_idle()
            except:
                killed = True
                try:
                    self._mpdclient.connect(self.host, self.port)
                except:
                    logger.error(
                        "MPD is maybe not running or host/port are not correct"
                    )
                    try:
                        notify_kwargs = self.parse_event(run=False)
                        try:
                            self.notify(**notify_kwargs)<commit_msg>MPDWidget: Fix lag after the last commit<commit_after>#!/usr/bin/env python3

import logging
import mpd
import select
import time

from . import _Hook

logger = logging.getLogger("barython")


class MPDHook(_Hook):
    """
    Listen on MPD events
    """
    def parse_event(self, event=None, run=True):
        """
        Parse event and return a kwargs meant be used by notify() then
        """
        return {"event": event, "run": run}

    def run(self):
        # killed will be here to track the last state of the connection. We
        # start at True to force the first print.
        killed = True
        while not self._stop_event.is_set():
            try:
                if killed:
                    # test the connection and notify to force printing the
                    # current song, as idle() will wait for any change in mpd
                    self._mpdclient.ping()
                    self._mpdclient.send_idle()
                    try:
                        self.notify(**self.parse_event(run=True))
                    except Exception as e:
                        logger.error(e)
                    killed = False
                else:
                    # wait for any change in mpd and then notify
                    changes = select.select(
                        [self._mpdclient], [], [], self.refresh
                    )
                    if not changes[0]:
                        continue
                    notify_kwargs = self.parse_event(
                        self._mpdclient.fetch_idle()
                    )
                    try:
                        self.notify(**notify_kwargs)
                    except Exception as e:
                        logger.error(e)
                    self._mpdclient.send_idle()
            except:
                killed = True
                try:
                    self._mpdclient.connect(self.host, self.port)
                except:
                    logger.error(
                        "MPD is maybe not running or host/port are not correct"
                    )
                    try:
                        notify_kwargs = self.parse_event(run=False)
                        try:
                            self.notify(**notify_kwargs)
{'fraction_non_alphanumeric': 0.07259488084730803, 'fraction_numerical': 0.00088261253309797, 'mean_word_length': 1.0831801470588236, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '729673', 'n_tokens_mistral': 1097, 'n_tokens_neox': 1026, 'n_words': 318}