<s> import json <EOL> import urllib . parse <EOL> import urllib . request <EOL> import websocket <EOL> endpoint = '<STR_LIT>' <EOL> package = '<STR_LIT>' <EOL> project = '<STR_LIT>' <EOL> parent = '<STR_LIT>' <EOL> class ResolveAPIError ( RuntimeError ) : <EOL> pass <EOL> class ResolveCompilerError ( RuntimeError ) : <EOL> pass <EOL> def decode_url ( string ) : <EOL> return urllib . parse . unquote ( string ) . replace ( '<STR_LIT>' , '<STR_LIT:U+0020>' ) . replace ( '<STR_LIT>' , '<STR_LIT:+>' ) <EOL> def decode_xml ( string ) : <EOL> return string . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def decode_json ( payload ) : <EOL> if isinstance ( payload , dict ) : <EOL> decoded = { } <EOL> for key in payload . keys ( ) : <EOL> decoded [ key ] = decode_json ( payload [ key ] ) <EOL> return decoded <EOL> elif isinstance ( payload , list ) : <EOL> decoded = [ ] <EOL> for element in payload : <EOL> decoded . append ( decode_json ( element ) ) <EOL> return decoded <EOL> elif isinstance ( payload , str ) : <EOL> element = decode_xml ( decode_url ( payload ) ) <EOL> try : <EOL> return decode_json ( json . loads ( element ) ) <EOL> except : <EOL> return element <EOL> else : <EOL> return payload <EOL> def decode ( payload ) : <EOL> return decode_json ( json . loads ( payload ) ) <EOL> def encode_url ( string ) : <EOL> return urllib . parse . quote ( string ) <EOL> def encode_json ( payload ) : <EOL> encoded = { } <EOL> for key in payload . keys ( ) : <EOL> if isinstance ( payload [ key ] , str ) : <EOL> encoded [ key ] = encode_url ( payload [ key ] ) <EOL> else : <EOL> encoded [ key ] = payload [ key ] <EOL> return encoded <EOL> def encode ( payload ) : <EOL> return json . dumps ( encode_json ( payload ) ) <EOL> def request ( job , name , content , package = package , project = project , parent = parent , endpoint = endpoint ) : <EOL> job_data = { <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : package , <EOL> '<STR_LIT>' : project , <EOL> '<STR_LIT:content>' : content , <EOL> '<STR_LIT>' : parent , <EOL> '<STR_LIT:type>' : '<STR_LIT:f>' <EOL> } <EOL> sock = websocket . WebSocket ( ) <EOL> sock . connect ( '<STR_LIT>' . format ( endpoint , job ) ) <EOL> sock . send ( encode ( job_data ) ) <EOL> sock . recv ( ) <EOL> return sock <EOL> def compile ( name , content , package = package , project = project , parent = parent , endpoint = endpoint ) : <EOL> sock = request ( '<STR_LIT>' , name , content , package , project , parent , endpoint ) <EOL> resp = decode ( sock . recv ( ) ) <EOL> if resp [ '<STR_LIT:status>' ] != '<STR_LIT>' : <EOL> try : <EOL> raise ResolveCompilerError ( resp [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> except KeyError : <EOL> raise ResolveAPIError ( resp [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> with urllib . request . urlopen ( '<STR_LIT>' . format ( endpoint , resp [ '<STR_LIT:result>' ] [ '<STR_LIT>' ] , resp [ '<STR_LIT:result>' ] [ '<STR_LIT>' ] ) ) as response : <EOL> jar = response . read ( ) <EOL> return jar <EOL> def genvcs ( name , content , package = package , project = project , parent = parent , endpoint = endpoint ) : <EOL> sock = request ( '<STR_LIT>' , name , content , package , project , parent , endpoint ) <EOL> resp = decode ( sock . recv ( ) ) <EOL> if resp [ '<STR_LIT:status>' ] != '<STR_LIT>' : <EOL> try : <EOL> raise ResolveCompilerError ( resp [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> except KeyError : <EOL> raise ResolveAPIError ( resp [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> return resp [ '<STR_LIT:result>' ] <EOL> def verify ( name , content , package = package , project = project , parent = parent , endpoint = endpoint ) : <EOL> sock = request ( '<STR_LIT>' , name , content , package , project , parent , endpoint ) <EOL> resp = decode ( sock . recv ( ) ) <EOL> while resp [ '<STR_LIT:status>' ] == '<STR_LIT>' : <EOL> if '<STR_LIT>' in resp [ '<STR_LIT:result>' ] [ '<STR_LIT:result>' ] : <EOL> result = <NUM_LIT:0> <EOL> elif '<STR_LIT>' in resp [ '<STR_LIT:result>' ] [ '<STR_LIT:result>' ] : <EOL> result = <NUM_LIT:1> <EOL> elif '<STR_LIT>' in resp [ '<STR_LIT:result>' ] [ '<STR_LIT:result>' ] : <EOL> result = <NUM_LIT:2> <EOL> else : <EOL> result = <NUM_LIT:3> <EOL> yield { '<STR_LIT:id>' : resp [ '<STR_LIT:result>' ] [ '<STR_LIT:id>' ] , '<STR_LIT:result>' : result } <EOL> resp = decode ( sock . recv ( ) ) <EOL> if resp [ '<STR_LIT:status>' ] != '<STR_LIT>' : <EOL> try : <EOL> raise ResolveCompilerError ( resp [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> except KeyError : <EOL> raise ResolveAPIError ( resp [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import os <EOL> import os . path <EOL> import sys <EOL> from argparse import ArgumentParser , FileType <EOL> parser = ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = endpoint , dest = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = package , dest = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = project , dest = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = parent , dest = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = sys . stdout , type = FileType ( '<STR_LIT:w>' ) , dest = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , choices = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT:file>' , type = FileType ( '<STR_LIT:r>' ) , help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> def binary_write ( data ) : <EOL> os . fdopen ( args . output . fileno ( ) , '<STR_LIT:wb>' ) . write ( data ) <EOL> def json_write ( obj ) : <EOL> args . output . write ( json . dumps ( obj ) ) <EOL> args . output . write ( '<STR_LIT:\n>' ) <EOL> def iter_write ( objs ) : <EOL> for obj in objs : <EOL> args . output . write ( json . dumps ( obj ) ) <EOL> args . output . write ( '<STR_LIT:\n>' ) <EOL> args . output . flush ( ) <EOL> if args . command == '<STR_LIT>' : <EOL> command = compile <EOL> output = binary_write <EOL> elif args . command == '<STR_LIT>' : <EOL> command = genvcs <EOL> output = json_write <EOL> elif args . command == '<STR_LIT>' : <EOL> command = verify <EOL> output = iter_write <EOL> try : <EOL> output ( command ( os . path . splitext ( os . path . basename ( args . file . name ) ) [ <NUM_LIT:0> ] , args . file . read ( ) , args . package , args . project , args . parent , args . endpoint ) ) <EOL> except ResolveAPIError as error : <EOL> print ( json . dumps ( error . args ) ) <EOL> sys . exit ( <NUM_LIT:1> ) </s>
