Open URLs automatically during OAuth

This commit is contained in:
Markus Unterwaditzer 2015-11-28 21:56:48 +01:00
parent 2f3b2cb4f1
commit 53bf33dd56
2 changed files with 31 additions and 1 deletions

View file

@ -98,8 +98,13 @@ class Session(object):
self._session.authorization_url(self.endpoints['oauth'])
click.echo('Opening {} ...'.format(authorization_url))
webbrowser.open(authorization_url)
try:
utils.open_graphical_browser(authorization_url)
except Exception as e:
logger.warning(str(e))
click.echo('Follow the instructions on the page.')
webbrowser.open(authorization_url)
raise exceptions.UserError('Aborted!')
def _discover_endpoints(self, subpath):

View file

@ -193,3 +193,28 @@ def synchronized(lock=None):
return f(*args, **kwargs)
return wrapper
return inner
def open_graphical_browser(url, new=0, autoraise=True):
'''Open a graphical web browser.
This is basically like `webbrowser.open`, but without trying to launch CLI
browsers at all. We're excluding those since it's undesirable to launch
those when you're using vdirsyncer on a server. Rather copypaste the URL
into the local browser, or use the URL-yanking features of your terminal
emulator.
'''
import webbrowser
cli_names = set(['www-browser', 'links', 'links2', 'elinks', 'lynx',
'w3m'])
for name in webbrowser._tryorder:
browser = webbrowser.get(name)
if browser in cli_names:
continue
if browser.open(url, new, autoraise):
return
raise RuntimeError('No graphical browser found. Please open the URL '
'manually.')