Package restkit :: Module console
[hide private]
[frames] | no frames]

Source Code for Module restkit.console

  1  # -*- coding: utf-8 - 
  2  # 
  3  # This file is part of restkit released under the MIT license.  
  4  # See the NOTICE for more information. 
  5   
  6  from __future__ import with_statement 
  7  import os 
  8  import optparse as op 
  9  import sys 
 10   
 11  from restkit import __version__, request, set_logging 
 12   
 13  __usage__ = "'%prog [options] url [METHOD] [filename]'" 
 14   
15 -def options():
16 """ build command lines options """ 17 return [ 18 op.make_option('-H', '--header', action='append', dest='headers', 19 help='http string header in the form of Key:Value. '+ 20 'For example: "Accept: application/json" '), 21 op.make_option('-S', '--server-response', action='store_true', 22 default=False, dest='server_response', 23 help='print server response'), 24 op.make_option('--log-level', dest="log_level", 25 help="Log level below which to silence messages. [info]", 26 default="info"), 27 op.make_option('-i', '--input', action='store', dest='input', 28 metavar='FILE', help='the name of the file to read from'), 29 op.make_option('-o', '--output', action='store', dest='output', 30 help='the name of the file to write to'), 31 op.make_option('--follow-redirect', action='store_false', 32 dest='follow_redirect', default=True) 33 ]
34
35 -def main():
36 """ function to manage restkit command line """ 37 parser = op.OptionParser(usage=__usage__, option_list=options(), 38 version="%prog " + __version__) 39 40 opts, args = parser.parse_args() 41 args_len = len(args) 42 43 if args_len < 1: 44 return parser.error('incorrect number of arguments') 45 46 set_logging(opts.log_level) 47 48 body = None 49 headers = [] 50 if opts.input: 51 if opts.input == '-': 52 body = sys.stdin.read() 53 headers.append(("Content-Length", str(len(body)))) 54 else: 55 fname = os.path.normpath(os.path.join(os.getcwd(),opts.input)) 56 body = open(fname, 'r') 57 58 if opts.headers: 59 for header in opts.headers: 60 try: 61 k, v = header.split(':') 62 headers.append((k, v)) 63 except ValueError: 64 pass 65 66 try: 67 if args_len == 3: 68 resp = request(args[0], method=args[1], body=body, 69 headers=headers, follow_redirect=opts.follow_redirect) 70 elif len(args) == 2: 71 if args[1] == "-": 72 body = sys.stdin.read() 73 headers.append(("Content-Length", str(len(body)))) 74 75 resp = request(args[0], method=args[1], body=body, 76 headers=headers, follow_redirect=opts.follow_redirect) 77 else: 78 if opts.input: 79 method = 'POST' 80 else: 81 method='GET' 82 resp = request(args[0], method=method, body=body, 83 headers=headers, follow_redirect=opts.follow_redirect) 84 85 if opts.output: 86 with open(opts.output, 'wb') as f: 87 if opts.server_response: 88 f.write("Server response from %s:\n" % resp.final_url) 89 for k, v in resp.headerslist: 90 f.write( "%s: %s" % (k, v)) 91 else: 92 for block in resp.body_file: 93 f.write(block) 94 else: 95 if opts.server_response: 96 print "\n\033[0m\033[95mServer response from %s:\n\033[0m" % ( 97 resp.final_url) 98 for k, v in resp.headerslist: 99 print "\033[94m%s\033[0m: %s" % (k, v) 100 print "\033[0m" 101 else: 102 print resp.body 103 104 except Exception, e: 105 sys.stderr.write("An error happened: %s" % str(e)) 106 sys.stderr.flush() 107 sys.exit(1) 108 109 sys.exit(0)
110