<commit_before>#filters
#python modules
import time
import colorsys

#rgb-pi modules
import log
import constants
import datatypes
import utils
import server
import xbmcremote
import config

class Filter(object):
    """
    represents an abstract request to the server
    """
    def __init__(self, type, filter): # takes a filter type and a filter as json encoded object
        """
        initializes a new Request object
        @param type: type of the request (see protocol)
        @param request: JSON object with the request
        @return: instance of Request
        """
        self.active = True #TODO: not used yet
        self.filter = filter
        self.onfinish = filter['onfinish'] if filter.has_key('onfinish') else 'remove'
        self.type = type
        self.finishTrigger = self.finishTrigger = datatypes.Condition(filter['finish']) if filter.has_key('finish') else None
        if log.m(log.LEVEL_FILTERS): log.l('filter initialized: ' + self.type)


    def onChangeColor(self, newColor):
        """
        transforms the given color object
        @param newColor: color to be set as new color for the LEDs
        @return: transformed color according to the filter
        """
        return newColor

    def onFadeEnd(self, seconds, startColor, endColor):
        """
        called on the end of every fade
        @param seconds: time of the fade in seconds
        @param startColor: starting color of the fade
        @param endColor: ending color of the fade
        """
        pass

    def onFadeStep(self, seconds, startColor, endColor, progress):
        """
        called on the end of every fade
        @param seconds: time of the fade in seconds
        @param startColor: starting color of the fade
        @param endColor: ending color of the fade
        @param progress: progress of the fade as percent value between 0.0 and 1.0
        """
        pass

    def finish(self):
        if self.onfinish == constants.FILTER_ONFINISH_REMOVE:
            server.CurrentFilters.remove(self)

        if self.onfinish == constants.FILTER_ONFINISH_STOP:
            if server.CurrentCMD is not None:
                server.CurrentCMD.stop()
                server.CurrentCMD = None
                server.CurrentFilters = []
                if log.m(log.LEVEL_FILTER_ACTIONS): log.l(self.type+'-filter finished...')



    @staticmethod
    def createFilter(filter):
        if isinstance(filter, dict) and filter.has_key('type'):
            if filter['type'] == constants.FILTER_TYPE_DIM:
                return DimFilter(filter)
            if filter['type'] == constants.FILTER_TYPE_VOLUMEFADE:
                return VolumeFadeFilter(filter)
            if filter['type'] == constants.FILTER_TYPE_STOPMUSIC:
                return StopMusicFilter(filter)
            if filter['type'] == constants.FILTER_TYPE_SATURATION:
                return SaturationFilter(filter)


class DimFilter(Filter):
    """
    this filter affects the led.setColor(color)-method and dims all incoming colors according to the defined time
    """
    def __init__(self, filter): # takes a command as json encoded object
        super(DimFilter, self).__init__(constants.FILTER_TYPE_DIM, filter)
        self.black = datatypes.Color(0,0,0)

    def onChangeColor(self, newColor):
        if not self.finishTrigger.check():
            self.finish()

        filteredColor = utils.interpolateColor(newColor, self.black, self.finishTrigger.progress())
        if log.m(log.LEVEL_FILTER_ACTIONS): log.l(self.type+'-filter color ('+str(self.finishTrigger.progress()*100)+'%) from '+str(newColor)+' to '+str(filteredColor))
        return filteredColor


class SaturationFilter(Filter):
    """
    this filter affects the led.setColor(color)-method and sets a specific saturation
    """
    def __init__(self, filter): # takes a command as json encoded object
        super(SaturationFilter, self).__init__(constants.FILTER_TYPE_SATURATION, filter)

    def onChangeColor(self, newColor):


        h, s, v = colorsys.rgb_to_hsv(newColor.red(), newColor.green(), newColor.blue())
        s = 1.0 #filter['saturation'] / 100
        r, g, b = colorsys.hsv_to_rgb(h, s, v)
        filteredColor = datatypes.Color(r, g, b) #utils.interpolateColor(newColor, self.black, self.finishTrigger.progress())
        log.l(self.type+'-filter color from '+str(newColor)+' to '+str(filteredColor))
        return filteredColor


class VolumeFadeFilter(Filter):
    """
    this filter affects the color changes in the corefunctions.fade() method.
    It changes the volume according to the progress percentage to 0% or 100%.
    """
    def __init__(self, filter): # takes a command as json encoded object
        if not config.ENABLE_XBMC_REMOTE:
            raise EnvironmentError(
                'config.ENABLE_XBMC_REMOTE needs to be enabled in order to send commands to your xbmc-service')
        super(VolumeFadeFilter, self).__init__(constants.FILTER_TYPE_VOLUMEFADE, filter)
        self.progress = filter['progress']
        self.limit = int(filter['limit'])
<commit_msg>~ fixed SaturationFilter<commit_after>#filters
#python modules
import time
import colorsys

#rgb-pi modules
import log
import constants
import datatypes
import utils
import server
import xbmcremote
import config

