Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved #1219 - Support load and save for CookieJar #1326

Merged
merged 5 commits into from
Oct 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ CHANGES

- Show more verbose message on import errors #1319

-
- Added save and load functionality for `CookieJar` #1219

-

Expand Down
12 changes: 12 additions & 0 deletions aiohttp/cookiejar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import datetime
import pathlib
import pickle
import re
from collections import defaultdict
from collections.abc import Mapping
Expand Down Expand Up @@ -37,6 +39,16 @@ def __init__(self, *, unsafe=False, loop=None):
self._next_expiration = ceil(self._loop.time())
self._expirations = {}

def save(self, file_path):
file_path = pathlib.Path(file_path)
with file_path.open(mode='wb') as f:
pickle.dump(self._cookies, f, pickle.HIGHEST_PROTOCOL)

def load(self, file_path):
file_path = pathlib.Path(file_path)
with file_path.open(mode='rb') as f:
self._cookies = pickle.load(f)

def clear(self):
self._cookies.clear()
self._host_only_cookies.clear()
Expand Down
14 changes: 14 additions & 0 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1538,5 +1538,19 @@ CookieJar
:return: :class:`http.cookies.SimpleCookie` with filtered
cookies for given URL.

.. method:: save(file_path)

Write a pickled representation of cookies into the file
at provided path.

:param str file_path: Path to file where cookies will be serialized.

.. method:: load(file_path)

Load a pickled representation of cookies from the file
at provided path.

:param str file_path: Path to file from where cookies will be imported.

.. disqus::
:title: aiohttp client reference
21 changes: 21 additions & 0 deletions tests/test_cookiejar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio
import datetime
import os
import tempfile
import unittest
from http.cookies import SimpleCookie
from unittest import mock
Expand Down Expand Up @@ -141,6 +143,25 @@ def test_constructor(loop, cookies_to_send, cookies_to_receive):
assert jar._loop is loop


def test_save_load(loop, cookies_to_send, cookies_to_receive):
file_path = tempfile.mkdtemp() + '/aiohttp.test.cookie'

# export cookie jar
jar_save = CookieJar(loop=loop)
jar_save.update_cookies(cookies_to_receive)
jar_save.save(file_path=file_path)

jar_load = CookieJar(loop=loop)
jar_load.load(file_path=file_path)

jar_test = SimpleCookie()
for cookie in jar_load:
jar_test[cookie.key] = cookie

os.unlink(file_path)
assert jar_test == cookies_to_receive


def test_ctor_ith_default_loop(loop):
asyncio.set_event_loop(loop)
jar = CookieJar()
Expand Down