<commit_before>"""
Commonly needed form widgets.
"""

__all__ = ['Input', 'Password', 'CheckedPassword', 'Hidden', 'TextArea',
        'Checkbox', 'DateParts', 'FileUpload', 'SelectChoice', 'RadioChoice',
        'CheckboxMultiChoice']

from formish.converter import *
from formish.validation import *


UNSET = object()


class Widget(object):
    
    def __init__(self,**k):
        self.converter_options = k.get('converter_options',{})
        if not self.converter_options.has_key('delimiter'):
            self.converter_options['delimiter'] = ','
    
    def pre_render(self, schemaType, data):
        return [string_converter(schemaType).fromType(data)]

    # ??: Should the validate be here? Confused
    def validate(self, data):
        errors = None
        return data, errors

    def convert(self, schemaType, data):
        return string_converter(schemaType).toType(data[0])


class Input(Widget):
    def __init__(self,**k):
        Widget.__init__(self, **k)
        if not self.converter_options.has_key('delimiter'):
            self.converter_options['delimiter'] = ','
   

class Password(Widget):
    pass

   
class CheckedPassword(Widget):
    
    def pre_render(self, schemaType, data):
        password = string_converter(schemaType).fromType(data)
        return {'password': [password], 'confirm': [password]}
    
    def convert(self, schemaType, data):
        password = data.get('password',[None])[0]
        confirm = data.get('confirm',[None])[0]
        if password != confirm:
            raise FieldValidationError('Password did not match')
        return string_converter(schemaType).toType(password)


class Hidden(Widget):
    
    def pre_render(self, schemaType, data):
        return [string_converter(schemaType).fromType(data)]
    
    def convert(self, schemaType, data):
        return string_converter(schemaType).toType(data[0])

        
class TextArea(Widget):
    
    def __init__(self, **k):
        self.cols = k.pop('cols', None)
        self.rows = k.pop('rows', None)
        Widget.__init__(self, **k)
        if not self.converter_options.has_key('delimiter'):
            self.converter_options['delimiter'] = '\n'
    
    def pre_render(self, schemaType, data):
        return [string_converter(schemaType).fromType(data, converter_options=self.converter_options)]
    
    def convert(self, schemaType, data):
        return string_converter(schemaType).toType(data[0], converter_options=self.converter_options)

    
class Checkbox(Widget):

    def pre_render(self, schemaType, data):
        return [string_converter(schemaType).fromType(data)]
    
    def convert(self, schemaType, data):
        if len(data) == 0:
            out='False'
        else:
            out='True'
        return string_converter(schemaType).toType(out)

    
class DateParts(Widget):
    
    def __init__(self, dayFirst=False):
        self.dayFirst = dayFirst
        
    def pre_render(self, schemaType, data):
        data = datetuple_converter(schemaType).fromType(data)
        d = {}
        d['year'] = [data[0]]
        d['month'] = [data[1]]
        d['day'] = [data[2]]
        return d
    
    def convert(self, schemaType, data):<commit_msg>added strip option to inputs, passwords and textareas<commit_after>"""
Commonly needed form widgets.
"""

__all__ = ['Input', 'Password', 'CheckedPassword', 'Hidden', 'TextArea',
        'Checkbox', 'DateParts', 'FileUpload', 'SelectChoice', 'RadioChoice',
        'CheckboxMultiChoice']

from formish.converter import *
from formish.validation import *


UNSET = object()


class Widget(object):
    
    def __init__(self,**k):
        self.converter_options = k.get('converter_options',{})
        if not self.converter_options.has_key('delimiter'):
            self.converter_options['delimiter'] = ','
    
    def pre_render(self, schemaType, data):
        return [string_converter(schemaType).fromType(data)]

    # ??: Should the validate be here? Confused
    def validate(self, data):
        errors = None
        return data, errors

    def convert(self, schemaType, data):
        return string_converter(schemaType).toType(data[0])


class Input(Widget):
    def __init__(self,**k):
        self.strip = k.pop('strip', True)
        Widget.__init__(self, **k)
        if not self.converter_options.has_key('delimiter'):
            self.converter_options['delimiter'] = ','
            
    def convert(self, schemaType, data):
        if self.strip is True:
            d = data[0].strip()
        else:
            d = data[0]
        return string_converter(schemaType).toType(d)            

class Password(Widget):
    def __init__(self,**k):
        self.strip = k.pop('strip', True)
        Widget.__init__(self, **k)
        if not self.converter_options.has_key('delimiter'):
            self.converter_options['delimiter'] = ','
            
    def convert(self, schemaType, data):
        if self.strip is True:
            d = data[0].strip()
        else:
            d = data[0]
        return string_converter(schemaType).toType(d)

   
class CheckedPassword(Widget):

    def __init__(self,**k):
        self.strip = k.pop('strip', True)
        Widget.__init__(self, **k)
        if not self.converter_options.has_key('delimiter'):
            self.converter_options['delimiter'] = ','
            
    def pre_render(self, schemaType, data):
        password = string_converter(schemaType).fromType(data)
        return {'password': [password], 'confirm': [password]}
    
    def convert(self, schemaType, data):
        password = data.get('password',[None])[0]
        confirm = data.get('confirm',[None])[0]
        if self.strip is True:
            password = password.strip()
            confirm = confirm.strip()
        if password != confirm:
            raise FieldValidationError('Password did not match')
        return string_converter(schemaType).toType(password)


class Hidden(Widget):
    
    def pre_render(self, schemaType, data):
        return [string_converter(schemaType).fromType(data)]
    
    def convert(self, schemaType, data):
        return string_converter(schemaType).toType(data[0])

        
class TextArea(Widget):
    
    def __init__(self, **k):
        self.cols = k.pop('cols', None)
        self.rows = k.pop('rows', None)
        self.strip = k.pop('strip', True)
        Widget.__init__(self, **k)
        if not self.converter_options.has_key('delimiter'):
            self.converter_options['delimiter'] = '\n'
    
    def pre_render(self, schemaType, data):
        return [string_converter(schemaType).fromType(data, converter_options=self.converter_options)]
    
    def convert(self, schemaType, data):
        if self.strip is True:
            d = data[0].strip()
        else:
            d = data[0]
        return string_converter(schemaType).toType(d, converter_options=self.converter_options)

    
class Checkbox(Widget):

    def pre_render(self, schemaType, data):
        return [string_converter(schemaType).fromType(data)]
    
    def convert(self, schemaType, data):
        if len(data) == 0:
            out='False'
        else:
            out='True'
        return string_converter(schemaType).toType(out)

    
class DateParts(Widget):
    
    def __init__(self, dayFirst=False):
        self.dayFirst = dayFirst
        
    def pre_render(self, schemaType, data):
        data = datetuple_converter(schemaType).fromType(data)
        d = {}
        d['year'] = [data[0]]
        d['month'] = [data[1]]
        d['day'] = [data[2]]
        return d
    
    def convert(self, schemaType, data):
{'fraction_non_alphanumeric': 0.1300524934383202, 'fraction_numerical': 0.0030183727034120734, 'mean_word_length': 2.7321253672869736, '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': '721609', 'n_tokens_mistral': 2293, 'n_tokens_neox': 2254, 'n_words': 474}