<s> from uliweb . core . html import Tag , begin_tag , end_tag <EOL> def safe_str ( s , encoding = '<STR_LIT:utf-8>' ) : <EOL> if isinstance ( s , unicode ) : <EOL> return s . encode ( encoding ) <EOL> else : <EOL> return str ( s ) <EOL> class Build ( object ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . kwargs = kwargs <EOL> def to_html ( self ) : <EOL> raise NotImplementedError <EOL> def pre_html ( self ) : <EOL> return '<STR_LIT>' <EOL> def post_html ( self ) : <EOL> return '<STR_LIT>' <EOL> def html ( self ) : <EOL> return '<STR_LIT>' . join ( [ self . pre_html ( ) % self . kwargs ] + [ self . to_html ( ) ] + <EOL> [ self . post_html ( ) % self . kwargs ] ) <EOL> def __str__ ( self ) : <EOL> return self . html ( ) <EOL> class Text ( Build ) : <EOL> type = '<STR_LIT:text>' <EOL> tag = '<STR_LIT:input>' <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( Text , self ) . __init__ ( ** kwargs ) <EOL> def to_html ( self ) : <EOL> args = self . kwargs . copy ( ) <EOL> args . setdefault ( '<STR_LIT:type>' , self . type ) <EOL> return str ( Tag ( self . tag , '<STR_LIT>' , ** args ) ) <EOL> class Password ( Text ) : type = '<STR_LIT:password>' <EOL> class Number ( Text ) : type = '<STR_LIT>' <EOL> class TextArea ( Build ) : <EOL> def __init__ ( self , value = '<STR_LIT>' , ** kwargs ) : <EOL> self . value = value or '<STR_LIT>' <EOL> super ( TextArea , self ) . __init__ ( ** kwargs ) <EOL> def to_html ( self ) : <EOL> args = self . kwargs . copy ( ) <EOL> return str ( Tag ( '<STR_LIT>' , self . value , ** args ) ) <EOL> class Hidden ( Text ) : type = '<STR_LIT>' <EOL> class Button ( Build ) : <EOL> def to_html ( self ) : <EOL> args = self . kwargs . copy ( ) <EOL> value = args . pop ( '<STR_LIT:value>' , None ) <EOL> return str ( Tag ( '<STR_LIT>' , value , ** args ) ) <EOL> class Submit ( Text ) : type = '<STR_LIT>' <EOL> class Reset ( Text ) : type = '<STR_LIT>' <EOL> class File ( Text ) : type = '<STR_LIT:file>' <EOL> class Radio ( Text ) : type = '<STR_LIT>' <EOL> class Select ( Build ) : <EOL> def __init__ ( self , choices , value = None , multiple = False , size = <NUM_LIT:10> , ** kwargs ) : <EOL> self . choices = choices or [ ] <EOL> self . value = value <EOL> self . multiple = multiple <EOL> self . size = size <EOL> super ( Select , self ) . __init__ ( ** kwargs ) <EOL> def to_html ( self ) : <EOL> from itertools import groupby <EOL> def _make ( v , caption ) : <EOL> v = safe_str ( v ) <EOL> args = { '<STR_LIT:value>' : v } <EOL> if isinstance ( self . value , ( tuple , list ) ) and v in [ safe_str ( x ) for x in self . value ] : <EOL> args [ '<STR_LIT>' ] = None <EOL> elif v == safe_str ( self . value ) : <EOL> args [ '<STR_LIT>' ] = None <EOL> return str ( Tag ( '<STR_LIT>' , safe_str ( caption ) , ** args ) ) <EOL> s = [ ] <EOL> group = False <EOL> if self . choices : <EOL> group = len ( self . choices [ <NUM_LIT:0> ] ) > <NUM_LIT:2> <EOL> if group : <EOL> for k , g in groupby ( self . choices , lambda x : x [ <NUM_LIT:0> ] ) : <EOL> s . append ( begin_tag ( '<STR_LIT>' , label = k ) ) <EOL> for x in g : <EOL> s . append ( _make ( x [ <NUM_LIT:1> ] , x [ <NUM_LIT:2> ] ) ) <EOL> s . append ( end_tag ( '<STR_LIT>' ) ) <EOL> else : <EOL> for v , caption in self . choices : <EOL> s . append ( _make ( v , caption ) ) <EOL> args = self . kwargs . copy ( ) <EOL> if self . multiple : <EOL> args [ '<STR_LIT>' ] = None <EOL> args [ '<STR_LIT:size>' ] = self . size <EOL> return str ( Tag ( '<STR_LIT>' , '<STR_LIT:\n>' . join ( s ) , newline = True , ** args ) ) <EOL> class RadioSelect ( Select ) : <EOL> _id = <NUM_LIT:0> <EOL> def __init__ ( self , choices , value = None , ** kwargs ) : <EOL> super ( RadioSelect , self ) . __init__ ( choices , value , ** kwargs ) <EOL> def to_html ( self ) : <EOL> s = [ ] <EOL> for v , caption in self . choices : <EOL> args = { '<STR_LIT:value>' : v } <EOL> kwargs = self . kwargs . copy ( ) <EOL> args [ '<STR_LIT:name>' ] = kwargs . pop ( '<STR_LIT:name>' ) <EOL> args [ '<STR_LIT:id>' ] = kwargs . pop ( '<STR_LIT:id>' ) <EOL> if isinstance ( self . value , ( tuple , list ) ) : <EOL> if v in self . value : <EOL> args [ '<STR_LIT>' ] = None <EOL> else : <EOL> if v == self . value : <EOL> args [ '<STR_LIT>' ] = None <EOL> r = str ( Radio ( ** args ) ) <EOL> s . append ( str ( Tag ( '<STR_LIT:label>' , r + caption , ** kwargs ) ) ) <EOL> return '<STR_LIT:\n>' . join ( s ) <EOL> def get_id ( self ) : <EOL> RadioSelect . _id += <NUM_LIT:1> <EOL> return self . _id <EOL> class CheckboxSelect ( Select ) : <EOL> _id = <NUM_LIT:0> <EOL> def __init__ ( self , choices , value = None , ** kwargs ) : <EOL> super ( CheckboxSelect , self ) . __init__ ( choices , value , ** kwargs ) <EOL> def to_html ( self ) : <EOL> s = [ ] <EOL> for v , caption in self . choices : <EOL> args = { '<STR_LIT:value>' : v } <EOL> kwargs = self . kwargs . copy ( ) <EOL> args [ '<STR_LIT:name>' ] = kwargs . pop ( '<STR_LIT:name>' ) <EOL> args [ '<STR_LIT:id>' ] = kwargs . pop ( '<STR_LIT:id>' ) <EOL> if isinstance ( self . value , ( tuple , list ) ) : <EOL> if v in self . value : <EOL> args [ '<STR_LIT>' ] = None <EOL> else : <EOL> if v == self . value : <EOL> args [ '<STR_LIT>' ] = None <EOL> r = str ( Checkbox ( ** args ) ) <EOL> s . append ( str ( Tag ( '<STR_LIT:label>' , r + caption , ** kwargs ) ) ) <EOL> return '<STR_LIT:\n>' . join ( s ) <EOL> def get_id ( self ) : <EOL> RadioSelect . _id += <NUM_LIT:1> <EOL> return self . _id <EOL> class Checkbox ( Build ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( Checkbox , self ) . __init__ ( ** kwargs ) <EOL> def to_html ( self ) : <EOL> args = self . kwargs . copy ( ) <EOL> args . setdefault ( '<STR_LIT:type>' , '<STR_LIT>' ) <EOL> return str ( Tag ( '<STR_LIT:input>' , '<STR_LIT>' , ** args ) ) </s>
