<commit_before># -*- coding: utf-8 -*-

from __future__ import unicode_literals
from django.utils.translation import ugettext_lazy as _
from cms.plugin_pool import plugin_pool

from ..models import SegmentLimitPluginModel
from .segment_plugin_base import SegmentPluginBase


class SegmentLimitPlugin(SegmentPluginBase):
    '''
    This is a special SegmentPlugin that acts as a top-level container for
    segmentation plugins and can set an upper-limit to the number of children
    that will be rendered in the current context.
    '''

    allow_children = True
    model = SegmentLimitPluginModel
    module = _('Segmentation')
    name = _('Limit Block')
    parent_classes = None
    render_template = 'segmentation/_limiter.html'

    allow_overrides = False

    def render(self, context, instance, placeholder):
        context = super(SegmentLimitPlugin, self).render(
            context, instance, placeholder)
        context['child_plugins'] = self.get_context_appropriate_children(
            context, instance)
        return context


    def is_context_appropriate(self, context, instance):
        '''
        Returns True if any of its children are context-appropriate,
        else False.
        '''
        apt_children = self.get_context_appropriate_children(context, instance)
        num_apt = sum( 1 for child in apt_children if child[1] )
        return num_apt > 0


    def get_context_appropriate_children(self, context, instance):
        from ..segment_pool import SegmentOverride
        '''
        Returns a LIST OF TUPLES each containing a child plugin instance and a
        Boolean representing the plugin's appropriateness for rendering in
        this context.
        '''

        children = []
        render_all = (instance.max_children == 0)
        slots_remaining = instance.max_children

        for child_instance in instance.child_plugin_instances:

            child_plugin = child_instance.get_plugin_class_instance()

            if render_all or slots_remaining > 0:
                #
                # We're allowing all 'ducks' here, but it may make sense to
                # instead use isinstance(...)
                #
                if hasattr(child_plugin, 'is_context_appropriate'):
                    #
                    # This is a segment plugin... or at least quacks like one.
                    #
                    if (hasattr(child_plugin, 'allow_overrides') and
                            child_plugin.allow_overrides and
                            hasattr(child_plugin, 'get_segment_override')):

                        override = child_plugin.get_segment_override(
                            context, child_instance)
<commit_msg>comments/whitespace<commit_after># -*- coding: utf-8 -*-

from __future__ import unicode_literals
from django.utils.translation import ugettext_lazy as _
from cms.plugin_pool import plugin_pool

from ..models import SegmentLimitPluginModel
from .segment_plugin_base import SegmentPluginBase


class SegmentLimitPlugin(SegmentPluginBase):
    '''
    This is a special SegmentPlugin that acts as a top-level container for
    segmentation plugins and can set an upper-limit to the number of children
    that will be rendered in the current context.
    '''

    allow_children = True
    model = SegmentLimitPluginModel
    module = _('Segmentation')
    name = _('Limit Block')
    parent_classes = None
    render_template = 'segmentation/_limiter.html'

    allow_overrides = False

    def render(self, context, instance, placeholder):
        context = super(SegmentLimitPlugin, self).render(
            context, instance, placeholder)
        context['child_plugins'] = self.get_context_appropriate_children(
            context, instance)
        return context


    def is_context_appropriate(self, context, instance):
        '''
        Returns True if any of its children are context-appropriate,
        else False.
        '''
        apt_children = self.get_context_appropriate_children(context, instance)
        num_apt = sum( 1 for child in apt_children if child[1] )
        return num_apt > 0


    def get_context_appropriate_children(self, context, instance):
        from ..segment_pool import SegmentOverride
        '''
        Returns a LIST OF TUPLES each containing a child plugin instance and a
        Boolean representing the plugin's appropriateness for rendering in
        this context.
        '''

        children = []
        render_all = (instance.max_children == 0)
        slots_remaining = instance.max_children

        for child_instance in instance.child_plugin_instances:

            child_plugin = child_instance.get_plugin_class_instance()

            if render_all or slots_remaining > 0:

                if hasattr(child_plugin, 'is_context_appropriate'):
                    #
                    # This quacks like a segment plugin...
                    #
                    if (hasattr(child_plugin, 'allow_overrides') and
                            child_plugin.allow_overrides and
                            hasattr(child_plugin, 'get_segment_override')):

                        override = child_plugin.get_segment_override(
                            context, child_instance)

{'fraction_non_alphanumeric': 0.06486589309492105, 'fraction_numerical': 0.002282670724747955, 'mean_word_length': 2.3278481012658228, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '966835', 'n_tokens_mistral': 1342, 'n_tokens_neox': 1283, 'n_words': 397}