This commit is contained in:
Jakub Roztocil 2012-08-21 15:45:22 +02:00
parent dc7d03e6b8
commit 9338aadd75
6 changed files with 45 additions and 54 deletions

View File

@ -3,7 +3,7 @@
NOTE: the CLI interface may change before reaching v1.0.
"""
import argparse
from argparse import FileType, OPTIONAL, SUPPRESS
from requests.compat import is_windows
@ -45,7 +45,7 @@ positional = parser.add_argument_group(
)
positional.add_argument(
'method', metavar='METHOD',
nargs='?',
nargs=OPTIONAL,
default=None,
help=_('''
The HTTP method to be used for the request
@ -114,9 +114,9 @@ content_type.add_argument(
output_processing = parser.add_argument_group(title='Output processing')
output_processing.add_argument(
'--output', '-o', type=argparse.FileType('w+b'),
'--output', '-o', type=FileType('w+b'),
metavar='FILE',
help= argparse.SUPPRESS if not is_windows else _(
help= SUPPRESS if not is_windows else _(
'''
Save output to FILE.
This option is a replacement for piping output to FILE,
@ -322,15 +322,13 @@ network.add_argument(
troubleshooting = parser.add_argument_group(title='Troubleshooting')
troubleshooting.add_argument(
'--help',
action='help', default=argparse.SUPPRESS,
help=argparse._('Show this help message and exit')
action='help', default=SUPPRESS,
help='Show this help message and exit'
)
troubleshooting.add_argument('--version', action='version', version=__version__)
troubleshooting.add_argument(
'--traceback', action='store_true', default=False,
help=_('''
Prints exception traceback should one occur.
''')
help='Prints exception traceback should one occur.'
)
troubleshooting.add_argument(
'--debug', action='store_true', default=False,

View File

@ -5,10 +5,10 @@ import os
import sys
import re
import json
import argparse
import mimetypes
import getpass
from io import BytesIO
from argparse import ArgumentParser, ArgumentTypeError
try:
from collections import OrderedDict
@ -79,7 +79,7 @@ OUTPUT_OPTIONS_DEFAULT = OUT_RESP_HEAD + OUT_RESP_BODY
OUTPUT_OPTIONS_DEFAULT_STDOUT_REDIRECTED = OUT_RESP_BODY
class Parser(argparse.ArgumentParser):
class Parser(ArgumentParser):
"""Adds additional logic to `argparse.ArgumentParser`.
Handles all input (CLI args, file args, stdin), applies defaults,
@ -125,7 +125,6 @@ class Parser(argparse.ArgumentParser):
# Stdin already read (if not a tty) so it's save to prompt.
args.auth.prompt_password(urlparse(args.url).netloc)
return args
def _print_message(self, message, file=None):
@ -171,7 +170,7 @@ class Parser(argparse.ArgumentParser):
args.items.insert(
0, KeyValueArgType(*SEP_GROUP_ITEMS).__call__(args.url))
except argparse.ArgumentTypeError as e:
except ArgumentTypeError as e:
if args.traceback:
raise
self.error(e.message)
@ -344,7 +343,7 @@ class KeyValueArgType(object):
break
else:
raise argparse.ArgumentTypeError(
raise ArgumentTypeError(
'"%s" is not a valid value' % string)
return self.key_value_class(
@ -383,7 +382,7 @@ class AuthCredentialsArgType(KeyValueArgType):
"""
try:
return super(AuthCredentialsArgType, self).__call__(string)
except argparse.ArgumentTypeError:
except ArgumentTypeError:
# No password provided, will prompt for it later.
return self.key_value_class(
key=string,

View File

@ -131,13 +131,15 @@ def make_stream(env, args):
RawStream,
chunk_size=RawStream.CHUNK_SIZE_BY_LINE
if args.stream
else RawStream.CHUNK_SIZE)
else RawStream.CHUNK_SIZE
)
elif args.prettify:
Stream = partial(
PrettyStream if args.stream else BufferedPrettyStream,
processor=OutputProcessor(env=env, groups=args.prettify,
pygments_style=args.style),
env=env)
env=env,
processor=OutputProcessor(
env=env, groups=args.prettify, pygments_style=args.style),
)
else:
Stream = partial(EncodedStream, env=env)
@ -205,6 +207,7 @@ class EncodedStream(BaseStream):
"""
CHUNK_SIZE = 1024 * 5
def __init__(self, env=Environment(), **kwargs):
super(EncodedStream, self).__init__(**kwargs)
@ -335,7 +338,8 @@ class HTTPLexer(lexer.RegexLexer):
token.Text,
token.String # Value
))
]}
]
}
class BaseProcessor(object):

View File

@ -1,13 +1,13 @@
"""Persistent, JSON-serialized sessions.
"""
import shutil
import os
import sys
import json
import glob
import errno
import codecs
import shutil
import subprocess
from requests.compat import urlparse
@ -24,7 +24,6 @@ SESSIONS_DIR = os.path.join(CONFIG_DIR, 'sessions')
def get_response(name, request_kwargs):
host = Host(request_kwargs['headers'].get('Host', None)
or urlparse(request_kwargs['url']).netloc.split('@')[-1])
@ -54,7 +53,6 @@ def get_response(name, request_kwargs):
class Host(object):
def __init__(self, name):
self.name = name
@ -86,7 +84,6 @@ class Host(object):
class Session(dict):
def __init__(self, host, name, *args, **kwargs):
super(Session, self).__init__(*args, **kwargs)
self.host = host
@ -128,9 +125,8 @@ class Session(dict):
def cookies(self):
jar = RequestsCookieJar()
for name, cookie_dict in self['cookies'].items():
cookie = create_cookie(
name, cookie_dict.pop('value'), **cookie_dict)
jar.set_cookie(cookie)
jar.set_cookie(create_cookie(
name, cookie_dict.pop('value'), **cookie_dict))
jar.clear_expired_cookies()
return jar
@ -200,12 +196,7 @@ def delete_command(args):
if not args.name:
host.delete()
else:
session = Session(host, args.name)
try:
session.delete()
except OSError as e:
if e.errno != errno.ENOENT:
raise
Session(host, args.name).delete()
def edit_command(args):
@ -220,7 +211,6 @@ def edit_command(args):
def add_commands(subparsers):
# List
list_ = subparsers.add_parser('session-list', help='list sessions')
list_.set_defaults(command=list_command)