#
# Copyright (c) 2011-2015 Genestack Limited
# All Rights Reserved
# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF GENESTACK LIMITED
# The copyright notice above does not evidence any
# actual or intended publication of such source code.
#
import argparse
import sys


def isatty():
    try:
        return sys.stdout.isatty()
    except AttributeError:
        return False


def make_connection_parser(user=None, password=None, host=None):
    """
    Create argument parser to with connection parameters.
    If one of email, password or user specified use them as default, else use data form user object.
    """
    parser = argparse.ArgumentParser()
    group = parser.add_argument_group('connection')
    group.add_argument('-H', '--host', default=host, help="server host", metavar='<host>')
    group.add_argument('-u', dest='user', metavar='<user>', default=user, help='user alias from settings or email')
    group.add_argument('-p', dest='pwd', default=password, metavar='<password>', help='user password')
    return parser


def get_user(args=None):
    """
    Return user corresponding to arguments.
    If arguments is None use connection argument parser to get arguments.

    :return:
    """

    from settings import config, User

    if args is None:
        args = make_connection_parser().parse_args()

    alias = args.user
    if not args.host and not args.pwd:
        if not alias and config.default_user:
            return config.default_user
        if alias in config.users:
            return config.users[alias]
    return User(email=alias, host=args.host, password=args.pwd)


def get_connection(args=None):
    """
    Shortcut to get_user().get_connection()
    """
    user = get_user(args)
    return user.get_connection(interactive=True)

@@ -278,16 +278,101 @@
 atty():%0A
+    %22%22%22%0A    True if the file is connected to a tty device.%0A%0A    :rtype: bool%0A    %22%22%22%0A
     try:
@@ -672,18 +672,18 @@
 e data f
-o
 r
+o
 m user o
@@ -688,16 +688,231 @@
  object.
+%0A%0A    :param user: user alias or email%0A    :type user: str%0A    :param password: user password%0A    :type password: str%0A    :param host: host%0A    :type host: str%0A    :return: parser%0A    :rtype: argparse.ArgumentParser
 %0A    %22%22%22
@@ -1443,34 +1443,42 @@
 use 
-connection argument 
+:function:%60make_connection_
 parser
+%60
  to 
@@ -1505,16 +1505,62 @@
 :return:
+ user%0A    :rtype: genestack.settings.User.User
 %0A    %22%22%22
@@ -2042,16 +2042,179 @@
 ection()
+%0A%0A    :param args: argument from :attr:%60argparse.parse_args%60%0A    :type args: argparse.Namespace%0A    :return: connection%0A    :rtype: genestack.Connection.Connection
 %0A    %22%22%22

Add docstring to utils.