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

Use ABC for CookieManagerAPI #1254

Merged
merged 5 commits into from
Mar 12, 2024
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
3 changes: 3 additions & 0 deletions splinter/abc/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .cookie_manager import CookieManagerAPI

__all__ = ["CookieManagerAPI"]
25 changes: 20 additions & 5 deletions splinter/cookie_manager.py → splinter/abc/cookie_manager.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
# Copyright 2012 splinter authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
from abc import ABC
from abc import abstractmethod


class CookieManagerAPI:
"""An API that specifies how a splinter driver deals with cookies.
class CookieManagerAPI(ABC):
"""Specification for how a Splinter driver deals with cookies.

You can add cookies using the :meth:`add <CookieManagerAPI.add>` method,
and remove one or all cookies using
the :meth:`delete <CookieManagerAPI.delete>` method.
Add cookies using the :meth:`add <CookieManagerAPI.add>` method,
and remove cookies using
the :meth:`delete <CookieManagerAPI.delete>` and
:meth:`delete <CookieManagerAPI.delete_all>`methods.

A CookieManager acts like a ``dict``, so you can access the value of a
cookie through the [] operator, passing the cookie identifier:

Examples:

>>> cookie_manager.add({'name': 'Tony'})
>>> assert cookie_manager['name'] == 'Tony'
"""

def __init__(self, driver) -> None:
self.driver = driver

@abstractmethod
def add(self, cookie, **kwargs) -> None:
"""Add a cookie.

Expand All @@ -35,6 +41,7 @@ def add(self, cookie, **kwargs) -> None:
"""
raise NotImplementedError

@abstractmethod
def delete(self, *cookies: str) -> None:
"""Delete one or more cookies.

Expand All @@ -51,10 +58,12 @@ def delete(self, *cookies: str) -> None:
"""
raise NotImplementedError

@abstractmethod
def delete_all(self) -> None:
"""Delete all cookies."""
raise NotImplementedError

@abstractmethod
def all(self, verbose: bool = False): # NOQA: A003
"""Get all of the cookies.

Expand All @@ -74,8 +83,14 @@ def all(self, verbose: bool = False): # NOQA: A003
"""
raise NotImplementedError

@abstractmethod
def __contains__(self, key) -> bool:
raise NotImplementedError

@abstractmethod
def __getitem__(self, item):
raise NotImplementedError

@abstractmethod
def __eq__(self, other_object) -> bool:
raise NotImplementedError
2 changes: 1 addition & 1 deletion splinter/driver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import Optional
from typing import Type

from splinter.cookie_manager import CookieManagerAPI
from splinter.abc import CookieManagerAPI
from splinter.element_list import ElementList


Expand Down
2 changes: 1 addition & 1 deletion splinter/driver/djangoclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from urllib import parse

from .lxmldriver import LxmlDriver
from splinter.abc import CookieManagerAPI
from splinter.config import Config
from splinter.cookie_manager import CookieManagerAPI
from splinter.request_handler.status_code import StatusCode


Expand Down
2 changes: 1 addition & 1 deletion splinter/driver/flaskclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from urllib.parse import urlunparse

from .lxmldriver import LxmlDriver
from splinter.abc import CookieManagerAPI
from splinter.config import Config
from splinter.cookie_manager import CookieManagerAPI
from splinter.request_handler.status_code import StatusCode


Expand Down
2 changes: 1 addition & 1 deletion splinter/driver/webdriver/cookie_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# license that can be found in the LICENSE file.
from urllib.parse import urlparse

from splinter.cookie_manager import CookieManagerAPI
from splinter.abc import CookieManagerAPI


class CookieManager(CookieManagerAPI):
Expand Down
2 changes: 1 addition & 1 deletion splinter/driver/zopetestbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from zope.testbrowser.browser import Browser
from zope.testbrowser.browser import ListControl

from splinter.abc import CookieManagerAPI
from splinter.config import Config
from splinter.cookie_manager import CookieManagerAPI
from splinter.driver import DriverAPI
from splinter.driver import ElementAPI
from splinter.driver.element_present import ElementPresentMixIn
Expand Down
Loading