<s> from __future__ import unicode_literals <EOL> from builtins import str <EOL> import datetime , logging , re <EOL> def string_strip ( text , loader_context ) : <EOL> if not isinstance ( text , str ) : <EOL> text = str ( text ) <EOL> chars = loader_context . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return text . strip ( chars ) <EOL> def remove_chars ( text , loader_context ) : <EOL> pattern = loader_context . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> result_str = re . sub ( pattern , '<STR_LIT>' , str ( text ) ) <EOL> return result_str <EOL> def pre_string ( text , loader_context ) : <EOL> pre_str = loader_context . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return pre_str + text <EOL> def post_string ( text , loader_context ) : <EOL> post_str = loader_context . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return text + post_str <EOL> def pre_url ( text , loader_context ) : <EOL> pre_url = loader_context . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if ( pre_url [ <NUM_LIT:0> : <NUM_LIT:7> ] == '<STR_LIT>' and text [ <NUM_LIT:0> : <NUM_LIT:7> ] == '<STR_LIT>' ) : <EOL> return text <EOL> if ( pre_url [ - <NUM_LIT:1> : ] == '<STR_LIT:/>' and text [ <NUM_LIT:0> : <NUM_LIT:1> ] == '<STR_LIT:/>' ) : <EOL> pre_url = pre_url [ : - <NUM_LIT:1> ] <EOL> return pre_url + text <EOL> def replace ( text , loader_context ) : <EOL> replace = loader_context . get ( '<STR_LIT:replace>' , '<STR_LIT>' ) <EOL> return replace <EOL> def static ( text , loader_context ) : <EOL> static = loader_context . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return static <EOL> def date ( text , loader_context ) : <EOL> cformat = loader_context . get ( '<STR_LIT:date>' ) <EOL> try : <EOL> if text . lower ( ) in [ '<STR_LIT>' , '<STR_LIT>' , ] : <EOL> date = datetime . date . today ( ) - datetime . timedelta ( <NUM_LIT:1> ) <EOL> elif text . lower ( ) in [ '<STR_LIT>' , '<STR_LIT>' , ] : <EOL> date = datetime . date . today ( ) <EOL> elif text . lower ( ) in [ '<STR_LIT>' , '<STR_LIT>' , ] : <EOL> date = datetime . date . today ( ) + datetime . timedelta ( <NUM_LIT:1> ) <EOL> else : <EOL> date = datetime . datetime . strptime ( text , cformat ) <EOL> except ValueError : <EOL> loader_context . get ( '<STR_LIT>' ) . log ( '<STR_LIT>' . format ( t = text , f = cformat ) , logging . ERROR ) <EOL> return None <EOL> return date . strftime ( '<STR_LIT>' ) <EOL> def time ( text , loader_context ) : <EOL> cformat = loader_context . get ( '<STR_LIT:time>' ) <EOL> try : <EOL> time = datetime . datetime . strptime ( text , cformat ) <EOL> except ValueError : <EOL> loader_context . get ( '<STR_LIT>' ) . log ( '<STR_LIT>' . format ( t = text , f = cformat ) , logging . ERROR ) <EOL> return None <EOL> return time . strftime ( '<STR_LIT>' ) <EOL> def ts_to_date ( ts_str , loader_context ) : <EOL> try : <EOL> ts_int = int ( ts_str ) <EOL> return datetime . datetime . fromtimestamp ( ts_int ) . strftime ( '<STR_LIT>' ) <EOL> except ValueError : <EOL> loader_context . get ( '<STR_LIT>' ) . log ( '<STR_LIT>' . format ( ts = ts_str ) , logging . ERROR ) <EOL> return None <EOL> def ts_to_time ( ts_str , loader_context ) : <EOL> try : <EOL> ts_int = int ( ts_str ) <EOL> return datetime . datetime . fromtimestamp ( ts_int ) . strftime ( '<STR_LIT>' ) <EOL> except ValueError : <EOL> loader_context . get ( '<STR_LIT>' ) . log ( '<STR_LIT>' . format ( ts = ts_str ) , logging . ERROR ) <EOL> return None <EOL> def _breakdown_time_unit_overlap ( time_str , limit ) : <EOL> time_list = time_str . split ( '<STR_LIT::>' ) <EOL> first = int ( time_list [ <NUM_LIT:0> ] ) <EOL> if first >= limit : <EOL> time_list [ <NUM_LIT:0> ] = str ( first % limit ) <EOL> time_list . insert ( <NUM_LIT:0> , str ( first // limit ) ) <EOL> else : <EOL> if ( len ( time_list [ <NUM_LIT:0> ] ) == <NUM_LIT:1> ) : <EOL> time_list [ <NUM_LIT:0> ] = '<STR_LIT:0>' + time_list [ <NUM_LIT:0> ] <EOL> time_list . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> time_str = '<STR_LIT::>' . join ( time_list ) <EOL> return time_str <EOL> def duration ( text , loader_context ) : <EOL> cformat = loader_context . get ( '<STR_LIT>' ) <EOL> text_int = None <EOL> try : <EOL> text_int = int ( text ) <EOL> except ValueError : <EOL> pass <EOL> if ( cformat == '<STR_LIT>' ) : <EOL> if text_int : <EOL> text += '<STR_LIT>' <EOL> if ( cformat == '<STR_LIT>' ) : <EOL> text = _breakdown_time_unit_overlap ( text , <NUM_LIT> ) <EOL> cformat = '<STR_LIT>' <EOL> if ( cformat == '<STR_LIT>' ) : <EOL> if text_int : <EOL> text += '<STR_LIT>' <EOL> text = _breakdown_time_unit_overlap ( text , <NUM_LIT> ) <EOL> cformat = '<STR_LIT>' <EOL> if ( cformat == '<STR_LIT>' ) : <EOL> if text_int : <EOL> if text_int >= <NUM_LIT> : <EOL> hours_str = str ( text_int // <NUM_LIT> ) + '<STR_LIT::>' <EOL> secs_under_hour_str = str ( text_int % <NUM_LIT> ) <EOL> text = hours_str + _breakdown_time_unit_overlap ( secs_under_hour_str , <NUM_LIT> ) <EOL> cformat = '<STR_LIT>' <EOL> else : <EOL> text = _breakdown_time_unit_overlap ( text , <NUM_LIT> ) <EOL> cformat = '<STR_LIT>' <EOL> try : <EOL> duration = datetime . datetime . strptime ( text , cformat ) <EOL> except ValueError : <EOL> loader_context . get ( '<STR_LIT>' ) . log ( '<STR_LIT>' . format ( t = text , f = cformat ) , logging . ERROR ) <EOL> return None <EOL> return duration . strftime ( '<STR_LIT>' ) </s>
