update functions working with cookies.txt files

- rename
  - load_cookiestxt -> cookiestxt_load
  - save_cookiestxt -< cookiestxt_store
- in cookiestxt_load, add cookies directly to a cookie jar
  instead of storing them in a list first
- other unnoticeable performance increases
pull/2584/head
Mike Fährmann 2 years ago
parent f190018e37
commit c4b9f7bab8
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -306,11 +306,10 @@ class Extractor():
cookiefile = util.expand_path(cookies)
try:
with open(cookiefile) as fp:
cookies = util.load_cookiestxt(fp)
util.cookiestxt_load(fp, self._cookiejar)
except Exception as exc:
self.log.warning("cookies: %s", exc)
else:
self._update_cookies(cookies)
self._cookiefile = cookiefile
else:
self.log.warning(
@ -322,7 +321,7 @@ class Extractor():
if self._cookiefile and self.config("cookies-update", True):
try:
with open(self._cookiefile, "w") as fp:
util.save_cookiestxt(fp, self._cookiejar)
util.cookiestxt_store(fp, self._cookiejar)
except OSError as exc:
self.log.warning("cookies: %s", exc)

@ -302,9 +302,9 @@ def set_mtime(path, mtime):
pass
def load_cookiestxt(fp):
"""Parse a Netscape cookies.txt file and return a list of its Cookies"""
cookies = []
def cookiestxt_load(fp, cookiejar):
"""Parse a Netscape cookies.txt file and add its Cookies to 'cookiejar'"""
set_cookie = cookiejar.set_cookie
for line in fp:
@ -321,11 +321,12 @@ def load_cookiestxt(fp):
domain, domain_specified, path, secure, expires, name, value = \
line.split("\t")
if not name:
name = value
value = None
cookies.append(Cookie(
set_cookie(Cookie(
0, name, value,
None, False,
domain,
@ -337,12 +338,11 @@ def load_cookiestxt(fp):
False, None, None, {},
))
return cookies
def save_cookiestxt(fp, cookies):
def cookiestxt_store(fp, cookies):
"""Write 'cookies' in Netscape cookies.txt format to 'fp'"""
fp.write("# Netscape HTTP Cookie File\n\n")
write = fp.write
write("# Netscape HTTP Cookie File\n\n")
for cookie in cookies:
if not cookie.domain:
@ -355,15 +355,15 @@ def save_cookiestxt(fp, cookies):
name = cookie.name
value = cookie.value
fp.write("\t".join((
write("\t".join((
cookie.domain,
"TRUE" if cookie.domain.startswith(".") else "FALSE",
cookie.path,
"TRUE" if cookie.secure else "FALSE",
"0" if cookie.expires is None else str(cookie.expires),
name,
value,
)) + "\n")
value + "\n",
)))
def code_to_language(code, default=None):

@ -168,11 +168,12 @@ class TestISO639_1(unittest.TestCase):
class TestCookiesTxt(unittest.TestCase):
def test_load_cookiestxt(self):
def test_cookiestxt_load(self):
def _assert(content, expected):
cookies = util.load_cookiestxt(io.StringIO(content, None))
for c, e in zip(cookies, expected):
jar = http.cookiejar.CookieJar()
util.cookiestxt_load(io.StringIO(content, None), jar)
for c, e in zip(jar, expected):
self.assertEqual(c.__dict__, e.__dict__)
_assert("", [])
@ -218,13 +219,14 @@ class TestCookiesTxt(unittest.TestCase):
)
with self.assertRaises(ValueError):
util.load_cookiestxt("example.org\tTRUE\t/\tTRUE\t0\tname")
util.cookiestxt_load("example.org\tTRUE\t/\tTRUE\t0\tname",
http.cookiejar.CookieJar())
def test_save_cookiestxt(self):
def test_cookiestxt_store(self):
def _assert(cookies, expected):
fp = io.StringIO(newline=None)
util.save_cookiestxt(fp, cookies)
util.cookiestxt_store(fp, cookies)
self.assertMultiLineEqual(fp.getvalue(), expected)
_assert([], "# Netscape HTTP Cookie File\n\n")

Loading…
Cancel
Save