<commit_before>""" This module provides the website-specific event hook plugins that implement
    the 'sites' settings block in the task configuration file.

    The plugin blocks websites during an active task.
    """

import os

from focus import common
from focus.plugin import base


class PlaySound(base.Plugin):
    """ Plays sound files at task start and completion.
        """
    name = 'PlaySound'
    version = '0.1'
    target_version = '>=0.1'
    events = ['task_start', 'task_end']
    options = [
        # Example:
        #   sounds {
        #       play /path/to/file;
        #       end_play /path/to/file;
        #       timer_play /path/to/file;
        #   }

        {
            'block': 'sounds',
            'options': [
                {
                    'name': 'play',
                    'allow_duplicates': False
                },
                {
                    'name': 'end_play',
                    'allow_duplicates': False
                },
                {
                    'name': 'timer_play',
                    'allow_duplicates': False
                }
            ]
        }
    ]

    def __init__(self):
        super(PlaySound, self).__init__()
        self.files = {}

    def _get_external_player(self):
        """ Determines external sound player to available.

            Returns string or ``None``.
            """

        if common.IS_MACOSX:
            return 'afplay'

        else:
            for name in ('mpg123', 'play', 'aplay'):
                if common.which(name):
                    return name
        return None  # couldn't find a player

    def _play_sound(self, filename):
        """ Shells player with the provided filename.

            `filename`
                Filename for sound file.
            """

        command = self._get_external_player()
        if not command:
            return  # no player found

        if common.IS_MACOSX:
            command += ' "{0}"'.format(filename)
        else:
            # append quiet flag and filename
            is_play = (command == 'play')
            command += ' -q "{0}"'.format(filename)

            # HACK: play can default to using pulseaudio. here, we
            # check if pulse command exists and delegate to alsa if
            # not
            if is_play and not common.which('pulseaudio'):
                command += ' -t alsa'

        # play sound file, ignore if it fails
        common.shell_process(command, background=True)

    def parse_option(self, option, block_name, *values):
        """ Parse options for play, end_play, and timer_play.
            """

        if len(values) != 1:
            raise TypeError
        value = os.path.realpath(values[0])

        if not os.path.isfile(value) and not os.path.islink(value):
            raise ValueError(u'Sound file "{0}" does not exist'<commit_msg>User paths now expanded for sound file paths under `sounds` config.<commit_after>""" This module provides the website-specific event hook plugins that implement
    the 'sites' settings block in the task configuration file.

    The plugin blocks websites during an active task.
    """

import os

from focus import common
from focus.plugin import base


class PlaySound(base.Plugin):
    """ Plays sound files at task start and completion.
        """
    name = 'PlaySound'
    version = '0.1'
    target_version = '>=0.1'
    events = ['task_start', 'task_end']
    options = [
        # Example:
        #   sounds {
        #       play /path/to/file;
        #       end_play /path/to/file;
        #       timer_play /path/to/file;
        #   }

        {
            'block': 'sounds',
            'options': [
                {
                    'name': 'play',
                    'allow_duplicates': False
                },
                {
                    'name': 'end_play',
                    'allow_duplicates': False
                },
                {
                    'name': 'timer_play',
                    'allow_duplicates': False
                }
            ]
        }
    ]

    def __init__(self):
        super(PlaySound, self).__init__()
        self.files = {}

    def _get_external_player(self):
        """ Determines external sound player to available.

            Returns string or ``None``.
            """

        if common.IS_MACOSX:
            return 'afplay'

        else:
            for name in ('mpg123', 'play', 'aplay'):
                if common.which(name):
                    return name
        return None  # couldn't find a player

    def _play_sound(self, filename):
        """ Shells player with the provided filename.

            `filename`
                Filename for sound file.
            """

        command = self._get_external_player()
        if not command:
            return  # no player found

        if common.IS_MACOSX:
            command += ' "{0}"'.format(filename)
        else:
            # append quiet flag and filename
            is_play = (command == 'play')
            command += ' -q "{0}"'.format(filename)

            # HACK: play can default to using pulseaudio. here, we
            # check if pulse command exists and delegate to alsa if
            # not
            if is_play and not common.which('pulseaudio'):
                command += ' -t alsa'

        # play sound file, ignore if it fails
        common.shell_process(command, background=True)

    def parse_option(self, option, block_name, *values):
        """ Parse options for play, end_play, and timer_play.
            """

        if len(values) != 1:
            raise TypeError
        value = os.path.realpath(os.path.expanduser(values[0]))

        if not os.path.isfile(value) and not os.path.islink(value):
            raise ValueError(u'Sound file "{0}" does not exist'
{'fraction_non_alphanumeric': 0.10071819425444596, 'fraction_numerical': 0.004103967168262654, 'mean_word_length': 1.495307167235495, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1660159', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1514, 'n_words': 463}