From 9023ea8947f228ec2ef1e72f254930ff8a4d8145 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Tue, 26 Mar 2019 13:00:31 -0400 Subject: [PATCH] python3: add Cookie wrapper to polyglot --- src/calibre/srv/tests/auth.py | 5 +++-- src/calibre/srv/utils.py | 2 +- src/calibre/utils/browser.py | 2 +- src/polyglot/http_cookie.py | 12 ++++++++++++ 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 src/polyglot/http_cookie.py diff --git a/src/calibre/srv/tests/auth.py b/src/calibre/srv/tests/auth.py index 9407516cf0..45764253e6 100644 --- a/src/calibre/srv/tests/auth.py +++ b/src/calibre/srv/tests/auth.py @@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import, __license__ = 'GPL v3' __copyright__ = '2015, Kovid Goyal ' -import base64, subprocess, os, cookielib, time +import base64, subprocess, os, time from collections import namedtuple try: from distutils.spawn import find_executable @@ -19,6 +19,7 @@ from calibre.srv.tests.base import BaseTest, TestServer from calibre.srv.routes import endpoint, Router from polyglot.builtins import iteritems, itervalues from polyglot import http_client +from polyglot.http_cookie import CookieJar from polyglot.urllib import (build_opener, HTTPBasicAuthHandler, HTTPCookieProcessor, HTTPDigestAuthHandler, HTTPError) @@ -283,7 +284,7 @@ class TestAuth(BaseTest): auth_handler = HTTPDigestAuthHandler() url = 'http://localhost:%d%s' % (server.address[1], '/android') auth_handler.add_password(realm=REALM, uri=url, user='testuser', passwd='testpw') - cj = cookielib.CookieJar() + cj = CookieJar() cookie_handler = HTTPCookieProcessor(cj) r = build_opener(auth_handler, cookie_handler).open(url) self.ae(r.getcode(), http_client.OK) diff --git a/src/calibre/srv/utils.py b/src/calibre/srv/utils.py index 0037e4e8a7..22f9d2b777 100644 --- a/src/calibre/srv/utils.py +++ b/src/calibre/srv/utils.py @@ -7,7 +7,6 @@ __license__ = 'GPL v3' __copyright__ = '2015, Kovid Goyal ' import errno, socket, select, os, time -from Cookie import SimpleCookie from contextlib import closing from email.utils import formatdate from operator import itemgetter @@ -23,6 +22,7 @@ from calibre.utils.logging import ThreadSafeLog from calibre.utils.shared_file import share_open, raise_winerror from polyglot.builtins import iteritems, map, unicode_type, range from polyglot import reprlib +from polyglot.http_cookie import SimpleCookie from polyglot.urllib import parse_qs, quote as urlquote HTTP1 = 'HTTP/1.0' diff --git a/src/calibre/utils/browser.py b/src/calibre/utils/browser.py index 47835dec1e..1228747ee8 100644 --- a/src/calibre/utils/browser.py +++ b/src/calibre/utils/browser.py @@ -6,11 +6,11 @@ __copyright__ = '2010, Kovid Goyal ' __docformat__ = 'restructuredtext en' import copy, ssl -from cookielib import CookieJar, Cookie from mechanize import Browser as B, HTTPSHandler from polyglot import http_client +from polyglot.http_cookie import CookieJar, Cookie class ModernHTTPSHandler(HTTPSHandler): diff --git a/src/polyglot/http_cookie.py b/src/polyglot/http_cookie.py new file mode 100644 index 0000000000..645b8b6a3a --- /dev/null +++ b/src/polyglot/http_cookie.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python2 +# vim:fileencoding=utf-8 +# License: GPL v3 Copyright: 2019, Eli Schwartz + +from polyglot.builtins import is_py3 + +if is_py3: + from http.cookies import SimpleCookie # noqa + from http.cookiejar import CookieJar, Cookie # noqa +else: + from Cookie import SimpleCookie # noqa + from cookielib import CookieJar, Cookie # noqa