<s> import sys , os , re , tarfile , json <EOL> FILES = { <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> } <EOL> WS_SPLIT = re . compile ( "<STR_LIT>" ) <EOL> def lines ( fn ) : <EOL> with tarfile . open ( fn , '<STR_LIT>' ) as tar : <EOL> for info in tar : <EOL> if not info . isfile ( ) or info . name not in FILES : <EOL> continue <EOL> f = tar . extractfile ( info ) <EOL> for ln in f : <EOL> ln = ln . rstrip ( ) <EOL> ln = ln . split ( '<STR_LIT:#>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> ln = ln . rstrip ( '<STR_LIT>' ) <EOL> if ln : <EOL> yield ln . decode ( '<STR_LIT>' ) <EOL> f . close ( ) <EOL> def offset ( s ) : <EOL> if s in { '<STR_LIT:->' , '<STR_LIT:0>' } : <EOL> return <NUM_LIT:0> <EOL> dir , s = ( - <NUM_LIT:1> , s [ <NUM_LIT:1> : ] ) if s [ <NUM_LIT:0> ] == '<STR_LIT:->' else ( <NUM_LIT:1> , s ) <EOL> words = [ int ( n ) for n in s . split ( '<STR_LIT::>' ) ] <EOL> assert <NUM_LIT:1> <= len ( words ) < <NUM_LIT:4> , words <EOL> words = words + [ <NUM_LIT:0> ] * ( <NUM_LIT:3> - len ( words ) ) <EOL> assert <NUM_LIT:0> <= words [ <NUM_LIT:0> ] < <NUM_LIT> , words <EOL> assert <NUM_LIT:0> <= words [ <NUM_LIT:1> ] < <NUM_LIT> , words <EOL> assert <NUM_LIT:0> <= words [ <NUM_LIT:2> ] < <NUM_LIT> , words <EOL> return dir * sum ( ( i * num ) for ( i , num ) in zip ( words , ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ) ) ) <EOL> def zoneline ( ls ) : <EOL> ls [ <NUM_LIT:1> ] = None if ls [ <NUM_LIT:1> ] == '<STR_LIT:->' else ls [ <NUM_LIT:1> ] <EOL> tmp = offset ( ls [ <NUM_LIT:0> ] ) , ls [ <NUM_LIT:1> ] , ls [ <NUM_LIT:2> ] , ls [ <NUM_LIT:3> : ] <EOL> return { k : v for ( k , v ) in zip ( '<STR_LIT>' , tmp ) } <EOL> def parse ( fn ) : <EOL> zones , rules , zone = { } , { } , None <EOL> for ln in lines ( fn ) : <EOL> words = WS_SPLIT . split ( ln ) <EOL> if words [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> assert words [ <NUM_LIT:1> ] not in zones , words [ <NUM_LIT:1> ] <EOL> zone = [ ] <EOL> zone . append ( zoneline ( words [ <NUM_LIT:2> : ] ) ) <EOL> if '<STR_LIT:/>' in words [ <NUM_LIT:1> ] : <EOL> zones [ words [ <NUM_LIT:1> ] ] = zone <EOL> elif words [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> assert zone is not None <EOL> zone . append ( zoneline ( words [ <NUM_LIT:1> : ] ) ) <EOL> elif words [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> zone = None <EOL> words [ <NUM_LIT:8> ] = offset ( words [ <NUM_LIT:8> ] ) <EOL> rule = rules . setdefault ( words [ <NUM_LIT:1> ] , [ ] ) <EOL> rule . append ( words [ <NUM_LIT:2> : ] ) <EOL> elif words [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> zone = None <EOL> else : <EOL> assert False , ln <EOL> return { '<STR_LIT>' : zones , '<STR_LIT>' : rules } <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> path = sys . argv [ <NUM_LIT:1> ] <EOL> version = re . match ( '<STR_LIT>' , os . path . basename ( path ) ) <EOL> if version is None : <EOL> raise StandardError ( '<STR_LIT>' ) <EOL> print json . dumps ( parse ( path ) ) </s>
