jenkins-bot has submitted this change and it was merged.
Change subject: [FEAT] bot: add open_webbrowser ......................................................................
[FEAT] bot: add open_webbrowser
This adds a open_webbrowser method to the bot module to open the webbrowser with the given page. The Page object now also supports to get the full URL and the Family object can now returns a URI now with the correct hostname and path prefix selected depending on the fact that SSL is used.
Change-Id: Ic4f340b42981121216af3ab1f6cee00289fb6b47 --- M pywikibot/bot.py M pywikibot/comms/http.py M pywikibot/family.py M pywikibot/page.py M scripts/add_text.py M scripts/interwiki.py M scripts/replace.py 7 files changed, 42 insertions(+), 43 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/bot.py b/pywikibot/bot.py index 0bb79ba..ef9673b 100644 --- a/pywikibot/bot.py +++ b/pywikibot/bot.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """User-interface related functions for building bots.""" # -# (C) Pywikibot team, 2008-2014 +# (C) Pywikibot team, 2008-2015 # # Distributed under the terms of the MIT license. # @@ -16,11 +16,12 @@ import logging.handlers # all output goes thru python std library "logging" module
-import os -import sys -import re -import json import datetime +import json +import os +import re +import sys +import webbrowser
_logger = "bot"
@@ -847,6 +848,13 @@ pywikibot.stdout(globalHelp)
+def open_webbrowser(page): + """Open the web browser displaying the page and wait for input.""" + from pywikibot import i18n + webbrowser.open(page.full_url()) + i18n.input('pywikibot-enter-finished-browser') + + class QuitKeyboardInterrupt(KeyboardInterrupt):
"""The user has cancelled processing at a prompt.""" diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py index 444f3c7..3da6d31 100644 --- a/pywikibot/comms/http.py +++ b/pywikibot/comms/http.py @@ -45,7 +45,6 @@ if sys.version_info[0] > 2: from ssl import SSLError as SSLHandshakeError import queue as Queue - import urllib.parse as urlparse from http import cookiejar as cookielib from urllib.parse import quote else: @@ -55,7 +54,6 @@ from httplib2 import ServerNotFoundError as SSLHandshakeError
import Queue - import urlparse import cookielib from urllib2 import quote
@@ -247,13 +245,7 @@ r = fetch(uri, *args, **kwargs) return r.content
- proto = site.protocol() - if proto == 'https': - host = site.ssl_hostname() - uri = site.ssl_pathprefix() + uri - else: - host = site.hostname() - baseuri = urlparse.urljoin("%s://%s" % (proto, host), uri) + baseuri = site.base_url(uri)
kwargs.setdefault("disable_ssl_certificate_validation", site.ignore_certificate_error()) diff --git a/pywikibot/family.py b/pywikibot/family.py index b545ee0..5850da8 100644 --- a/pywikibot/family.py +++ b/pywikibot/family.py @@ -15,9 +15,9 @@ import imp
if sys.version_info[0] > 2: - from urllib.parse import urlparse + import urllib.parse as urlparse else: - from urlparse import urlparse + import urlparse
import pywikibot from pywikibot import config2 as config @@ -1015,6 +1015,15 @@ # Override this ONLY if the wiki family requires a path prefix return ''
+ def base_url(self, code, uri): + protocol = self.protocol(code) + if protocol == 'https': + host = self.ssl_hostname(code) + uri = self.ssl_pathprefix(code) + uri + else: + host = self.hostname(code) + return urlparse.urljoin('{0}://{1}'.format(protocol, host), uri) + def path(self, code): return '%s/index.php' % self.scriptpath(code)
@@ -1244,7 +1253,7 @@ """Constructor.""" super(AutoFamily, self).__init__() self.name = name - self.url = urlparse(url) + self.url = urlparse.urlparse(url) self.langs = { name: self.url.netloc } diff --git a/pywikibot/page.py b/pywikibot/page.py index f8f2c47..45e41c3 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -10,19 +10,19 @@
""" # -# (C) Pywikibot team, 2008-2014 +# (C) Pywikibot team, 2008-2015 # # Distributed under the terms of the MIT license. # __version__ = '$Id$' #
-import sys -import logging -import re -import unicodedata import collections import hashlib +import logging +import re +import sys +import unicodedata
try: from collections import OrderedDict @@ -293,6 +293,11 @@ """ return hash(unicode(self))
+ def full_url(self): + """Return the full URL.""" + return self.site.base_url(self.site.nice_get_address(self.title( + asUrl=True))) + def autoFormat(self): """Return L{date.getAutoFormat} dictName and value, if any.
diff --git a/scripts/add_text.py b/scripts/add_text.py index 69ab777..3bfc2b9 100644 --- a/scripts/add_text.py +++ b/scripts/add_text.py @@ -64,7 +64,7 @@
# # (C) Filnik, 2007-2010 -# (C) Pywikibot team, 2007-2014 +# (C) Pywikibot team, 2007-2015 # # Distributed under the terms of the MIT license. # @@ -72,7 +72,6 @@ #
import re -import webbrowser import codecs import time
@@ -231,11 +230,7 @@ elif choice == 'n': return (False, False, always) elif choice == 'b': - webbrowser.open("http://%s%s" % ( - site.hostname(), - site.nice_get_address(page.title(asUrl=True)) - )) - pywikibot.input("Press Enter when finished in browser.") + pywikibot.bot.open_webbrowser(page) if always or choice == 'y': try: if always: diff --git a/scripts/interwiki.py b/scripts/interwiki.py index 55d5667..1158110 100755 --- a/scripts/interwiki.py +++ b/scripts/interwiki.py @@ -338,7 +338,7 @@ # (C) Daniel Herding, 2004 # (C) Yuri Astrakhan, 2005-2006 # (C) xqt, 2009-2014 -# (C) Pywikibot team, 2007-2014 +# (C) Pywikibot team, 2007-2015 # # Distributed under the terms of the MIT license. # @@ -353,7 +353,6 @@ import codecs import pickle import socket -import webbrowser import pywikibot from pywikibot import config, i18n, pagegenerators, textlib, interwiki_graph, titletranslate
@@ -1987,11 +1986,7 @@ ('Always', 'a')], automatic_quit=False) if answer == 'b': - webbrowser.open("http://%s%s" % ( - page.site.hostname(), - page.site.nice_get_address(page.title(asUrl=True)) - )) - pywikibot.input(u"Press Enter when finished in browser.") + pywikibot.bot.open_webbrowser(page) return True elif answer == 'a': # don't ask for the rest of this subject diff --git a/scripts/replace.py b/scripts/replace.py index a8fc8cd..ee1a67c 100755 --- a/scripts/replace.py +++ b/scripts/replace.py @@ -118,7 +118,7 @@ """ # # (C) Daniel Herding, 2004-2012 -# (C) Pywikibot team, 2004-2014 +# (C) Pywikibot team, 2004-2015 # # Distributed under the terms of the MIT license. # @@ -127,7 +127,6 @@
import re import time -import webbrowser import sys
import pywikibot @@ -593,11 +592,7 @@ new_text = as_edited continue if choice == 'b': - webbrowser.open("http://%s%s" % ( - page.site.hostname(), - page.site.nice_get_address(page.title(asUrl=True)) - )) - i18n.input('pywikibot-enter-finished-browser') + pywikibot.bot.open_webbrowser(page) try: original_text = page.get(get_redirect=True, force=True) except pywikibot.NoPage: