def read_options()

in pylib/cqlshlib/cqlshmain.py [0:0]


def read_options(cmdlineargs, parser, config_file, cql_dir, environment=os.environ):
    configs = configparser.ConfigParser()
    configs.read(config_file)

    rawconfigs = configparser.RawConfigParser()
    rawconfigs.read(config_file)

    username_from_cqlshrc = option_with_default(configs.get, 'authentication', 'username')
    password_from_cqlshrc = option_with_default(rawconfigs.get, 'authentication', 'password')
    if username_from_cqlshrc or password_from_cqlshrc:
        if password_from_cqlshrc and not is_file_secure(os.path.expanduser(config_file)):
            print("\nWarning: Password is found in an insecure cqlshrc file. The file is owned or readable by other users on the system.",
                  end='', file=sys.stderr)
        print("\nNotice: Credentials in the cqlshrc file is deprecated and will be ignored in the future."
              "\nPlease use a credentials file to specify the username and password.\n", file=sys.stderr)

    argvalues = argparse.Namespace()

    argvalues.username = None
    argvalues.password = None
    argvalues.credentials = os.path.expanduser(option_with_default(configs.get, 'authentication', 'credentials',
                                                                   os.path.join(cql_dir, 'credentials')))
    argvalues.keyspace = option_with_default(configs.get, 'authentication', 'keyspace')
    argvalues.browser = option_with_default(configs.get, 'ui', 'browser', None)
    argvalues.completekey = option_with_default(configs.get, 'ui', 'completekey',
                                                DEFAULT_COMPLETEKEY)
    argvalues.color = option_with_default(configs.getboolean, 'ui', 'color')
    argvalues.time_format = raw_option_with_default(configs, 'ui', 'time_format',
                                                    DEFAULT_TIMESTAMP_FORMAT)
    argvalues.nanotime_format = raw_option_with_default(configs, 'ui', 'nanotime_format',
                                                        DEFAULT_NANOTIME_FORMAT)
    argvalues.date_format = raw_option_with_default(configs, 'ui', 'date_format',
                                                    DEFAULT_DATE_FORMAT)
    argvalues.float_precision = option_with_default(configs.getint, 'ui', 'float_precision',
                                                    Shell.DEFAULT_FLOAT_PRECISION)
    argvalues.double_precision = option_with_default(configs.getint, 'ui', 'double_precision',
                                                     Shell.DEFAULT_DOUBLE_PRECISION)
    argvalues.max_trace_wait = option_with_default(configs.getfloat, 'tracing', 'max_trace_wait',
                                                   Shell.DEFAULT_MAX_TRACE_WAIT)
    argvalues.timezone = option_with_default(configs.get, 'ui', 'timezone', None)

    argvalues.debug = False

    argvalues.coverage = False
    if 'CQLSH_COVERAGE' in environment.keys():
        argvalues.coverage = True

    argvalues.file = None
    argvalues.ssl = option_with_default(configs.getboolean, 'connection', 'ssl', Shell.DEFAULT_SSL)
    argvalues.encoding = option_with_default(configs.get, 'ui', 'encoding', UTF8)

    argvalues.tty = option_with_default(configs.getboolean, 'ui', 'tty', sys.stdin.isatty())
    argvalues.protocol_version = option_with_default(configs.getint, 'protocol', 'version', None)
    argvalues.cqlversion = option_with_default(configs.get, 'cql', 'version', None)
    argvalues.connect_timeout = option_with_default(configs.getint, 'connection', 'timeout', Shell.DEFAULT_CONNECT_TIMEOUT_SECONDS)
    argvalues.request_timeout = option_with_default(configs.getint, 'connection', 'request_timeout', Shell.DEFAULT_REQUEST_TIMEOUT_SECONDS)
    argvalues.execute = None
    argvalues.insecure_password_without_warning = False

    options, arguments = parser.parse_known_args(cmdlineargs, argvalues)

    # Credentials from cqlshrc will be expanded,
    # credentials from the command line are also expanded if there is a space...
    # we need the following so that these two scenarios will work
    #   cqlsh --credentials=~/.cassandra/creds
    #   cqlsh --credentials ~/.cassandra/creds

    if options.credentials is not None:
        options.credentials = os.path.expanduser(options.credentials)

    if options.credentials is not None:
        if not is_file_secure(options.credentials):
            print("\nWarning: Credentials file '{0}' exists but is not used, because:"
                  "\n  a. the file owner is not the current user; or"
                  "\n  b. the file is readable by group or other."
                  "\nPlease ensure the file is owned by the current user and is not readable by group or other."
                  "\nOn a Linux or UNIX-like system, you often can do this by using the `chown` and `chmod` commands:"
                  "\n  chown YOUR_USERNAME credentials"
                  "\n  chmod 600 credentials\n".format(options.credentials),
                  file=sys.stderr)
            options.credentials = ''  # ConfigParser.read() will ignore unreadable files

    if not options.username:
        credentials = configparser.ConfigParser()
        if options.credentials is not None:
            credentials.read(options.credentials)

        # use the username from credentials file but fallback to cqlshrc if username is absent from the command line parameters
        options.username = option_with_default(credentials.get, 'plain_text_auth', 'username', username_from_cqlshrc)

    if not options.password:
        rawcredentials = configparser.RawConfigParser()
        if options.credentials is not None:
            rawcredentials.read(options.credentials)

        # handling password in the same way as username, priority cli > credentials > cqlshrc
        options.password = option_with_default(rawcredentials.get, 'plain_text_auth', 'password', password_from_cqlshrc)
    elif not options.insecure_password_without_warning:
        print("\nWarning: Using a password on the command line interface can be insecure."
              "\nRecommendation: use the credentials file to securely provide the password.\n", file=sys.stderr)

    hostname = option_with_default(configs.get, 'connection', 'hostname', Shell.DEFAULT_HOST)
    port = option_with_default(configs.get, 'connection', 'port', Shell.DEFAULT_PORT)

    hostname = environment.get('CQLSH_HOST', hostname)
    port = environment.get('CQLSH_PORT', port)

    try:
        options.connect_timeout = int(options.connect_timeout)
    except ValueError:
        parser.error('"%s" is not a valid connect timeout.' % (options.connect_timeout,))
        options.connect_timeout = Shell.DEFAULT_CONNECT_TIMEOUT_SECONDS

    try:
        options.request_timeout = int(options.request_timeout)
    except ValueError:
        parser.error('"%s" is not a valid request timeout.' % (options.request_timeout,))
        options.request_timeout = Shell.DEFAULT_REQUEST_TIMEOUT_SECONDS

    if len(arguments) > 0:
        hostname = arguments[0]
    if len(arguments) > 1:
        port = arguments[1]

    if options.file or options.execute:
        options.tty = False

    if options.execute and not options.execute.endswith(';'):
        options.execute += ';'

    if argvalues.color in (True, False):
        options.color = argvalues.color
    else:
        if options.file is not None:
            options.color = False
        else:
            options.color = should_use_color()

    try:
        port = int(port)
    except ValueError:
        parser.error('%r is not a valid port number.' % port)
    return options, hostname, port