示例#1
0
def create_session():
    from coursera.coursera_dl import get_session
    from coursera.credentials import get_credentials
    from coursera.cookies import login

    session = get_session()
    username, password = get_credentials(netrc=expanduser('~/.netrc'))
    login(session, username, password)
    return session
示例#2
0
def create_session():
    from coursera.coursera_dl import get_session
    from coursera.credentials import get_credentials
    from coursera.cookies import login

    session = get_session()
    username, password = get_credentials(netrc=expanduser('~/.netrc'))
    login(session, username, password)
    return session
示例#3
0
def test_get_credentials_with_keyring():
    if not credentials.keyring:
        return None
    test_get_credentials_with_username_given(True)

    # Test again, this time without getpass
    username, password = credentials.get_credentials(username='******',
                                                     use_keyring=True)
    assert username == 'user'
    assert password == 'pass'
示例#4
0
def test_get_credentials_with_keyring():
    if not credentials.keyring:
        return None
    test_get_credentials_with_username_given(True)

    # Test again, this time without getpass
    username, password = credentials.get_credentials(username='******',
                                                     use_keyring=True)
    assert username == 'user'
    assert password == 'pass'
    def test_get_credentials_with_username_given(self):
        import getpass
        _getpass = getpass.getpass
        getpass.getpass = lambda x: 'pass'

        username, password = credentials.get_credentials(username='******')
        self.assertEquals(username, 'user')
        self.assertEquals(password, 'pass')

        getpass.getpass = _getpass
def test_get_credentials_with_username_given():
    import getpass
    _getpass = getpass.getpass
    getpass.getpass = lambda x: 'pass'

    username, password = credentials.get_credentials(
        username='******')
    assertEquals(username, 'user')
    assertEquals(password, 'pass')

    getpass.getpass = _getpass
示例#7
0
def test_get_credentials_with_username_given(use_keyring=False):
    import getpass
    _getpass = getpass.getpass
    getpass.getpass = lambda x: 'pass'

    username, password = credentials.get_credentials(username='******',
                                                     use_keyring=use_keyring)
    assert username == 'user'
    assert password == 'pass'

    getpass.getpass = _getpass
示例#8
0
def test_get_credentials_with_username_given(use_keyring=False):
    import getpass
    _getpass = getpass.getpass
    getpass.getpass = lambda x: 'pass'

    username, password = credentials.get_credentials(username='******',
                                                     use_keyring=use_keyring)
    assert username == 'user'
    assert password == 'pass'

    getpass.getpass = _getpass
def test_get_credentials_with_username_and_password_given():
    username, password = credentials.get_credentials(
        username='******', password='******')
    assertEquals(username, 'user')
    assertEquals(password, 'pass')
def test_get_credentials_with_netrc():
    username, password = credentials.get_credentials(netrc=NETRC)
    assertEquals(username, '*****@*****.**')
    assertEquals(password, 'secret')
示例#11
0
def test_get_credentials_with_username_and_password_given():
    username, password = credentials.get_credentials(username='******',
                                                     password='******')
    assert username == 'user'
    assert password == 'pass'
示例#12
0
def test_get_credentials_with_netrc():
    username, password = credentials.get_credentials(netrc=NETRC)
    assert username == '*****@*****.**'
    assert password == 'secret'
示例#13
0
def test_get_credentials_with_username_and_password_given():
    username, password = credentials.get_credentials(username='******',
                                                     password='******')
    assert username == 'user'
    assert password == 'pass'
示例#14
0
def test_get_credentials_with_netrc():
    username, password = credentials.get_credentials(netrc=NETRC)
    assert username == '*****@*****.**'
    assert password == 'secret'
示例#15
0
def parse_args(args=None):
    """
    Parse the arguments/options passed to the program on the command line.
    """

    parser = argparse.ArgumentParser(
        description='Download Coursera.org lecture material and resources.')

    # Basic options
    group_basic = parser.add_argument_group('Basic options')

    group_basic.add_argument('class_names',
                             action='store',
                             nargs='+',
                             help='name(s) of the class(es) (e.g. "ml-005")')

    group_basic.add_argument('-u',
                             '--username',
                             dest='username',
                             action='store',
                             default=None,
                             help='coursera username')

    group_basic.add_argument('-p',
                             '--password',
                             dest='password',
                             action='store',
                             default=None,
                             help='coursera password')

    group_basic.add_argument('--path',
                             dest='path',
                             action='store',
                             default='',
                             help='path to where to save the file. (Default: current directory)')
    
    # Advanced authentication
    group_adv_auth = parser.add_argument_group('Advanced authentication options')

    group_adv_auth.add_argument('-c',
                                '--cookies_file',
                                dest='cookies_file',
                                action='store',
                                default=None,
                                help='full path to the cookies.txt file')

    group_adv_auth.add_argument('-n',
                                '--netrc',
                                dest='netrc',
                                nargs='?',
                                action='store',
                                const=True,
                                default=False,
                                help='use netrc for reading passwords, uses default'
                                ' location if no path specified')

    group_adv_auth.add_argument('-k',
                                '--keyring',
                                dest='use_keyring',
                                action='store_true',
                                default=False,
                                help='use keyring provided by operating system to '
                                'save and load credentials')

    group_adv_auth.add_argument('--clear-cache',
                                dest='clear_cache',
                                action='store_true',
                                default=False,
                                help='clear cached cookies')

    # Debug options
    group_debug = parser.add_argument_group('Debugging options')

    group_debug.add_argument('--skip-download',
                             dest='skip_download',
                             action='store_true',
                             default=False,
                             help='for debugging: skip actual downloading of files')

    group_debug.add_argument('--debug',
                             dest='debug',
                             action='store_true',
                             default=False,
                             help='print lots of debug information')

    group_debug.add_argument('--version',
                             dest='version',
                             action='store_true',
                             default=False,
                             help='display version and exit')

    group_debug.add_argument('-l',  # FIXME: remove short option from rarely used ones
                             '--process_local_page',
                             dest='local_page',
                             help='uses or creates local cached version of syllabus'
                             ' page')

    # Final parsing of the options
    args = parser.parse_args(args)

    # Initialize the logging system first so that other functions
    # can use it right away
    if args.debug:
        logging.basicConfig(level=logging.DEBUG,
                            format='%(name)s[%(funcName)s] %(message)s')
    else:
        logging.basicConfig(level=logging.INFO,
                            format='%(message)s')

    # show version?
    if args.version:
        # we use print (not logging) function because version may be used
        # by some external script while logging may output excessive information
        print(__version__)
        sys.exit(0)

    # decode path so we can work properly with cyrillic symbols on different
    # versions on Python
    args.path = decode_input(args.path)

    # check arguments
    if args.use_keyring and args.password:
        logging.warning('--keyring and --password cannot be specified together')
        args.use_keyring = False

    if args.use_keyring and not keyring:
        logging.warning('The python module `keyring` not found.')
        args.use_keyring = False

    if args.cookies_file and not os.path.exists(args.cookies_file):
        logging.error('Cookies file not found: %s', args.cookies_file)
        sys.exit(1)

    if not args.cookies_file:
        try:
            args.username, args.password = get_credentials(
                username=args.username, password=args.password,
                netrc=args.netrc, use_keyring=args.use_keyring)
        except CredentialsError as e:
            logging.error(e)
            sys.exit(1)

    return args