~cjwatson/launchpadlib/isolate-doctests

« back to all changes in this revision

Viewing changes to src/launchpadlib/credentials.py

  • Committer: Dimitri John Ledkov
  • Date: 2014-11-08 12:26:25 UTC
  • mfrom: (120.1.5 bug-643699)
  • Revision ID: dimitri.j.ledkov@intel.com-20141108122625-q42zrp6npojdu728
[r=benji,bug=643699] Add a delay before attempting to launch browser (landed for stefanor)

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
 
38
38
import httplib2
39
39
import os
 
40
from select import select
40
41
import stat
 
42
from sys import stdin
41
43
import time
42
44
try:
43
45
    from urllib.parse import urlencode
583
585
    themselves.
584
586
    """
585
587
 
586
 
    WAITING_FOR_USER = "The authorization page:\n (%s)\nshould be opening in your browser. Use your browser to authorize\nthis program to access Launchpad on your behalf. \n\nWaiting to hear from Launchpad about your decision..."
 
588
    WAITING_FOR_USER = (
 
589
        "The authorization page:\n"
 
590
        " (%s)\n"
 
591
        "should be opening in your browser. Use your browser to authorize\n"
 
592
        "this program to access Launchpad on your behalf.")
 
593
    TIMEOUT_MESSAGE = "Press Enter to continue or wait (%d) seconds..."
 
594
    TIMEOUT = 5
 
595
    WAITING_FOR_LAUNCHPAD = (
 
596
            "Waiting to hear from Launchpad about your decision...")
 
597
    TERMINAL_BROWSERS = ('www-browser', 'links', 'links2', 'lynx',
 
598
                         'elinks', 'elinks-lite', 'netrik', 'w3m')
587
599
 
588
600
    def __init__(self, service_root, application_name, consumer_name=None,
589
601
                 credential_save_failed=None, allow_access_levels=None):
619
631
        """Have the end-user authorize the token in their browser."""
620
632
 
621
633
        authorization_url = self.authorization_url(request_token)
622
 
        webbrowser.open(authorization_url)
623
634
        self.output(self.WAITING_FOR_USER % authorization_url)
 
635
 
 
636
        try:
 
637
            browser = webbrowser.get().basename
 
638
            console_browser = browser in self.TERMINAL_BROWSERS
 
639
        except webbrowser.Error:
 
640
            browser = None
 
641
            console_browser = False
 
642
 
 
643
        if console_browser:
 
644
            self.output(self.TIMEOUT_MESSAGE % self.TIMEOUT)
 
645
            # Wait a little time before attempting to launch browser,
 
646
            # give users the chance to press a key to skip it anyway.
 
647
            rlist, _, _ = select([stdin], [], [], self.TIMEOUT)
 
648
            if rlist:
 
649
                stdin.readline()
 
650
 
 
651
        self.output(self.WAITING_FOR_LAUNCHPAD)
 
652
        if browser is not None:
 
653
            webbrowser.open(authorization_url)
624
654
        while credentials.access_token is None:
625
655
            time.sleep(access_token_poll_time)
626
656
            try: