<commit_before># -*- encoding: utf-8 -*-
import itertools
import sys
from supriya.tools.systemtools.SupriyaObject import SupriyaObject


class ResponseManager(SupriyaObject):
    r'''Handles OSC responses from scsynth.

    ::

        >>> from supriya import osctools
        >>> from supriya import responsetools
        >>> manager = responsetools.ResponseManager

    ::

        >>> message = osctools.OscMessage(
        ...     '/status.reply', 1, 0, 0, 2, 4,
        ...     0.040679048746824265, 0.15118031203746796,
        ...     44100.0, 44100.00077873274,
        ...     )
        >>> manager.handle_message(message)
        StatusResponse(
            actual_sample_rate=44100.00077873274,
            average_cpu_usage=0.040679048746824265,
            group_count=2,
            peak_cpu_usage=0.15118031203746796,
            synth_count=0,
            synthdef_count=4,
            target_sample_rate=44100.0,
            ugen_count=0
            )

    ::

        >>> message = osctools.OscMessage('/b_info', 1100, 512, 1, 44100.0)
        >>> manager.handle_message(message)[0]
        BufferInfoResponse(
            buffer_id=1100,
            frame_count=512,
            channel_count=1,
            sample_rate=44100.0
            )

    ::

        >>> message = osctools.OscMessage('/n_set', 1023, '/one', -1, '/two', 0)
        >>> manager.handle_message(message)
        NodeSetResponse(
            node_id=1023,
            items=(
                NodeSetItem(
                    control_index_or_name='/one',
                    control_value=-1
                    ),
                NodeSetItem(
                    control_index_or_name='/two',
                    control_value=0
                    ),
                )
            )

    ::

        >>> message = osctools.OscMessage('/b_setn', 1, 0, 8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
        >>> manager.handle_message(message)
        BufferSetContiguousResponse(
            items=(
                BufferSetContiguousItem(
                    starting_sample_index=0,
                    sample_values=(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
                    ),
                ),
            buffer_id=1
            )

    ::

        >>> message = osctools.OscMessage('/g_queryTree.reply', 0, 0, 1, 1, 2, 1001, 0, 1000, 1, 1002, 0)
        >>> manager.handle_message(message)
        QueryTreeResponse(
            node_id=0,
            query_tree_group=QueryTreeGroup(
                node_id=0,
                children=(
                    QueryTreeGroup(
                        node_id=1,
                        children=(
                            QueryTreeGroup(
                                node_id=1001,
                                children=()
                                ),
                            QueryTreeGroup(
                                node_id=1000,
                                children=(
                                    QueryTreeGroup(
                                        node_id=1002,
                                        children=()
                                        ),
                                    )
                                ),
                            )
                        ),
                    )
                )
            )

    ::

        >>> print(manager.handle_message(message))
        NODE TREE 0 group
            1 group
                1001 group
                1000 group
                    1002 group

    '''

    ### PUBLIC METHODS ###

    @staticmethod
    def group_items(items, length):
        iterators = [iter(items)] * length
        if sys.version_info[0] == 2:
            iterator = itertools.izip(*iterators)
        else:
            iterator = zip(*iterators)
        return iterator

    @staticmethod
    def handle_message(message):
        address, contents = message.address, message.contents
        if address in _response_handlers:
            handler = _response_handlers[address]
            response = handler(address, contents)
            return response
        print('UNHANDLED:', message)

    @staticmethod
    def handle_b_info(command, contents):
        from supriya.tools import responsetools
        responses = []
        for group in ResponseManager.group_items(contents, 4):
            response = responsetools.BufferInfoResponse(*group)
            responses.append(response)
        responses = tuple(responses)<commit_msg>Silenced ResponseManager debugging.<commit_after># -*- encoding: utf-8 -*-
import itertools
import sys
from supriya.tools.systemtools.SupriyaObject import SupriyaObject


class ResponseManager(SupriyaObject):
    r'''Handles OSC responses from scsynth.

    ::

        >>> from supriya import osctools
        >>> from supriya import responsetools
        >>> manager = responsetools.ResponseManager

    ::

        >>> message = osctools.OscMessage(
        ...     '/status.reply', 1, 0, 0, 2, 4,
        ...     0.040679048746824265, 0.15118031203746796,
        ...     44100.0, 44100.00077873274,
        ...     )
        >>> manager.handle_message(message)
        StatusResponse(
            actual_sample_rate=44100.00077873274,
            average_cpu_usage=0.040679048746824265,
            group_count=2,
            peak_cpu_usage=0.15118031203746796,
            synth_count=0,
            synthdef_count=4,
            target_sample_rate=44100.0,
            ugen_count=0
            )

    ::

        >>> message = osctools.OscMessage('/b_info', 1100, 512, 1, 44100.0)
        >>> manager.handle_message(message)[0]
        BufferInfoResponse(
            buffer_id=1100,
            frame_count=512,
            channel_count=1,
            sample_rate=44100.0
            )

    ::

        >>> message = osctools.OscMessage('/n_set', 1023, '/one', -1, '/two', 0)
        >>> manager.handle_message(message)
        NodeSetResponse(
            node_id=1023,
            items=(
                NodeSetItem(
                    control_index_or_name='/one',
                    control_value=-1
                    ),
                NodeSetItem(
                    control_index_or_name='/two',
                    control_value=0
                    ),
                )
            )

    ::

        >>> message = osctools.OscMessage('/b_setn', 1, 0, 8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
        >>> manager.handle_message(message)
        BufferSetContiguousResponse(
            items=(
                BufferSetContiguousItem(
                    starting_sample_index=0,
                    sample_values=(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
                    ),
                ),
            buffer_id=1
            )

    ::

        >>> message = osctools.OscMessage('/g_queryTree.reply', 0, 0, 1, 1, 2, 1001, 0, 1000, 1, 1002, 0)
        >>> manager.handle_message(message)
        QueryTreeResponse(
            node_id=0,
            query_tree_group=QueryTreeGroup(
                node_id=0,
                children=(
                    QueryTreeGroup(
                        node_id=1,
                        children=(
                            QueryTreeGroup(
                                node_id=1001,
                                children=()
                                ),
                            QueryTreeGroup(
                                node_id=1000,
                                children=(
                                    QueryTreeGroup(
                                        node_id=1002,
                                        children=()
                                        ),
                                    )
                                ),
                            )
                        ),
                    )
                )
            )

    ::

        >>> print(manager.handle_message(message))
        NODE TREE 0 group
            1 group
                1001 group
                1000 group
                    1002 group

    '''

    ### PUBLIC METHODS ###

    @staticmethod
    def group_items(items, length):
        iterators = [iter(items)] * length
        if sys.version_info[0] == 2:
            iterator = itertools.izip(*iterators)
        else:
            iterator = zip(*iterators)
        return iterator

    @staticmethod
    def handle_message(message):
        address, contents = message.address, message.contents
        if address in _response_handlers:
            handler = _response_handlers[address]
            response = handler(address, contents)
            return response
        #print('UNHANDLED:', message)

    @staticmethod
    def handle_b_info(command, contents):
        from supriya.tools import responsetools
        responses = []
        for group in ResponseManager.group_items(contents, 4):
            response = responsetools.BufferInfoResponse(*group)
            responses.append(response)
        responses = tuple(responses)
{'fraction_non_alphanumeric': 0.09516397998888271, 'fraction_numerical': 0.05736520289049472, 'mean_word_length': 1.1952171791117618, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2971682', 'n_tokens_mistral': 2751, 'n_tokens_neox': 2332, 'n_words': 417}