Skip to content

Commit

Permalink
Refs. #30. Create a base engine class ``pyzor.engines.common.BaseEngi…
Browse files Browse the repository at this point in the history
…ne``.

Add a base class for engines with stubs for all methods that require
implementation for a pyzord server engine.

The ``whitelist`` and ``report`` methods are exceptions to this, as
noted in the documentation.
  • Loading branch information
alexkiro committed Oct 28, 2014
1 parent 0e3271e commit 55044ea
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
58 changes: 57 additions & 1 deletion pyzor/engines/common.py
Expand Up @@ -5,7 +5,7 @@

from collections import namedtuple

__all__ = ["DBHandle", "DatabaseError", "Record"]
__all__ = ["DBHandle", "DatabaseError", "Record", "BaseEngine"]

DBHandle = namedtuple("DBHandle", ["single_threaded", "multi_threaded",
"multi_processing", "prefork"])
Expand Down Expand Up @@ -51,3 +51,59 @@ def r_update(self):
def wl_update(self):
self.wl_updated = datetime.datetime.now()


class BaseEngine(object):
"""Base class for Pyzor engines."""
absolute_source = True
handles_one_step = False

def __iter__(self):
"""Iterate over all keys"""
raise NotImplementedError()

def iteritems(self):
"""Iterate over pairs of (key, record)."""
raise NotImplementedError()

def items(self):
"""Return a list of (key, record)."""
raise NotImplementedError()

def __getitem__(self, key):
"""Get the record for this corresponding key."""
raise NotImplementedError()

def __setitem__(self, key, value):
"""Set the record for this corresponding key. 'value' should be a
instance of the ``Record`` class.
"""
raise NotImplementedError()

def __delitem__(self, key):
"""Remove the corresponding record from the database."""
raise NotImplementedError()

def report(self, keys):
"""Report the corresponding key as spam, incrementing the report count.
Engines that implement don't implement this method should have
handles_one_step set to False.
"""
raise NotImplementedError()

def whitelist(self, keys):
"""Report the corresponding key as ham, incrementing the whitelist
count.
Engines that implement don't implement this method should have
handles_one_step set to False.
"""

raise NotImplementedError()

@classmethod
def get_prefork_connections(cls, fn, mode, max_age=None):
"""Yields an unlimited number of partial functions that return a new
engine instance, suitable for using toghether with the Pre-Fork server.
"""
raise NotImplementedError()
4 changes: 2 additions & 2 deletions pyzor/engines/gdbm_.py
Expand Up @@ -15,7 +15,7 @@
import datetime
import threading

from pyzor.engines.common import Record, DBHandle
from pyzor.engines.common import Record, DBHandle, BaseEngine


def _dt_decode(datetime_str):
Expand All @@ -28,7 +28,7 @@ def _dt_decode(datetime_str):
return datetime.datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S")


class GdbmDBHandle(object):
class GdbmDBHandle(BaseEngine):
absolute_source = True
handles_one_step = False

Expand Down
2 changes: 1 addition & 1 deletion pyzor/engines/mysql.py
Expand Up @@ -22,7 +22,7 @@
from pyzor.engines.common import *


class MySQLDBHandle(object):
class MySQLDBHandle(BaseEngine):
absolute_source = False
handles_one_step = True
# The table must already exist, and have this schema:
Expand Down
2 changes: 1 addition & 1 deletion pyzor/engines/redis_.py
Expand Up @@ -37,7 +37,7 @@ def wrapped_f(self, *args, **kwargs):
return wrapped_f


class RedisDBHandle(object):
class RedisDBHandle(BaseEngine):
absolute_source = False
handles_one_step = False

Expand Down

0 comments on commit 55044ea

Please sign in to comment.