from __future__ import absolute_import

import argparse
import logging
import sys

import tornado.ioloop

from .tchannel import TChannel


log = logging.getLogger('tchannel')


@tornado.gen.coroutine
def tcurl(host, endpoint, headers, body):

    client = TChannel()

    print
    print "Sending this to", host
    print "*" * 80
    print " arg1:", endpoint
    print " arg2:", headers
    print " arg3:", body
    print

    request = client.request(host)

    response = yield request.send(
        endpoint,
        headers,
        body,
    )

    print
    print "Got this from", host
    print "*" * 80
    print " arg1:", getattr(response, 'arg_1', None)
    print " arg2:", getattr(response, 'arg_2', None)
    print " arg3:", getattr(response, 'arg_3', None)

    raise tornado.gen.Return(response)


def parse_args():
    parser = argparse.ArgumentParser()

    parser.add_argument(
        "--serve-port",
        dest="port",
        default=None,
        type=int,
        help="Optional port to accept incoming requests on.",
    )

    parser.add_argument(
        "--host",
        dest="host",
        default="localhost:8888/",
        help="Hostname, port, and optional endpoint (arg1) to hit.",
    )

    parser.add_argument(
        "-d", "--body",
        dest="body",
        default='',
        help="arg3",
    )

    parser.add_argument(
        "--headers",
        dest="headers",
        default=None,
        help="arg2",
    )

    parser.add_argument(
        "-v", "--verbose",
        dest="verbose",
        action="store_true"
    )

    return parser.parse_args()


def main():
    args = parse_args()

    if args.verbose:
        logging.basicConfig(
            format="%(name)s[%(process)s] %(levelname)s: %(message)s",
            stream=sys.stdout,
            level=logging.DEBUG,
        )

    # Transform something like "localhost:8888" into "localhost:8888/" so we
    # consider it as the '' endpoint.
    if '/' not in args.host:
        args.host += '/'

    host, endpoint = args.host.split('/', 1)

    tornado.ioloop.IOLoop.instance().run_sync(
        lambda: tcurl(host, endpoint, args.headers, args.body)
    )


if __name__ == '__main__':  # pragma: no cover
    main()

@@ -262,32 +262,36 @@
 nel()%0A%0A    print
+(%22%22)
 %0A    print %22Send
@@ -284,17 +284,17 @@
    print
- 
+(
 %22Sending
@@ -301,23 +301,28 @@
  this to
-%22,
+ %25s%22 %25
  host
+)
 %0A    pri
@@ -319,33 +319,33 @@
 t)%0A    print
- 
+(
 %22*%22 * 80
 %0A    print %22
@@ -324,32 +324,33 @@
   print(%22*%22 * 80
+)
 %0A    print %22 arg
@@ -343,26 +343,30 @@
    print
- 
+(
 %22 arg1:
-%22,
+ %25s%22 %25
  endpoin
@@ -366,16 +366,17 @@
 endpoint
+)
 %0A    pri
@@ -381,26 +381,30 @@
 rint
- 
+(
 %22 arg2:
-%22,
+ %25s%22 %25
  headers
 %0A   
@@ -399,16 +399,17 @@
  headers
+)
 %0A    pri
@@ -414,23 +414,28 @@
 rint
- 
+(
 %22 arg3:
-%22,
+ %25s%22 %25
  body
+)
 %0A   
@@ -440,16 +440,20 @@
    print
+(%22%22)
 %0A%0A    re
@@ -582,16 +582,20 @@
    print
+(%22%22)
 %0A    pri
@@ -596,17 +596,17 @@
    print
- 
+(
 %22Got thi
@@ -615,15 +615,20 @@
 from
-%22,
+ %25s%22 %25
  host
+)
 %0A   
@@ -637,17 +637,17 @@
 rint
- 
+(
 %22*%22 * 80
 %0A   
@@ -642,16 +642,17 @@
 %22*%22 * 80
+)
 %0A    pri
@@ -657,18 +657,22 @@
 rint
- 
+(
 %22 arg1:
-%22,
+ %25s%22 %25
  get
@@ -692,32 +692,33 @@
 , 'arg_1', None)
+)
 %0A    print %22 arg
@@ -715,18 +715,22 @@
 rint
- 
+(
 %22 arg2:
-%22,
+ %25s%22 %25
  get
@@ -758,16 +758,17 @@
 ', None)
+)
 %0A    pri
@@ -773,18 +773,22 @@
 rint
- 
+(
 %22 arg3:
-%22,
+ %25s%22 %25
  get
@@ -816,16 +816,17 @@
 ', None)
+)
 %0A%0A    ra

fix print statements for python 3 compat