Skip to content

Commit

Permalink
Use ABC for CookieManagerAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
jsfehler committed Feb 27, 2024
1 parent 5889c16 commit 89d6aa3
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
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"]
24 changes: 19 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,30 @@
# 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, 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 +40,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 +57,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 +82,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/djangoclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .lxmldriver import LxmlDriver
from splinter.config import Config
from splinter.cookie_manager import CookieManagerAPI
from splinter.abc 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 @@ -9,7 +9,7 @@

from .lxmldriver import LxmlDriver
from splinter.config import Config
from splinter.cookie_manager import CookieManagerAPI
from splinter.abc 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 @@ -13,7 +13,7 @@
from zope.testbrowser.browser import ListControl

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

0 comments on commit 89d6aa3

Please sign in to comment.