<s> import json <EOL> import os <EOL> import sys <EOL> import urllib2 <EOL> def get_env ( name ) : <EOL> env = os . environ . get ( name ) <EOL> if not env : <EOL> sys . stderr . write ( "<STR_LIT>" . format ( name ) ) <EOL> sys . exit ( <NUM_LIT:5> ) <EOL> return env <EOL> class Request ( urllib2 . Request ) : <EOL> def __init__ ( self , method , * args , ** kwargs ) : <EOL> self . _method = method <EOL> urllib2 . Request . __init__ ( self , * args , ** kwargs ) <EOL> def get_method ( self ) : <EOL> return self . _method <EOL> def proxy_request ( service_name , instance_name , method , path , body = None , headers = None ) : <EOL> target = get_env ( "<STR_LIT>" ) . rstrip ( "<STR_LIT:/>" ) <EOL> token = get_env ( "<STR_LIT>" ) <EOL> if not target . startswith ( "<STR_LIT>" ) and not target . startswith ( "<STR_LIT>" ) : <EOL> target = "<STR_LIT>" . format ( target ) <EOL> url = "<STR_LIT>" . format ( target , service_name , instance_name , path ) <EOL> if body : <EOL> body = json . dumps ( body ) <EOL> request = Request ( method , url , data = body ) <EOL> request . add_header ( "<STR_LIT>" , "<STR_LIT>" + token ) <EOL> if headers : <EOL> for header , value in headers . items ( ) : <EOL> request . add_header ( header , value ) <EOL> return urllib2 . urlopen ( request , timeout = <NUM_LIT:30> ) <EOL> def add_url ( service_name , name , url , expected_string = None , comment = None ) : <EOL> """<STR_LIT>""" <EOL> data = { <EOL> "<STR_LIT:name>" : name , <EOL> "<STR_LIT:url>" : url , <EOL> } <EOL> if expected_string : <EOL> data [ "<STR_LIT>" ] = expected_string <EOL> if comment : <EOL> data [ "<STR_LIT>" ] = comment <EOL> headers = { <EOL> "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> proxy_request ( service_name , name , "<STR_LIT:POST>" , "<STR_LIT>" , data , headers ) <EOL> msg = "<STR_LIT>" . format ( url ) <EOL> sys . stdout . write ( msg ) <EOL> def remove_url ( service_name , name , url ) : <EOL> """<STR_LIT>""" <EOL> body = { "<STR_LIT:name>" : name , "<STR_LIT:url>" : url } <EOL> headers = { "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" } <EOL> try : <EOL> proxy_request ( service_name , name , "<STR_LIT>" , "<STR_LIT>" , body = body , headers = headers ) <EOL> except urllib2 . HTTPError : <EOL> sys . stdout . write ( "<STR_LIT>" % url ) <EOL> return <EOL> msg = "<STR_LIT>" . format ( url ) <EOL> sys . stdout . write ( msg ) <EOL> def list_urls ( service_name , name ) : <EOL> """<STR_LIT>""" <EOL> url = "<STR_LIT>" . format ( name ) <EOL> headers = { "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" } <EOL> response = proxy_request ( service_name , name , "<STR_LIT:GET>" , url , "<STR_LIT>" , headers ) <EOL> urls = response . read ( ) <EOL> sys . stdout . write ( urls + "<STR_LIT:\n>" ) <EOL> def add_watcher ( service_name , name , watcher ) : <EOL> """<STR_LIT>""" <EOL> data = { <EOL> "<STR_LIT:name>" : name , <EOL> "<STR_LIT>" : watcher , <EOL> } <EOL> headers = { <EOL> "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> proxy_request ( service_name , name , "<STR_LIT:POST>" , "<STR_LIT>" , data , headers ) <EOL> msg = "<STR_LIT>" . format ( watcher ) <EOL> sys . stdout . write ( msg ) <EOL> def remove_watcher ( service_name , name , watcher ) : <EOL> """<STR_LIT>""" <EOL> url = "<STR_LIT>" . format ( name , watcher ) <EOL> proxy_request ( service_name , name , "<STR_LIT>" , url ) <EOL> msg = "<STR_LIT>" . format ( watcher ) <EOL> sys . stdout . write ( msg ) <EOL> def list_watchers ( service_name , name ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' . format ( name ) <EOL> headers = { "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" } <EOL> response = proxy_request ( service_name , name , "<STR_LIT:GET>" , url , "<STR_LIT>" , headers ) <EOL> watchers_json = response . read ( ) <EOL> watchers = json . loads ( watchers_json ) <EOL> for watcher in watchers : <EOL> sys . stdout . write ( watcher + "<STR_LIT:\n>" ) <EOL> def show_help ( command_name = None , exit = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> plugin_name = os . environ . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> commands = _get_commands ( ) <EOL> if command_name and command_name in commands : <EOL> command = commands [ command_name ] <EOL> doc = command . __doc__ . format ( plugin_name = plugin_name ) <EOL> sys . stderr . write ( doc . rstrip ( ) + "<STR_LIT:\n>" ) <EOL> sys . exit ( exit ) <EOL> msg = "<STR_LIT>" <EOL> sys . stderr . write ( msg . format ( plugin_name = plugin_name ) ) <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> for name in sorted ( commands . keys ( ) ) : <EOL> if name != "<STR_LIT>" : <EOL> sys . stderr . write ( "<STR_LIT>" . format ( name ) ) <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> msg = "<STR_LIT>" <EOL> sys . stderr . write ( "<STR_LIT:\n>" + msg . format ( plugin_name = plugin_name ) + "<STR_LIT:\n>" ) <EOL> sys . exit ( exit ) <EOL> def _get_commands ( ) : <EOL> return { <EOL> "<STR_LIT>" : add_url , <EOL> "<STR_LIT>" : remove_url , <EOL> "<STR_LIT>" : list_urls , <EOL> "<STR_LIT>" : add_watcher , <EOL> "<STR_LIT>" : remove_watcher , <EOL> "<STR_LIT>" : list_watchers , <EOL> "<STR_LIT>" : show_help , <EOL> } <EOL> def command ( command_name ) : <EOL> commands = _get_commands ( ) <EOL> if command_name in commands : <EOL> return commands [ command_name ] <EOL> show_help ( exit = <NUM_LIT:2> ) <EOL> def main ( cmd , * args ) : <EOL> try : <EOL> command ( cmd ) ( * args ) <EOL> except TypeError : <EOL> show_help ( cmd , exit = <NUM_LIT:2> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> if len ( sys . argv ) < <NUM_LIT:2> : <EOL> show_help ( exit = <NUM_LIT:2> ) <EOL> main ( sys . argv [ <NUM_LIT:1> ] , * sys . argv [ <NUM_LIT:2> : ] ) </s>
