# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2015 Digi International, Inc.
from getpass import getpass
import os
from devicecloud import DeviceCloud


def get_authenticated_dc():
    while True:
        base_url = os.environ.get('DC_BASE_URL', 'https://login.etherios.com')

        username = os.environ.get('DC_USERNAME', None)
        if not username:
            username = input("username: ")

        password = os.environ.get('DC_PASSWORD', None)
        if not password:
            password = getpass("password: ")

        dc = DeviceCloud(username, password, base_url=base_url)
        if dc.has_valid_credentials():
            print("Credentials accepted!")
            return dc
        else:
            print("Invalid username or password provided, try again")

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2015 Digi International, Inc.
from getpass import getpass
import os
from six.moves import input
from devicecloud import DeviceCloud


def get_authenticated_dc():
    while True:
        base_url = os.environ.get('DC_BASE_URL', 'https://login.etherios.com')

        username = os.environ.get('DC_USERNAME', None)
        if not username:
            username = input("username: ")

        password = os.environ.get('DC_PASSWORD', None)
        if not password:
            password = getpass("password: ")

        dc = DeviceCloud(username, password, base_url=base_url)
        if dc.has_valid_credentials():
            print("Credentials accepted!")
            return dc
        else:
            print("Invalid username or password provided, try again")

Handle using the input function in python 2 for getting username for examples
Handle using the input function in python 2 for getting username for examples

Previously this used the builtin input function to get the username.  In
python 3 this is fine, but if python 2 this is equivalent to

eval(raw_input(prompt))

and thus tried to evaluate the username as a variable and typically failed.
