<commit_before>from __future__ import absolute_import

from . import constants
from ..utils import camel_dictionary


class Widget():
    """Widget

    Args:
        data (dict): The widget definition for a layer. It contains the information to render a widget:
            `type`: 'default', 'formula', time-series', 'animation', 'category', 'histogram'
            `value`: A constant value or a CARTO VL expression
            `options`: Options for the widget, this varies depending on the widget

            The widget also can display text information: `title`, `description` and `footer`.
    Example:

    .. code::

        from cartoframes.viz import Widget

        Widget('formula', value='viewportSum($amount)', title='...')
    """

    def __init__(self, f_arg, **kwargs):
        if isinstance(f_arg, dict):
            self._init_widget(f_arg)
        elif isinstance(f_arg, str):
            self._init_widget(kwargs, f_arg)
        else:
            raise ValueError('Wrong widget input.')

    def _init_widget(self, data, widget_type=''):
        self._type = None
        self._value = None
        self._title = None
        self._prop = None
        self._description = None
        self._footer = None
        self._options = {}

        if data is not None:
            self._type = widget_type if widget_type else data.get('type', None)
            self._value = data.get('value', None)
            self._title = data.get('title', None)
            self._description = data.get('description', None)
            self._footer = data.get('footer', None)
            self._options = camel_dictionary(data.get('options', {}))
            self._prop = self.get_default_prop(data)
        else:
            raise ValueError('Wrong widget input.')

    def get_default_prop(self, data):
        prop = data.get('prop', None)
        return 'filter' if self._type == 'animation' and not prop else prop

    def get_info(self):
        if self._type or self._title or self._description or self._footer:
            self._check_type()

            return {
                'type': self._type,
                'prop': self._prop,
                'value': self._value,
                'title': self._title,
                'description': self._description,
                'footer': self._footer,
                'has_bridge': self.has_bridge(),
                'options': self._options
            }
        else:
            return {}

    def has_bridge(self):
        return self._type not in ('formula', 'default')

    def _check_type(self):
        if self._type and self._type not in constants.WIDGET_TYPES:
            raise ValueError(
                'Widget type is not valid. Valid widget types are: {}.'.format(
                    ', '.join(constants.WIDGET_TYPES)
                ))<commit_msg>Revert setting default to None<commit_after>from __future__ import absolute_import

from . import constants
from ..utils import camel_dictionary


class Widget():
    """Widget

    Args:
        data (dict): The widget definition for a layer. It contains the information to render a widget:
            `type`: 'default', 'formula', time-series', 'animation', 'category', 'histogram'
            `value`: A constant value or a CARTO VL expression
            `options`: Options for the widget, this varies depending on the widget

            The widget also can display text information: `title`, `description` and `footer`.
    Example:

    .. code::

        from cartoframes.viz import Widget

        Widget('formula', value='viewportSum($amount)', title='...')
    """

    def __init__(self, f_arg, **kwargs):
        if isinstance(f_arg, dict):
            self._init_widget(f_arg)
        elif isinstance(f_arg, str):
            self._init_widget(kwargs, f_arg)
        else:
            raise ValueError('Wrong widget input.')

    def _init_widget(self, data, widget_type=''):
        self._type = ''
        self._value = ''
        self._title = ''
        self._prop = ''
        self._description = ''
        self._footer = ''
        self._options = {}

        if data is not None:
            self._type = widget_type if widget_type else data.get('type', '')
            self._value = data.get('value', '')
            self._title = data.get('title', '')
            self._description = data.get('description', '')
            self._footer = data.get('footer', '')
            self._options = camel_dictionary(data.get('options', {}))
            self._prop = self.get_default_prop(data)
        else:
            raise ValueError('Wrong widget input.')

    def get_default_prop(self, data):
        prop = data.get('prop', '')
        return 'filter' if self._type == 'animation' and not prop else prop

    def get_info(self):
        if self._type or self._title or self._description or self._footer:
            self._check_type()

            return {
                'type': self._type,
                'prop': self._prop,
                'value': self._value,
                'title': self._title,
                'description': self._description,
                'footer': self._footer,
                'has_bridge': self.has_bridge(),
                'options': self._options
            }
        else:
            return {}

    def has_bridge(self):
        return self._type not in ('formula', 'default')

    def _check_type(self):
        if self._type and self._type not in constants.WIDGET_TYPES:
            raise ValueError(
                'Widget type is not valid. Valid widget types are: {}.'.format(
                    ', '.join(constants.WIDGET_TYPES)
                ))
{'fraction_non_alphanumeric': 0.1137289636846767, 'fraction_numerical': 0.0, 'mean_word_length': 1.945226917057903, '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': '3678791', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1472, 'n_words': 436}