<s> """<STR_LIT>""" <EOL> import re <EOL> import os <EOL> import traceback <EOL> _desc_re = re . compile ( r'<STR_LIT>' ) <EOL> _expect_re = re . compile ( r'<STR_LIT>' ) <EOL> _data_expect_re = re . compile ( r"<STR_LIT>" ) <EOL> _feed_data_expect_re = re . compile ( r"<STR_LIT>" ) <EOL> def parse_content ( content ) : <EOL> match = _desc_re . search ( content ) <EOL> desc = match . group ( <NUM_LIT:1> ) <EOL> match = _expect_re . search ( content ) <EOL> expect = match . group ( <NUM_LIT:1> ) <EOL> data = None <EOL> for regex in [ _data_expect_re , _feed_data_expect_re ] : <EOL> match = regex . search ( expect ) <EOL> if match : <EOL> data = eval ( match . group ( <NUM_LIT:1> ) . strip ( ) ) <EOL> break <EOL> c = None <EOL> for tag in [ '<STR_LIT:content>' , '<STR_LIT>' , '<STR_LIT:title>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:info>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:body>' , '<STR_LIT:description>' , '<STR_LIT>' ] : <EOL> regex = re . compile ( r"<STR_LIT>" % ( tag , tag ) , re . S ) <EOL> match = regex . search ( content ) <EOL> if match : <EOL> c = match . group ( <NUM_LIT:1> ) <EOL> break <EOL> assert c is not None <EOL> if tag != '<STR_LIT:body>' : <EOL> c = c . replace ( '<STR_LIT>' , '<STR_LIT:<>' ) <EOL> c = c . replace ( '<STR_LIT>' , '<STR_LIT:&>' ) <EOL> return { <EOL> '<STR_LIT>' : desc , <EOL> '<STR_LIT>' : expect , <EOL> '<STR_LIT:data>' : data , <EOL> '<STR_LIT:content>' : c } <EOL> def serialize_content ( d ) : <EOL> s = '''<STR_LIT>''' % d <EOL> if d . get ( '<STR_LIT:data>' ) is not None : <EOL> s += '<STR_LIT>' % d [ '<STR_LIT:data>' ] <EOL> return s <EOL> def translate_file ( filename ) : <EOL> f = open ( filename , '<STR_LIT:rb>' ) <EOL> c = f . read ( ) <EOL> f . close ( ) <EOL> try : <EOL> output = serialize_content ( parse_content ( c ) ) <EOL> except : <EOL> print ( '<STR_LIT>' % filename ) <EOL> print ( c ) <EOL> traceback . print_exc ( ) <EOL> print ( '<STR_LIT:->' * <NUM_LIT> ) <EOL> return <EOL> new = os . path . splitext ( filename ) [ <NUM_LIT:0> ] + '<STR_LIT>' <EOL> f = open ( new , '<STR_LIT:wb>' ) <EOL> f . write ( output ) <EOL> f . close ( ) <EOL> def translate_all ( dir ) : <EOL> for fn in os . listdir ( dir ) : <EOL> fn = os . path . join ( dir , fn ) <EOL> if fn . endswith ( '<STR_LIT>' ) : <EOL> translate_file ( fn ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> translate_all ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) ) </s>
