<commit_before># %FILEHEADER%
import gtk
from gpyconf.mvc import MVCComponent

from .utils import *

def get_widget_for_field(field, mapping=None):
    """
    Looks up for a corresponding :class:`Widget` in ``mapping``
    (Uses the module-default :attr:`WIDGET_MAP` if ``mapping`` is :const:`None`).
    Raises :exc:`KeyError` if no such widget is defined.
    """
    if mapping is None:
        mapping = WIDGET_MAP
    if field._class_name in mapping:
        return mapping[field._class_name](field)
    else:
        raise NotImplementedError("No widget defined for '%s' field. "
            "You could extend the `gpyconf.frontends._gtk.WIDGET_MAP` dict "
            "with your own implementation of a widget for this field "
            "(inherit from `gpyconf.frontends._gtk.Widget`)." % field)


class Widget(MVCComponent):
    """
    Abstract base class for all widgets.

    A widget is used to "display" a field within the window.
    """
    __events__ = ('value-changed',)
    _changed_signal = 'changed'

    def __init__(self, field):
        MVCComponent.__init__(self)

        self.widget = self.gtk_widget()
        self.field_var = field.field_var
        self.label = field.label
        self.label2 = field.label2
        self.widget.connect(self._changed_signal, self.on_value_changed)
        self.initialize()
        self.value = field.value

    def initialize(self):
        """
        Called after initialition. Widgets can to stuff like additional method
        calls here.
        """
        pass

    def on_value_changed(self, sender):
        """
        Value of the :class:`gtk.Widget` changed (mostly through the end user).

        Emits :signal:`value-changed` with :attr:`value` as parameter.
        """
        self.emit('value-changed', self.value)
        log_msg = "Value of %s '%s' changed to '%s'" % (self._class_name,
                                                        self.field_var,
                                                        self.value)
        self.emit('log', log_msg, level='info')

    def get_value(self):
        """
        Returns the widget's value. If the :attr:`prop` attribute is defined,
        tries to return the corresponding :attr:`gtk.Widget.props` attribute.

        If not, :exc:`NotImplementedError` is raised (so this method has to be
        overwritten in that case).
        """
        if hasattr(self, 'prop'):
            return getattr(self.widget.props, self.prop)
        else:
            raise NotImplementedError()

    def set_value(self, value):
        """
        Set the widget's value to ``value``. If the :attr:`prop` attribute is
        defined, tries to set the corresponding :attr:`gtk.Widget.props`
        attribute.

        If not, :exc:`NotImplementedError` is raised (so this method has to be
        overwritten in that case).
        """
        if hasattr(self, 'prop'):
            setattr(self.widget.props, self.prop, value)
        else:
            raise NotImplementedError()

    def to_gtk(self, value):
        """ Converts ``value`` to gtk compatible value """
        return value

    def to_python(self, value):
        """ Converts ``value`` to a standard python value """
        return value

    def _set_value(self, value):
        self.set_value(self.to_gtk(value))

    def _get_value(self):
        return self.to_python(self.get_value())

    #: The current value
    value = property(_get_value, _set_value)


# DEFAULT WIDGETS AND CONTAINERS

class BooleanWidget(Widget):
    gtk_widget = gtk.CheckButton
    prop = 'active'
    _changed_signal = 'toggled'

    def initialize(self):
        self.widget.set_label(self.label)
        self.label = None
        self.label2 = None

class NumberWidget(Widget):
    gtk_widget = gtk.SpinButton
    _changed_signal = 'value-changed'
    prop = 'value'

    def __init__(self, field):
        Widget.__init__(self, field)
        self.widget.set_range(float(field.min), float(field.max))

class FloatingPointNumberWidget(NumberWidget):
    def __init__(self, field, decimals=None):
        NumberWidget.__init__(self, field)
        self.widget.set_digits(2)
        self.widget.set_increments(0.01, self.widget.get_increments()[0])

        self.value = field.value
        # setting the value doesn't work before the range/digits setup

class IntegerWidget(NumberWidget):
    def __init__(self, field):
        NumberWidget.__init__(self, field)
        self.widget.set_digits(0)
        self.widget.set_increments(1, self.widget.get_increments()[0])

class CharWidget(Widget):
    gtk_widget = gtk.Entry
    prop = 'text'<commit_msg>Set value for IntegerWidget<commit_after># %FILEHEADER%
import gtk
from gpyconf.mvc import MVCComponent

from .utils import *

