<commit_before>'''blox/base.py

The base for all blox

Copyright (C) 2015  <NAME>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

'''
from collections import OrderedDict
import re
from itertools import chain

from connectable import Connectable
from blox.attributes import (AbstractAttribute, Attribute, RenderedDirect, SetAttribute,
                             BooleanAttribute, IntegerAttribute, DirectAttribute, BlokAttribute,
                             AccessorAttribute)

from io import StringIO

UNDERSCORE = (re.compile('(.)([A-Z][a-z]+)'), re.compile('([a-z0-9])([A-Z])'))


class TagAttributes(type):
    '''A meta class to automatically register signals for tag attributes'''

    def __new__(metaclass, name, parents, class_dict, *kargs, **kwargs):
        '''Updates a tag class to automatically register all signals'''
        attributes = {name: attribute for name, attribute in class_dict.items() if isinstance(attribute,
                                                                                              AbstractAttribute)}
        if attributes:
            if hasattr(parents[0], 'attribute_descriptors'):
                full_attributes = parents[0].attribute_descriptors.copy()
                full_attributes.update(attributes)
                attributes = full_attributes

            blok_attributes = {}
            render_attributes = []
            direct_attributes = []
            init_attributes = []
            accessor_attributes = []
            for attribute_name, attribute in attributes.items():
                if not hasattr(attribute, 'name'):
                    attribute.name = attribute_name
                if isinstance(attribute, DirectAttribute):
                    direct_attributes.append(attribute)
                    if hasattr(attribute, 'render'):
                        render_attributes.append(attribute)
                    if not hasattr(attribute, 'object_attribute'):
                        attribute.object_attribute = '_{0}'.format(attribute_name)
                    if getattr(attribute, 'init', False):
                        init_attributes.append(attribute_name)
                    if isinstance(attribute, BlokAttribute) and hasattr(attribute.type, 'tag'):
                        blok_attributes[attribute.type.tag] = attribute
                if isinstance(attribute, AccessorAttribute):
                    accessor_attributes.append(attribute)
                    if not hasattr(attribute, 'parent_attribute'):
                        attribute.parent_attribute = '_{0}_parent'.format(attribute_name)

            if direct_attributes and not name == 'AbstractTag' and '__slots__' in class_dict:
                class_dict['__slots__'] += tuple(attribute.object_attribute for attribute in direct_attributes)
                class_dict['__slots__'] += tuple(attribute.parent_attribute for attribute in accessor_attributes)

            if render_attributes:
                if hasattr(parents[0], 'render_attributes'):
                    render_attributes = list(parents[0].render_attributes) + render_attributes
                class_dict['render_attributes'] = set(render_attributes)

            if init_attributes:
                if hasattr(parents[0], 'init_attributes'):
                    init_attributes = list(parents[0].init_attributes) + init_attributes
                class_dict['init_attributes'] = init_attributes

            if blok_attributes:
                if hasattr(parents[0], 'blok_attributes'):
                    full_blok_attributes = dict(parents[0].blok_attributes)
                    full_blok_attributes.update(blok_attributes)
                    blok_attributes = full_blok_attributes
                class_dict['blok_attributes'] = blok_attributes

            class_dict['attribute_descriptors'] = attributes
            attribute_signals = (attribute.signal for attribute in attributes.values() if getattr(attribute, 'signal'))
            if attribute_signals:
                class_dict['signals'] = class_dict.get('signals', ()) + tuple(attribute_signals)

        return super(TagAttributes, metaclass).__new__(metaclass, name, parents, class_dict, *kargs, **kwargs)


class Blok(Connectable, metaclass=TagAttributes):
    '''Defines the base blox blok object which can render itself and be instanciated'''
    __slots__ = ()

    def output(self, to=None, *args, **kwargs):
        '''Outputs to a stream (like a file or request)'''
        to.write('')

    def render(self, *args, **kwargs):
        '''Renders as a str'''
        render_to = StringIO()
        self.output(render_to, *args, **kwargs)
        return render_to.getvalue()

    def __str__(self):
        return self.render(formatted=True)

    def __repr_self__(self, identifiers=()):
        return "{0}({1})".format(self.__class__.__name__, " ".join(identifiers))

    def __repr__(self):
        return self.__repr_self__()


class Invalid(Blok):
    '''Defines how the lack of a vaild Blok should be rendered'''
    __slots__ = ()

    def output(self, to=None, *args, **kwargs):
        to.write('<h2>Invalid</h2>')


class Blox(Blok):
    '''A Block that can contain child blocks'''
    __slots__ = ('_blox', )

    def __init__(self, *blox):
        super().__init__()
        if hasattr(self, 'init_attributes'):
            for attribute_name in self.init_attributes:
                getattr(self, attribute_name)
        for blok in blox:
            self(blok)

    @property
    def blox_container(self):
        '''Returns the container that should be responsible adding children, outside of init'''
        return self

    @property
    def blox(self):
        '''Lazily creates and returns the list of child blox'''
        if not hasattr(self, '_blox'):
            self._blox = []
        return self._blox

    def __call__(self, blok, position=None):
        '''Adds a nested blok to this blok'''
        if position is not None:
            self.blox_container.blox.insert(position, blok)
        else:
            self.blox_container.blox.append(blok)
        return blok

    def __iter__(self):
        return self.blox_container.blox.__iter__()

    def __contains__(self, blok):
        return blok in self.blox_container.blox<commit_msg>Update __call__ to support multiple elements being added at once<commit_after>'''blox/base.py

The base for all blox

Copyright (C) 2015  <NAME>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

'''
from collections import OrderedDict
import re
from itertools import chain

