Use six to standardize imports between Python 2 and Python 3

This commit is contained in:
Labrys 2016-06-04 23:53:26 -04:00
commit cf1ae938fc
4 changed files with 33 additions and 34 deletions

View file

@ -1,4 +1,6 @@
# coding=utf-8
from six.moves import reload_module
import locale
import os
import re
@ -252,7 +254,7 @@ def initialize(section=None):
SYS_ENCODING = 'UTF-8'
if not hasattr(sys, "setdefaultencoding"):
reload(sys)
reload_module(sys)
try:
# pylint: disable=E1101

View file

@ -18,12 +18,8 @@ from core.transmissionrpc.torrent import Torrent
from core.transmissionrpc.session import Session
from six import PY3, integer_types, string_types, iteritems
if PY3:
from urllib.parse import urlparse
from urllib.request import urlopen
else:
from urlparse import urlparse
from urllib2 import urlopen
from six.moves.urllib_parse import urlparse
from six.moves.urllib_request import urlopen
def debug_httperror(error):

View file

@ -4,19 +4,15 @@
import sys
from core.transmissionrpc.error import HTTPHandlerError
from six import PY3
from six.moves.urllib_request import (
build_opener, install_opener,
HTTPBasicAuthHandler, HTTPDigestAuthHandler, HTTPPasswordMgrWithDefaultRealm,
Request,
)
from six.moves.urllib_error import HTTPError, URLError
from six.moves.http_client import BadStatusLine
if PY3:
from urllib.request import Request, build_opener, \
HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, HTTPDigestAuthHandler
from urllib.error import HTTPError, URLError
from http.client import BadStatusLine
else:
from urllib2 import Request, build_opener, \
HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, HTTPDigestAuthHandler
from urllib2 import HTTPError, URLError
from httplib import BadStatusLine
from core.transmissionrpc.error import HTTPHandlerError
class HTTPHandler(object):

View file

@ -1,12 +1,17 @@
# coding=utf8
import urllib
import urllib2
import urlparse
import cookielib
import json
import re
import StringIO
from six import StringIO
from six.moves.http_cookiejar import CookieJar
from six.moves.urllib_error import HTTPError
from six.moves.urllib_parse import urljoin, urlencode
from six.moves.urllib_request import (
build_opener, install_opener,
HTTPBasicAuthHandler, HTTPCookieProcessor,
Request,
)
from core.utorrent.upload import MultiPartForm
@ -23,23 +28,23 @@ class UTorrentClient(object):
def _make_opener(self, realm, base_url, username, password):
"""uTorrent API need HTTP Basic Auth and cookie support for token verify."""
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler = HTTPBasicAuthHandler()
auth_handler.add_password(realm=realm,
uri=base_url,
user=username,
passwd=password)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
opener = build_opener(auth_handler)
install_opener(opener)
cookie_jar = cookielib.CookieJar()
cookie_handler = urllib2.HTTPCookieProcessor(cookie_jar)
cookie_jar = CookieJar()
cookie_handler = HTTPCookieProcessor(cookie_jar)
handlers = [auth_handler, cookie_handler]
opener = urllib2.build_opener(*handlers)
opener = build_opener(*handlers)
return opener
def _get_token(self):
url = urlparse.urljoin(self.base_url, 'token.html')
url = urljoin(self.base_url, 'token.html')
response = self.opener.open(url)
token_re = "<div id='token' style='display:none;'>([^<>]+)</div>"
match = re.search(token_re, response.read())
@ -122,8 +127,8 @@ class UTorrentClient(object):
def _action(self, params, body=None, content_type=None):
# about token, see https://github.com/bittorrent/webui/wiki/TokenSystem
url = self.base_url + '?token=' + self.token + '&' + urllib.urlencode(params)
request = urllib2.Request(url)
url = self.base_url + '?token=' + self.token + '&' + urlencode(params)
request = Request(url)
if body:
request.add_data(body)
@ -134,5 +139,5 @@ class UTorrentClient(object):
try:
response = self.opener.open(request)
return response.code, json.loads(response.read())
except urllib2.HTTPError, e:
except HTTPError:
raise