def get_widget_for_field(field, mapping=None):
    """
    Looks up for a corresponding :class:`Widget` in ``mapping``
    (Uses the module-default :attr:`WIDGET_MAP` if ``mapping`` is :const:`None`).
    Raises :exc:`KeyError` if no such widget is defined.
    """
    if mapping is None:
        mapping = WIDGET_MAP
    if field._class_name in mapping:
        return mapping[field._class_name](field)
    else:
        raise NotImplementedError("No widget defined for '%s' field. "
            "You could extend the `gpyconf.frontends._gtk.WIDGET_MAP` dict "
            "with your own implementation of a widget for this field "
            "(inherit from `gpyconf.frontends._gtk.Widget`)." % field)


class Widget(MVCComponent):
    """
    Abstract base class for all widgets.

    A widget is used to "display" a field within the window.
    """
    __events__ = ('value-changed',)
    _changed_signal = 'changed'

    def __init__(self, field):
        MVCComponent.__init__(self)

        self.widget = self.gtk_widget()
        self.field_var = field.field_var
        self.label = field.label
        self.label2 = field.label2
        self.widget.connect(self._changed_signal, self.on_value_changed)
        self.initialize()
        self.value = field.value

    def initialize(self):
        """
        Called after initialition. Widgets can to stuff like additional method
        calls here.
        """
        pass

    def on_value_changed(self, sender):
        """
        Value of the :class:`gtk.Widget` changed (mostly through the end user).

        Emits :signal:`value-changed` with :attr:`value` as parameter.
        """
        self.emit('value-changed', self.value)
        log_msg = "Value of %s '%s' changed to '%s'" % (self._class_name,
                                                        self.field_var,
                                                        self.value)
        self.emit('log', log_msg, level='info')

    def get_value(self):
        """
        Returns the widget's value. If the :attr:`prop` attribute is defined,
        tries to return the corresponding :attr:`gtk.Widget.props` attribute.

        If not, :exc:`NotImplementedError` is raised (so this method has to be
        overwritten in that case).
        """
        if hasattr(self, 'prop'):
            return getattr(self.widget.props, self.prop)
        else:
            raise NotImplementedError()

    def set_value(self, value):
        """
        Set the widget's value to ``value``. If the :attr:`prop` attribute is
        defined, tries to set the corresponding :attr:`gtk.Widget.props`
        attribute.

        If not, :exc:`NotImplementedError` is raised (so this method has to be
        overwritten in that case).
        """
        if hasattr(self, 'prop'):
            setattr(self.widget.props, self.prop, value)
        else:
            raise NotImplementedError()

    def to_gtk(self, value):
        """ Converts ``value`` to gtk compatible value """
        return value

    def to_python(self, value):
        """ Converts ``value`` to a standard python value """
        return value

    def _set_value(self, value):
        self.set_value(self.to_gtk(value))

    def _get_value(self):
        return self.to_python(self.get_value())

    #: The current value
    value = property(_get_value, _set_value)


# DEFAULT WIDGETS AND CONTAINERS

class BooleanWidget(Widget):
    gtk_widget = gtk.CheckButton
    prop = 'active'
    _changed_signal = 'toggled'

    def initialize(self):
        self.widget.set_label(self.label)
        self.label = None
        self.label2 = None

class NumberWidget(Widget):
    gtk_widget = gtk.SpinButton
    _changed_signal = 'value-changed'
    prop = 'value'

    def __init__(self, field):
        Widget.__init__(self, field)
        self.widget.set_range(float(field.min), float(field.max))

class FloatingPointNumberWidget(NumberWidget):
    def __init__(self, field, decimals=None):
        NumberWidget.__init__(self, field)
        self.widget.set_digits(2)
        self.widget.set_increments(0.01, self.widget.get_increments()[0])

        self.value = field.value
        # setting the value doesn't work before the range/digits setup

class IntegerWidget(NumberWidget):
    def __init__(self, field):
        NumberWidget.__init__(self, field)
        self.widget.set_digits(0)
        self.widget.set_increments(1, self.widget.get_increments()[0])

        self.value = field.value

class CharWidget(Widget):
    gtk_widget = gtk.Entry
    prop = 'text'
{'fraction_non_alphanumeric': 0.10630765930057927, 'fraction_numerical': 0.0023600085818493884, 'mean_word_length': 2.4175219941348973, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4129749', 'n_tokens_mistral': 2733, 'n_tokens_neox': 2651, 'n_words': 736}