from connectable import Connectable
from blox.attributes import (AbstractAttribute, Attribute, RenderedDirect, SetAttribute,
                             BooleanAttribute, IntegerAttribute, DirectAttribute, BlokAttribute,
                             AccessorAttribute)

from io import StringIO

UNDERSCORE = (re.compile('(.)([A-Z][a-z]+)'), re.compile('([a-z0-9])([A-Z])'))


class TagAttributes(type):
    '''A meta class to automatically register signals for tag attributes'''

    def __new__(metaclass, name, parents, class_dict, *kargs, **kwargs):
        '''Updates a tag class to automatically register all signals'''
        attributes = {name: attribute for name, attribute in class_dict.items() if isinstance(attribute,
                                                                                              AbstractAttribute)}
        if attributes:
            if hasattr(parents[0], 'attribute_descriptors'):
                full_attributes = parents[0].attribute_descriptors.copy()
                full_attributes.update(attributes)
                attributes = full_attributes

            blok_attributes = {}
            render_attributes = []
            direct_attributes = []
            init_attributes = []
            accessor_attributes = []
            for attribute_name, attribute in attributes.items():
                if not hasattr(attribute, 'name'):
                    attribute.name = attribute_name
                if isinstance(attribute, DirectAttribute):
                    direct_attributes.append(attribute)
                    if hasattr(attribute, 'render'):
                        render_attributes.append(attribute)
                    if not hasattr(attribute, 'object_attribute'):
                        attribute.object_attribute = '_{0}'.format(attribute_name)
                    if getattr(attribute, 'init', False):
                        init_attributes.append(attribute_name)
                    if isinstance(attribute, BlokAttribute) and hasattr(attribute.type, 'tag'):
                        blok_attributes[attribute.type.tag] = attribute
                if isinstance(attribute, AccessorAttribute):
                    accessor_attributes.append(attribute)
                    if not hasattr(attribute, 'parent_attribute'):
                        attribute.parent_attribute = '_{0}_parent'.format(attribute_name)

            if direct_attributes and not name == 'AbstractTag' and '__slots__' in class_dict:
                class_dict['__slots__'] += tuple(attribute.object_attribute for attribute in direct_attributes)
                class_dict['__slots__'] += tuple(attribute.parent_attribute for attribute in accessor_attributes)

            if render_attributes:
                if hasattr(parents[0], 'render_attributes'):
                    render_attributes = list(parents[0].render_attributes) + render_attributes
                class_dict['render_attributes'] = set(render_attributes)

            if init_attributes:
                if hasattr(parents[0], 'init_attributes'):
                    init_attributes = list(parents[0].init_attributes) + init_attributes
                class_dict['init_attributes'] = init_attributes

            if blok_attributes:
                if hasattr(parents[0], 'blok_attributes'):
                    full_blok_attributes = dict(parents[0].blok_attributes)
                    full_blok_attributes.update(blok_attributes)
                    blok_attributes = full_blok_attributes
                class_dict['blok_attributes'] = blok_attributes

            class_dict['attribute_descriptors'] = attributes
            attribute_signals = (attribute.signal for attribute in attributes.values() if getattr(attribute, 'signal'))
            if attribute_signals:
                class_dict['signals'] = class_dict.get('signals', ()) + tuple(attribute_signals)

        return super(TagAttributes, metaclass).__new__(metaclass, name, parents, class_dict, *kargs, **kwargs)


class Blok(Connectable, metaclass=TagAttributes):
    '''Defines the base blox blok object which can render itself and be instanciated'''
    __slots__ = ()

    def output(self, to=None, *args, **kwargs):
        '''Outputs to a stream (like a file or request)'''
        to.write('')

    def render(self, *args, **kwargs):
        '''Renders as a str'''
        render_to = StringIO()
        self.output(render_to, *args, **kwargs)
        return render_to.getvalue()

    def __str__(self):
        return self.render(formatted=True)

    def __repr_self__(self, identifiers=()):
        return "{0}({1})".format(self.__class__.__name__, " ".join(identifiers))

    def __repr__(self):
        return self.__repr_self__()


class Invalid(Blok):
    '''Defines how the lack of a vaild Blok should be rendered'''
    __slots__ = ()

    def output(self, to=None, *args, **kwargs):
        to.write('<h2>Invalid</h2>')


class Blox(Blok):
    '''A Block that can contain child blocks'''
    __slots__ = ('_blox', )

    def __init__(self, *blox):
        super().__init__()
        if hasattr(self, 'init_attributes'):
            for attribute_name in self.init_attributes:
                getattr(self, attribute_name)
        for blok in blox:
            self(blok)

    @property
    def blox_container(self):
        '''Returns the container that should be responsible adding children, outside of init'''
        return self

    @property
    def blox(self):
        '''Lazily creates and returns the list of child blox'''
        if not hasattr(self, '_blox'):
            self._blox = []
        return self._blox

    def __call__(self, *blox, position=None):
        '''Adds a nested blok to this blok'''
        if position is not None:
            for blok in blox:
                self.blox_container.blox.insert(position, blok)
        else:
            for blok in blox:
                self.blox_container.blox.append(blok)
        return blok

    def __iter__(self):
        return self.blox_container.blox.__iter__()

    def __contains__(self, blok):
        return blok in self.blox_container.blox
{'fraction_non_alphanumeric': 0.08861365813603395, 'fraction_numerical': 0.0027371014096072258, 'mean_word_length': 2.533607350096712, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1114179', 'n_tokens_mistral': 3781, 'n_tokens_neox': 3618, 'n_words': 1144}