2012-08-18 23:03:31 +02:00
|
|
|
"""
|
|
|
|
Provides the `httpie' management command.
|
|
|
|
|
|
|
|
Note that the main `http' command points to `httpie.__main__.main()`.
|
|
|
|
|
|
|
|
"""
|
2012-12-11 12:54:34 +01:00
|
|
|
import inspect
|
2012-08-18 23:03:31 +02:00
|
|
|
import argparse
|
2012-12-11 12:54:34 +01:00
|
|
|
import functools
|
2012-08-18 23:03:31 +02:00
|
|
|
|
|
|
|
from . import __version__
|
2012-12-11 12:54:34 +01:00
|
|
|
from .input import RegexValidator
|
|
|
|
from .sessions import (Session, Host,
|
|
|
|
command_session_list,
|
2012-12-01 18:16:00 +01:00
|
|
|
command_session_edit,
|
|
|
|
command_session_show,
|
|
|
|
command_session_delete)
|
2012-08-18 23:03:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='The HTTPie management command.',
|
|
|
|
version=__version__
|
|
|
|
)
|
|
|
|
subparsers = parser.add_subparsers()
|
|
|
|
|
|
|
|
|
2012-12-01 18:16:00 +01:00
|
|
|
#################################################################
|
|
|
|
# Session commands
|
|
|
|
#################################################################
|
|
|
|
|
2012-12-11 12:54:34 +01:00
|
|
|
hostname_validator = RegexValidator(
|
|
|
|
Host.VALID_NAME_PATTERN,
|
|
|
|
'Hostname contains invalid characters.'
|
|
|
|
)
|
|
|
|
session_name_validator = RegexValidator(
|
|
|
|
Session.VALID_NAME_PATTERN,
|
|
|
|
'Session name contains invalid characters.'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def make_command(func):
|
|
|
|
@functools.wraps(func)
|
|
|
|
def wrapper(parsed_args):
|
|
|
|
"""Convert parsed args to function kwargs."""
|
|
|
|
kwargs = dict((name, getattr(parsed_args, name, None))
|
|
|
|
for name in inspect.getargspec(func).args)
|
|
|
|
return func(**kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def add_hostname_arg(parser, *args, **kwargs):
|
|
|
|
parser.add_argument(
|
|
|
|
'hostname', metavar='HOSTNAME',
|
|
|
|
type=hostname_validator,
|
|
|
|
*args, **kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def add_session_name_arg(parser, *args, **kwargs):
|
|
|
|
parser.add_argument(
|
|
|
|
'session_name', metavar='SESSION_NAME',
|
|
|
|
type=session_name_validator,
|
|
|
|
*args, **kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2012-12-01 18:16:00 +01:00
|
|
|
session = subparsers.add_parser('session',
|
|
|
|
help='manipulate and inspect sessions').add_subparsers()
|
|
|
|
|
|
|
|
# List
|
2012-12-11 12:54:34 +01:00
|
|
|
session_list_parser = session.add_parser('list', help='list sessions')
|
|
|
|
session_list_parser.set_defaults(command=make_command(command_session_list))
|
|
|
|
add_hostname_arg(session_list_parser, nargs=argparse.OPTIONAL)
|
|
|
|
|
2012-12-01 18:16:00 +01:00
|
|
|
|
|
|
|
# Show
|
2012-12-11 12:54:34 +01:00
|
|
|
session_show_parser = session.add_parser('show', help='show a session')
|
|
|
|
session_show_parser.set_defaults(command=make_command(command_session_show))
|
|
|
|
add_hostname_arg(session_show_parser)
|
|
|
|
add_session_name_arg(session_show_parser)
|
|
|
|
|
2012-12-01 18:16:00 +01:00
|
|
|
|
|
|
|
# Edit
|
2012-12-11 12:54:34 +01:00
|
|
|
session_edit_parser = session.add_parser(
|
2012-12-01 18:16:00 +01:00
|
|
|
'edit', help='edit a session in $EDITOR')
|
2012-12-11 12:54:34 +01:00
|
|
|
session_edit_parser.set_defaults(command=make_command(command_session_edit))
|
|
|
|
add_hostname_arg(session_edit_parser)
|
|
|
|
add_session_name_arg(session_edit_parser)
|
2012-12-01 18:16:00 +01:00
|
|
|
|
|
|
|
# Delete
|
2012-12-11 12:54:34 +01:00
|
|
|
session_delete_parser = session.add_parser('delete', help='delete a session')
|
|
|
|
session_delete_parser.set_defaults(
|
|
|
|
command=make_command(command_session_delete))
|
|
|
|
add_hostname_arg(session_delete_parser)
|
|
|
|
add_session_name_arg(session_delete_parser, nargs=argparse.OPTIONAL,
|
2012-12-01 18:16:00 +01:00
|
|
|
help='The name of the session to be deleted.'
|
2012-12-11 12:54:34 +01:00
|
|
|
' If not specified, all of the host\'s')
|
|
|
|
|
|
|
|
|
|
|
|
#################################################################
|
|
|
|
# Main
|
|
|
|
#################################################################
|
2012-08-18 23:03:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
args = parser.parse_args()
|
2012-08-19 04:58:14 +02:00
|
|
|
args.command(args)
|
2012-08-18 23:03:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|