<commit_before>"""Light config player."""
from copy import deepcopy
from mpf.config_players.device_config_player import DeviceConfigPlayer
from mpf.core.rgb_color import RGBColor
from mpf.core.utility_functions import Util


class LightPlayer(DeviceConfigPlayer):

    """Sets lights based on config."""

    config_file_section = 'light_player'
    show_section = 'lights'
    machine_collection_name = 'lights'

    def play(self, settings, context, calling_context, priority=0, **kwargs):
        """Set light color based on config."""
        instance_dict = self._get_instance_dict(context)
        full_context = self._get_full_context(context)
        del kwargs

        for light, s in settings.items():
            s = deepcopy(s)
            try:
                s['priority'] += priority
            except KeyError:
                s['priority'] = priority
            if isinstance(light, str):
                light_names = Util.string_to_list(light)
                for light_name in light_names:
                    # skip non-replaces placeholders
                    if not light_name or light_name[0:1] == "(" and light_name[-1:] == ")":
                        continue
                    self._light_named_color(light_name, instance_dict, full_context, **s)
            else:
                self._light_color(light, instance_dict, full_context, **s)

    def _remove(self, settings, context, priority):
        instance_dict = self._get_instance_dict(context)
        full_context = self._get_full_context(context)

        for light, s in settings.items():
            s = deepcopy(s)
            try:
                s['priority'] += priority
            except KeyError:
                s['priority'] = priority
            if isinstance(light, str):
                light_names = Util.string_to_list(light)
                for light_name in light_names:
                    self._light_remove_named(light_name, instance_dict, full_context)
            else:
                self._light_remove(light, instance_dict, full_context)

    def _light_remove_named(self, light_name, instance_dict, full_context):
        try:
            lights = [self.machine.lights[light_name]]
        except KeyError:
            lights = self.machine.lights.items_tagged(light_name)

        for light in lights:
            self._light_remove(light, instance_dict, full_context)

    @staticmethod
    def _light_remove(light, instance_dict, full_context):
        light.remove_from_stack_by_key(full_context)
        try:
            del instance_dict[light.name]
        except KeyError:
            pass

    def handle_subscription_change(self, value, settings, priority, context):
        """Handle subscriptions."""
        if value:
            self.play(settings, context, "", priority)
        else:
            self._remove(settings, context, priority)

    def _light_named_color(self, light_name, instance_dict,
                           full_context, color, **s):
        try:
            lights = [self.machine.lights[light_name]]
        except KeyError:
            lights = self.machine.lights.items_tagged(light_name)

        if not lights:
            raise AssertionError("Could not find light or tag {} in {}".format(light_name, full_context))

        for light in lights:
            self._light_color(light, instance_dict, full_context, color, **s)

    @staticmethod
    def _light_color(light, instance_dict, full_context, color, **s):
        if color == "on":
            color = light.config['default_on_color']
        else:
            # hack to keep compatibility for matrix_light values
            if len(color) == 1:
                color = "0" + color + "0" + color + "0" + color
            elif len(color) == 2:
                color = color + color + color

            color = RGBColor(color)
        light.color(color, key=full_context, **s)
        instance_dict[light.name] = light

    def clear_context(self, context):
        """Remove all colors which were set in context."""
        full_context = self._get_full_context(context)
        for light in self._get_instance_dict(context).values():
            light.remove_from_stack_by_key(full_context)<commit_msg>support "stop" in light_player<commit_after>"""Light config player."""
from copy import deepcopy
from mpf.config_players.device_config_player import DeviceConfigPlayer
from mpf.core.rgb_color import RGBColor
from mpf.core.utility_functions import Util


class LightPlayer(DeviceConfigPlayer):

    """Sets lights based on config."""

    config_file_section = 'light_player'
    show_section = 'lights'
    machine_collection_name = 'lights'

    def play(self, settings, context, calling_context, priority=0, **kwargs):
        """Set light color based on config."""
        instance_dict = self._get_instance_dict(context)
        full_context = self._get_full_context(context)
        del kwargs

        for light, s in settings.items():
            s = deepcopy(s)
            try:
                s['priority'] += priority
            except KeyError:
                s['priority'] = priority
            if isinstance(light, str):
                light_names = Util.string_to_list(light)
                for light_name in light_names:
                    # skip non-replaces placeholders
                    if not light_name or light_name[0:1] == "(" and light_name[-1:] == ")":
                        continue
                    self._light_named_color(light_name, instance_dict, full_context, **s)
            else:
                self._light_color(light, instance_dict, full_context, **s)

    def _remove(self, settings, context, priority):
        instance_dict = self._get_instance_dict(context)
        full_context = self._get_full_context(context)

        for light, s in settings.items():
            s = deepcopy(s)
            try:
                s['priority'] += priority
            except KeyError:
                s['priority'] = priority
            if isinstance(light, str):
                light_names = Util.string_to_list(light)
                for light_name in light_names:
                    self._light_remove_named(light_name, instance_dict, full_context)
            else:
                self._light_remove(light, instance_dict, full_context)

    def _light_remove_named(self, light_name, instance_dict, full_context):
        try:
            lights = [self.machine.lights[light_name]]
        except KeyError:
            lights = self.machine.lights.items_tagged(light_name)

        for light in lights:
            self._light_remove(light, instance_dict, full_context)

    @staticmethod
    def _light_remove(light, instance_dict, full_context):
        light.remove_from_stack_by_key(full_context)
        try:
            del instance_dict[light.name]
        except KeyError:
            pass

    def handle_subscription_change(self, value, settings, priority, context):
        """Handle subscriptions."""
        if value:
            self.play(settings, context, "", priority)
        else:
            self._remove(settings, context, priority)

    def _light_named_color(self, light_name, instance_dict,
                           full_context, color, **s):
        try:
            lights = [self.machine.lights[light_name]]
        except KeyError:
            lights = self.machine.lights.items_tagged(light_name)

        if not lights:
            raise AssertionError("Could not find light or tag {} in {}".format(light_name, full_context))

        for light in lights:
            if color == "stop":
                self._light_remove(light, instance_dict, full_context)
            else:
                self._light_color(light, instance_dict, full_context, color, **s)

    @staticmethod
    def _light_color(light, instance_dict, full_context, color, **s):
        if color == "on":
            color = light.config['default_on_color']
        else:
            # hack to keep compatibility for matrix_light values
            if len(color) == 1:
                color = "0" + color + "0" + color + "0" + color
            elif len(color) == 2:
                color = color + color + color

            color = RGBColor(color)
        light.color(color, key=full_context, **s)
        instance_dict[light.name] = light

    def clear_context(self, context):
        """Remove all colors which were set in context."""
        full_context = self._get_full_context(context)
        for light in self._get_instance_dict(context).values():
            light.remove_from_stack_by_key(full_context)
{'fraction_non_alphanumeric': 0.09210526315789473, 'fraction_numerical': 0.002095947834187238, 'mean_word_length': 2.1965016747301824, 'pattern_counts': {'":': 5, '<': 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': '2251191', 'n_tokens_mistral': 2350, 'n_tokens_neox': 2266, 'n_words': 567}