#!/usr/bin/env python
#
# Autogenerated by Thrift Compiler (0.9.0)
#
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
#
#  options string: py
#

import sys
import pprint
from urlparse import urlparse
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.transport import THttpClient
from thrift.protocol import TBinaryProtocol

import THBaseService
from ttypes import *

if len(sys.argv) <= 1 or sys.argv[1] == '--help':
  print ''
  print 'Usage: ' + sys.argv[0] + ' [-h host[:port]] [-u url] [-f[ramed]] function [arg1 [arg2...]]'
  print ''
  print 'Functions:'
  print '  bool exists(string table, TGet get)'
  print '  TResult get(string table, TGet get)'
  print '   getMultiple(string table,  gets)'
  print '  void put(string table, TPut put)'
  print '  bool checkAndPut(string table, string row, string family, string qualifier, string value, TPut put)'
  print '  void putMultiple(string table,  puts)'
  print '  void deleteSingle(string table, TDelete deleteSingle)'
  print '   deleteMultiple(string table,  deletes)'
  print '  bool checkAndDelete(string table, string row, string family, string qualifier, string value, TDelete deleteSingle)'
  print '  TResult increment(string table, TIncrement increment)'
  print '  i32 openScanner(string table, TScan scan)'
  print '   getScannerRows(i32 scannerId, i32 numRows)'
  print '  void closeScanner(i32 scannerId)'
  print ''
  sys.exit(0)

pp = pprint.PrettyPrinter(indent = 2)
host = 'localhost'
port = 9090
uri = ''
framed = False
http = False
argi = 1

if sys.argv[argi] == '-h':
  parts = sys.argv[argi+1].split(':')
  host = parts[0]
  if len(parts) > 1:
    port = int(parts[1])
  argi += 2

if sys.argv[argi] == '-u':
  url = urlparse(sys.argv[argi+1])
  parts = url[1].split(':')
  host = parts[0]
  if len(parts) > 1:
    port = int(parts[1])
  else:
    port = 80
  uri = url[2]
  if url[4]:
    uri += '?%s' % url[4]
  http = True
  argi += 2

if sys.argv[argi] == '-f' or sys.argv[argi] == '-framed':
  framed = True
  argi += 1

cmd = sys.argv[argi]
args = sys.argv[argi+1:]

if http:
  transport = THttpClient.THttpClient(host, port, uri)
else:
  socket = TSocket.TSocket(host, port)
  if framed:
    transport = TTransport.TFramedTransport(socket)
  else:
    transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = THBaseService.Client(protocol)
transport.open()

if cmd == 'exists':
  if len(args) != 2:
    print 'exists requires 2 args'
    sys.exit(1)
  pp.pprint(client.exists(args[0],eval(args[1]),))

elif cmd == 'get':
  if len(args) != 2:
    print 'get requires 2 args'
    sys.exit(1)
  pp.pprint(client.get(args[0],eval(args[1]),))

elif cmd == 'getMultiple':
  if len(args) != 2:
    print 'getMultiple requires 2 args'
    sys.exit(1)
  pp.pprint(client.getMultiple(args[0],eval(args[1]),))

elif cmd == 'put':
  if len(args) != 2:
    print 'put requires 2 args'
    sys.exit(1)
  pp.pprint(client.put(args[0],eval(args[1]),))

elif cmd == 'checkAndPut':
  if len(args) != 6:
    print 'checkAndPut requires 6 args'
    sys.exit(1)
  pp.pprint(client.checkAndPut(args[0],args[1],args[2],args[3],args[4],eval(args[5]),))

elif cmd == 'putMultiple':
  if len(args) != 2:
    print 'putMultiple requires 2 args'
    sys.exit(1)
  pp.pprint(client.putMultiple(args[0],eval(args[1]),))

elif cmd == 'deleteSingle':
  if len(args) != 2:
    print 'deleteSingle requires 2 args'
    sys.exit(1)
  pp.pprint(client.deleteSingle(args[0],eval(args[1]),))

elif cmd == 'deleteMultiple':
  if len(args) != 2:
    print 'deleteMultiple requires 2 args'
    sys.exit(1)
  pp.pprint(client.deleteMultiple(args[0],eval(args[1]),))

elif cmd == 'checkAndDelete':
  if len(args) != 6:
    print 'checkAndDelete requires 6 args'
    sys.exit(1)
  pp.pprint(client.checkAndDelete(args[0],args[1],args[2],args[3],args[4],eval(args[5]),))

elif cmd == 'increment':
  if len(args) != 2:
    print 'increment requires 2 args'
    sys.exit(1)
  pp.pprint(client.increment(args[0],eval(args[1]),))

elif cmd == 'openScanner':
  if len(args) != 2:
    print 'openScanner requires 2 args'
    sys.exit(1)
  pp.pprint(client.openScanner(args[0],eval(args[1]),))

elif cmd == 'getScannerRows':
  if len(args) != 2:
    print 'getScannerRows requires 2 args'
    sys.exit(1)
  pp.pprint(client.getScannerRows(eval(args[0]),eval(args[1]),))

elif cmd == 'closeScanner':
  if len(args) != 1:
    print 'closeScanner requires 1 args'
    sys.exit(1)
  pp.pprint(client.closeScanner(eval(args[0]),))

else:
  print 'Unrecognized method %s' % cmd
  sys.exit(1)

transport.close()
