<s> import argparse <EOL> import json <EOL> import requests <EOL> from operator import itemgetter <EOL> from pprint import pprint <EOL> from sys import stderr , exit , argv <EOL> from urllib import urlencode <EOL> import codecs <EOL> codecs . register ( lambda name : codecs . lookup ( '<STR_LIT:utf-8>' ) if name == '<STR_LIT>' else None ) <EOL> PARAMETER_TYPEMAP = { <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT:float>' : float , <EOL> '<STR_LIT:bool>' : bool , <EOL> '<STR_LIT:string>' : str , <EOL> '<STR_LIT>' : bytes , <EOL> } <EOL> def filter_none_values ( d ) : <EOL> return dict ( ( k , v ) for k , v in d . iteritems ( ) if v is not None ) <EOL> def argparser_for_method ( prog , method ) : <EOL> parser = argparse . ArgumentParser ( prog = prog ) <EOL> for parameter in method [ '<STR_LIT>' ] : <EOL> if parameter [ '<STR_LIT:name>' ] == '<STR_LIT:key>' : <EOL> continue <EOL> paramargs = { <EOL> '<STR_LIT:type>' : PARAMETER_TYPEMAP [ parameter [ '<STR_LIT:type>' ] ] , <EOL> '<STR_LIT>' : not parameter [ '<STR_LIT>' ] , <EOL> } <EOL> if '<STR_LIT:description>' in parameter : <EOL> paramargs [ '<STR_LIT>' ] = parameter [ '<STR_LIT:description>' ] <EOL> name = '<STR_LIT>' % parameter [ '<STR_LIT:name>' ] <EOL> parser . add_argument ( name , ** paramargs ) <EOL> return parser <EOL> def find_method ( interfaces , interface_name , method_name , version ) : <EOL> matching_interfaces = filter ( lambda interface : interface [ '<STR_LIT:name>' ] == interface_name , interfaces ) <EOL> if len ( matching_interfaces ) < <NUM_LIT:1> : <EOL> return ( None , None ) <EOL> interface = matching_interfaces [ <NUM_LIT:0> ] <EOL> methods = interface [ '<STR_LIT>' ] <EOL> matching_methods = filter ( lambda method : method [ '<STR_LIT:name>' ] == method_name , methods ) <EOL> if len ( matching_methods ) < <NUM_LIT:1> : <EOL> return ( interface , None ) <EOL> if version is None : <EOL> version = max ( map ( itemgetter ( '<STR_LIT:version>' ) , matching_methods ) ) <EOL> matching_methods = filter ( lambda method : method [ '<STR_LIT:version>' ] == version , matching_methods ) <EOL> if len ( matching_methods ) < <NUM_LIT:1> : <EOL> return ( interface , None ) <EOL> return ( interface , matching_methods [ <NUM_LIT:0> ] ) <EOL> BASE_URL = '<STR_LIT>' <EOL> def method_url ( interface , method ) : <EOL> return "<STR_LIT>" % ( BASE_URL , interface [ '<STR_LIT:name>' ] , method [ '<STR_LIT:name>' ] , method [ '<STR_LIT:version>' ] ) <EOL> def get_interfaces ( args ) : <EOL> url = method_url ( { '<STR_LIT:name>' : '<STR_LIT>' } , { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:version>' : <NUM_LIT:1> } ) <EOL> params = { '<STR_LIT>' : '<STR_LIT>' } <EOL> if args . key : <EOL> params [ '<STR_LIT:key>' ] = args . key <EOL> url += '<STR_LIT>' % urlencode ( params ) <EOL> if args . verbose : <EOL> print "<STR_LIT>" % url <EOL> response = requests . get ( url ) <EOL> return response . json ( ) [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> def list_commands ( args ) : <EOL> interfaces = get_interfaces ( args ) <EOL> for interface in interfaces : <EOL> if args . interface and interface [ '<STR_LIT:name>' ] != args . interface : <EOL> continue <EOL> print "<STR_LIT>" % interface [ '<STR_LIT:name>' ] <EOL> for method in interface [ '<STR_LIT>' ] : <EOL> if args . method and method [ '<STR_LIT:name>' ] != args . method : <EOL> continue <EOL> if '<STR_LIT:description>' in method : <EOL> print '<STR_LIT>' % ( method [ '<STR_LIT:name>' ] , method [ '<STR_LIT:version>' ] , method [ '<STR_LIT>' ] , method [ '<STR_LIT:description>' ] ) <EOL> else : <EOL> print '<STR_LIT>' % ( method [ '<STR_LIT:name>' ] , method [ '<STR_LIT:version>' ] , method [ '<STR_LIT>' ] ) <EOL> if args . interface : <EOL> for parameter in method [ '<STR_LIT>' ] : <EOL> mandatory_mark = '<STR_LIT:*>' <EOL> if parameter [ '<STR_LIT>' ] : <EOL> mandatory_mark = '<STR_LIT:U+0020>' <EOL> desc = '<STR_LIT>' % ( mandatory_mark , parameter [ '<STR_LIT:type>' ] , parameter [ '<STR_LIT:name>' ] ) <EOL> if '<STR_LIT:description>' in parameter : <EOL> desc += '<STR_LIT>' % ( parameter [ '<STR_LIT:description>' ] ) <EOL> print desc <EOL> if args . interface : <EOL> print '<STR_LIT>' <EOL> return True <EOL> def call_command ( args ) : <EOL> interfaces = get_interfaces ( args ) <EOL> interface , method = find_method ( interfaces , args . interface , args . method , args . method_version ) <EOL> if not interface : <EOL> print >> stderr , "<STR_LIT>" % args . interface <EOL> print >> stderr , "<STR_LIT>" <EOL> print >> stderr , "<STR_LIT>" % '<STR_LIT:U+002CU+0020>' . join ( map ( itemgetter ( '<STR_LIT:name>' ) , interfaces ) ) <EOL> return False <EOL> if not method : <EOL> print >> stderr , "<STR_LIT>" % args . method <EOL> print >> stderr , "<STR_LIT>" <EOL> print >> stderr , "<STR_LIT>" % '<STR_LIT:U+002CU+0020>' . join ( map ( itemgetter ( '<STR_LIT:name>' ) , interface [ '<STR_LIT>' ] ) ) <EOL> return False <EOL> original_args = [ '<STR_LIT>' ] + argv [ <NUM_LIT:1> : ] <EOL> if len ( args . parameters ) : <EOL> original_args = original_args [ : - len ( args . parameters ) ] <EOL> cmd = '<STR_LIT:U+0020>' . join ( original_args ) <EOL> arguments = argparser_for_method ( cmd , method ) . parse_args ( args . parameters ) <EOL> arguments = filter_none_values ( arguments . __dict__ ) <EOL> if args . key : <EOL> arguments [ '<STR_LIT:key>' ] = args . key <EOL> arguments [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> url = method_url ( interface , method ) <EOL> response = None <EOL> if method [ '<STR_LIT>' ] != '<STR_LIT:POST>' : <EOL> if len ( arguments ) > <NUM_LIT:0> : <EOL> url += '<STR_LIT>' % urlencode ( arguments ) <EOL> if args . verbose : <EOL> print "<STR_LIT>" % url <EOL> response = requests . get ( url ) <EOL> else : <EOL> if args . verbose : <EOL> print "<STR_LIT>" % url <EOL> pprint ( arguments ) <EOL> response = requests . post ( url , data = json . dumps ( arguments ) , headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) <EOL> if args . raw : <EOL> print response . text <EOL> else : <EOL> pprint ( response . json ( ) ) <EOL> return True <EOL> def main ( ) : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> commands = parser . add_subparsers ( ) <EOL> commands_subcommand = commands . add_parser ( '<STR_LIT>' , description = '<STR_LIT>' ) <EOL> commands_subcommand . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:?>' ) <EOL> commands_subcommand . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:?>' ) <EOL> commands_subcommand . set_defaults ( func = list_commands ) <EOL> call_subcommand = commands . add_parser ( '<STR_LIT>' , description = '<STR_LIT>' ) <EOL> call_subcommand . add_argument ( '<STR_LIT>' ) <EOL> call_subcommand . add_argument ( '<STR_LIT>' ) <EOL> call_subcommand . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' , type = int ) <EOL> call_subcommand . add_argument ( '<STR_LIT>' , nargs = argparse . REMAINDER ) <EOL> call_subcommand . set_defaults ( func = call_command ) <EOL> args = parser . parse_args ( ) <EOL> if not args . func ( args ) : <EOL> exit ( <NUM_LIT:1> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
