From 53bf33dd56ae9524601870b5b4fab1c7ec434cee Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Sat, 28 Nov 2015 21:56:48 +0100 Subject: [PATCH] Open URLs automatically during OAuth --- vdirsyncer/storage/remotestorage.py | 7 ++++++- vdirsyncer/utils/__init__.py | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/vdirsyncer/storage/remotestorage.py b/vdirsyncer/storage/remotestorage.py index ffa4187..0f275f2 100644 --- a/vdirsyncer/storage/remotestorage.py +++ b/vdirsyncer/storage/remotestorage.py @@ -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): diff --git a/vdirsyncer/utils/__init__.py b/vdirsyncer/utils/__init__.py index 37e2d13..fce7411 100644 --- a/vdirsyncer/utils/__init__.py +++ b/vdirsyncer/utils/__init__.py @@ -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.')