Merge lp:~stefanor/launchpadlib/bug-643699 into lp:launchpadlib

Proposed by Stefano Rivera
Status: Merged
Approved by: Benji York
Approved revision: 125
Merged at revision: 138
Proposed branch: lp:~stefanor/launchpadlib/bug-643699
Merge into: lp:launchpadlib
Diff against target: 60 lines (+32/-2)
1 file modified
src/launchpadlib/credentials.py (+32/-2)
To merge this branch: bzr merge lp:~stefanor/launchpadlib/bug-643699
Reviewer Review Type Date Requested Status
Benji York (community) code Approve
Review via email: mp+104572@code.launchpad.net

Commit message

[r=benji,bug=643699] Add a delay before attempting to launch browser (landed for stefanor)

Description of the change

As I've taken over Debian maintainance of this package, I should help out in it's upstream merge requests too.

This supersedes https://code.launchpad.net/~dktrkranz/launchpadlib/bug-643699/+merge/78650

To post a comment you must log in.
Revision history for this message
Benji York (benji) wrote :

Thanks for the branch. It looks good.

I suspect an incoming signal can cause the select to return before TIMEOUT and with an empty rlist, but that should be rare and would only cause a shorter than usual delay, so I wouldn't add any code to compensate for that possibility.

Would you like for me to land this branch for you?

review: Approve (code)
Revision history for this message
Stefano Rivera (stefanor) wrote :

Yes please, thanks.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/launchpadlib/credentials.py'
2--- src/launchpadlib/credentials.py 2011-12-05 18:53:25 +0000
3+++ src/launchpadlib/credentials.py 2012-05-03 15:20:25 +0000
4@@ -31,7 +31,9 @@
5 from cStringIO import StringIO
6 import httplib2
7 import os
8+from select import select
9 import stat
10+from sys import stdin
11 import time
12 from urllib import urlencode
13 from urlparse import urljoin
14@@ -530,7 +532,17 @@
15 themselves.
16 """
17
18- 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..."
19+ WAITING_FOR_USER = (
20+ "The authorization page:\n"
21+ " (%s)\n"
22+ "should be opening in your browser. Use your browser to authorize\n"
23+ "this program to access Launchpad on your behalf.")
24+ TIMEOUT_MESSAGE = "Press Enter to continue or wait (%d) seconds..."
25+ TIMEOUT = 5
26+ WAITING_FOR_LAUNCHPAD = (
27+ "Waiting to hear from Launchpad about your decision...")
28+ TERMINAL_BROWSERS = ('www-browser', 'links', 'links2', 'lynx',
29+ 'elinks', 'elinks-lite', 'netrik', 'w3m')
30
31 def __init__(self, service_root, application_name, consumer_name=None,
32 credential_save_failed=None, allow_access_levels=None):
33@@ -566,8 +578,26 @@
34 """Have the end-user authorize the token in their browser."""
35
36 authorization_url = self.authorization_url(request_token)
37- webbrowser.open(authorization_url)
38 self.output(self.WAITING_FOR_USER % authorization_url)
39+
40+ try:
41+ browser = webbrowser.get().basename
42+ console_browser = browser in self.TERMINAL_BROWSERS
43+ except webbrowser.Error:
44+ browser = None
45+ console_browser = False
46+
47+ if console_browser:
48+ self.output(self.TIMEOUT_MESSAGE % self.TIMEOUT)
49+ # Wait a little time before attempting to launch browser,
50+ # give users the chance to press a key to skip it anyway.
51+ rlist, _, _ = select([stdin], [], [], self.TIMEOUT)
52+ if rlist:
53+ stdin.readline()
54+
55+ self.output(self.WAITING_FOR_LAUNCHPAD)
56+ if browser is not None:
57+ webbrowser.open(authorization_url)
58 while credentials.access_token is None:
59 time.sleep(access_token_poll_time)
60 try:

Subscribers

People subscribed via source and target branches