class Filter(object):
    """
    represents an abstract request to the server
    """
    def __init__(self, type, filter): # takes a filter type and a filter as json encoded object
        """
        initializes a new Request object
        @param type: type of the request (see protocol)
        @param request: JSON object with the request
        @return: instance of Request
        """
        self.active = True #TODO: not used yet
        self.filter = filter
        self.onfinish = filter['onfinish'] if filter.has_key('onfinish') else 'remove'
        self.type = type
        self.finishTrigger = self.finishTrigger = datatypes.Condition(filter['finish']) if filter.has_key('finish') else None
        if log.m(log.LEVEL_FILTERS): log.l('filter initialized: ' + self.type)


    def onChangeColor(self, newColor):
        """
        transforms the given color object
        @param newColor: color to be set as new color for the LEDs
        @return: transformed color according to the filter
        """
        return newColor

    def onFadeEnd(self, seconds, startColor, endColor):
        """
        called on the end of every fade
        @param seconds: time of the fade in seconds
        @param startColor: starting color of the fade
        @param endColor: ending color of the fade
        """
        pass

    def onFadeStep(self, seconds, startColor, endColor, progress):
        """
        called on the end of every fade
        @param seconds: time of the fade in seconds
        @param startColor: starting color of the fade
        @param endColor: ending color of the fade
        @param progress: progress of the fade as percent value between 0.0 and 1.0
        """
        pass

    def finish(self):
        if self.onfinish == constants.FILTER_ONFINISH_REMOVE:
            server.CurrentFilters.remove(self)

        if self.onfinish == constants.FILTER_ONFINISH_STOP:
            if server.CurrentCMD is not None:
                server.CurrentCMD.stop()
                server.CurrentCMD = None
                server.CurrentFilters = []
                if log.m(log.LEVEL_FILTER_ACTIONS): log.l(self.type+'-filter finished...')



    @staticmethod
    def createFilter(filter):
        if isinstance(filter, dict) and filter.has_key('type'):
            if filter['type'] == constants.FILTER_TYPE_DIM:
                return DimFilter(filter)
            if filter['type'] == constants.FILTER_TYPE_VOLUMEFADE:
                return VolumeFadeFilter(filter)
            if filter['type'] == constants.FILTER_TYPE_STOPMUSIC:
                return StopMusicFilter(filter)
            if filter['type'] == constants.FILTER_TYPE_SATURATION:
                return SaturationFilter(filter)


class DimFilter(Filter):
    """
    this filter affects the led.setColor(color)-method and dims all incoming colors according to the defined time
    """
    def __init__(self, filter): # takes a command as json encoded object
        super(DimFilter, self).__init__(constants.FILTER_TYPE_DIM, filter)
        self.black = datatypes.Color(0,0,0)

    def onChangeColor(self, newColor):
        if not self.finishTrigger.check():
            self.finish()

        filteredColor = utils.interpolateColor(newColor, self.black, self.finishTrigger.progress())
        if log.m(log.LEVEL_FILTER_ACTIONS): log.l(self.type+'-filter color ('+str(self.finishTrigger.progress()*100)+'%) from '+str(newColor)+' to '+str(filteredColor))
        return filteredColor


class SaturationFilter(Filter):
    """
    this filter affects the led.setColor(color)-method and sets a specific saturation
    """
    def __init__(self, filter): # takes a command as json encoded object
        super(SaturationFilter, self).__init__(constants.FILTER_TYPE_SATURATION, filter)
        self.saturation = filter['saturation']

    def onChangeColor(self, newColor):
        h, s, v = colorsys.rgb_to_hsv(newColor.red(), newColor.green(), newColor.blue())
        r, g, b = colorsys.hsv_to_rgb(h, self.saturation, v)
        filteredColor = datatypes.Color(r, g, b) #utils.interpolateColor(newColor, self.black, self.finishTrigger.progress())
        log.l(self.type+'-filter color from '+str(newColor)+' to '+str(filteredColor))
        return filteredColor


class VolumeFadeFilter(Filter):
    """
    this filter affects the color changes in the corefunctions.fade() method.
    It changes the volume according to the progress percentage to 0% or 100%.
    """
    def __init__(self, filter): # takes a command as json encoded object
        if not config.ENABLE_XBMC_REMOTE:
            raise EnvironmentError(
                'config.ENABLE_XBMC_REMOTE needs to be enabled in order to send commands to your xbmc-service')
        super(VolumeFadeFilter, self).__init__(constants.FILTER_TYPE_VOLUMEFADE, filter)
        self.progress = filter['progress']
        self.limit = int(filter['limit'])

{'fraction_non_alphanumeric': 0.09375611665688001, 'fraction_numerical': 0.0032295948326482677, 'mean_word_length': 2.770848708487085, 'pattern_counts': {'":': 0, '<': 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': '462519', 'n_tokens_mistral': 2837, 'n_tokens_neox': 2768, 'n_words': 870}