<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from formencode . rewritingparser import RewritingParser <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def rename ( form , rename_func ) : <EOL> """<STR_LIT>""" <EOL> p = RenamingParser ( rename_func ) <EOL> p . feed ( form ) <EOL> p . close ( ) <EOL> return p . text ( ) <EOL> def add_prefix ( form , prefix , dotted = False ) : <EOL> """<STR_LIT>""" <EOL> def rename_func ( field_name ) : <EOL> if dotted : <EOL> if field_name : <EOL> return prefix + '<STR_LIT:.>' + field_name <EOL> else : <EOL> return prefix <EOL> else : <EOL> return prefix + field_name <EOL> return rename ( form , rename_func ) <EOL> class RenamingParser ( RewritingParser ) : <EOL> def __init__ ( self , rename_func ) : <EOL> RewritingParser . __init__ ( self ) <EOL> self . rename_func = rename_func <EOL> def close ( self ) : <EOL> self . handle_misc ( None ) <EOL> RewritingParser . close ( self ) <EOL> self . _text = self . _get_text ( ) <EOL> def text ( self ) : <EOL> try : <EOL> return self . _text <EOL> except AttributeError : <EOL> raise Exception ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def handle_starttag ( self , tag , attrs , startend = False ) : <EOL> self . write_pos ( ) <EOL> if tag in ( '<STR_LIT:input>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . handle_field ( tag , attrs , startend ) <EOL> else : <EOL> return <EOL> def handle_startendtag ( self , tag , attrs ) : <EOL> return self . handle_starttag ( tag , attrs , True ) <EOL> def handle_field ( self , tag , attrs , startend ) : <EOL> name = self . get_attr ( attrs , '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> new_name = self . rename_func ( name ) <EOL> if name is None : <EOL> self . del_attr ( attrs , '<STR_LIT:name>' ) <EOL> else : <EOL> self . set_attr ( attrs , '<STR_LIT:name>' , new_name ) <EOL> self . write_tag ( tag , attrs ) <EOL> self . skip_next = True </s>
