<s> from jinja2 import Template <EOL> import json <EOL> class ReactTemplate ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , template , template_args , path ) : <EOL> self . template = template <EOL> self . args = template_args <EOL> self . path = path <EOL> def write_to_file ( self , s ) : <EOL> """<STR_LIT:U+0020>""" <EOL> f = open ( self . path , '<STR_LIT:w>' ) <EOL> f . write ( s ) <EOL> f . close ( ) <EOL> def to_js ( self ) : <EOL> """<STR_LIT:U+0020>""" <EOL> t = Template ( self . template ) <EOL> js = t . render ( ** self . args ) <EOL> self . write_to_file ( js ) <EOL> def format_props ( props , prop_template = "<STR_LIT>" , delim = "<STR_LIT:\n>" ) : <EOL> """<STR_LIT>""" <EOL> vars_ = [ ] <EOL> props_ = [ ] <EOL> for k , v in list ( props . items ( ) ) : <EOL> vars_ . append ( Template ( "<STR_LIT>" ) . render ( k = k , v = json . dumps ( v ) ) ) <EOL> props_ . append ( Template ( prop_template ) . render ( k = k , v = k ) ) <EOL> return "<STR_LIT:\n>" . join ( vars_ ) , delim . join ( props_ ) <EOL> class ReactComponent ( ReactTemplate ) : <EOL> """<STR_LIT>""" <EOL> _react = ( <EOL> """<STR_LIT>""" ) <EOL> _react_export = ( <EOL> """<STR_LIT>""" ) <EOL> def __init__ ( self , name , path , element_id , <EOL> props = { } , static_path = "<STR_LIT>" , alias = None ) : <EOL> _template = self . _react <EOL> prop_template = "<STR_LIT>" <EOL> delim = "<STR_LIT:\n>" <EOL> if alias : <EOL> prop_template = "<STR_LIT>" <EOL> delim = "<STR_LIT>" <EOL> _template = self . _react_export <EOL> vars_ , props_ = format_props ( props , <EOL> prop_template = prop_template , delim = delim ) <EOL> params = { <EOL> "<STR_LIT:name>" : name , <EOL> "<STR_LIT>" : alias , <EOL> "<STR_LIT:path>" : path , <EOL> "<STR_LIT>" : vars_ , <EOL> "<STR_LIT>" : props_ , <EOL> "<STR_LIT:id>" : element_id <EOL> } <EOL> for k , v in list ( props . items ( ) ) : <EOL> params [ k ] = v <EOL> super ( ReactComponent , self ) . __init__ ( <EOL> _template , params , static_path ) <EOL> self . to_js ( ) </s>
