<s> """<STR_LIT>""" <EOL> import re <EOL> import six <EOL> class BaseTemplate ( object ) : <EOL> idpattern = None <EOL> braces = ( None , None ) <EOL> def __init__ ( self , template ) : <EOL> self . template = template <EOL> self . pattern = re . compile ( <EOL> r'<STR_LIT>' . format ( <EOL> self . braces [ <NUM_LIT:0> ] , self . braces [ <NUM_LIT:1> ] , self . idpattern <EOL> ) , <EOL> re . IGNORECASE | re . VERBOSE ) <EOL> def substitute ( self , data = { } , ** kwargs ) : <EOL> def convert ( mo ) : <EOL> key = mo . group ( '<STR_LIT:id>' ) <EOL> val = kwargs [ key ] if key in kwargs else data [ key ] <EOL> return six . text_type ( val ) <EOL> return self . pattern . sub ( convert , self . template ) <EOL> def safe_substitute ( self , data = { } , ** kwargs ) : <EOL> def convert ( mo ) : <EOL> key = mo . group ( '<STR_LIT:id>' ) <EOL> if key in kwargs : <EOL> val = kwargs [ key ] <EOL> elif key in data : <EOL> val = data [ key ] <EOL> else : <EOL> val = mo . group ( ) <EOL> return six . text_type ( val ) <EOL> return self . pattern . sub ( convert , self . template ) <EOL> class NetworkTemplate ( BaseTemplate ) : <EOL> """<STR_LIT>""" <EOL> idpattern = r'<STR_LIT>' <EOL> braces = ( '<STR_LIT>' , '<STR_LIT>' ) </s>
