"Fossies" - the Fresh Open Source Software Archive

Member "pyzor-1.0.0/pyzor/engines/common.py" (10 Dec 2014, 3188 Bytes) of package /linux/privat/pyzor-1.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "common.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.9.0_vs_1.0.0.

    1 """Common library shared by different engines."""
    2 
    3 import sys
    4 import datetime
    5 
    6 from collections import namedtuple
    7 
    8 __all__ = ["DBHandle", "DatabaseError", "Record", "BaseEngine"]
    9 
   10 DBHandle = namedtuple("DBHandle", ["single_threaded", "multi_threaded",
   11                                    "multi_processing", "prefork"])
   12 
   13 
   14 class DatabaseError(Exception):
   15     pass
   16 
   17 
   18 class Record(object):
   19     """Prefix conventions used in this class:
   20     r = report (spam)
   21     wl = whitelist
   22     """
   23     def __init__(self, r_count=0, wl_count=0, r_entered=None,
   24                  r_updated=None, wl_entered=None, wl_updated=None):
   25         self.r_count = r_count
   26         self.wl_count = wl_count
   27         self.r_entered = r_entered
   28         self.r_updated = r_updated
   29         self.wl_entered = wl_entered
   30         self.wl_updated = wl_updated
   31 
   32     def wl_increment(self):
   33         # overflow prevention
   34         if self.wl_count < sys.maxint:
   35             self.wl_count += 1
   36         if self.wl_entered is None:
   37             self.wl_entered = datetime.datetime.now()
   38         self.wl_update()
   39 
   40     def r_increment(self):
   41         # overflow prevention
   42         if self.r_count < sys.maxint:
   43             self.r_count += 1
   44         if self.r_entered is None:
   45             self.r_entered = datetime.datetime.now()
   46         self.r_update()
   47 
   48     def r_update(self):
   49         self.r_updated = datetime.datetime.now()
   50 
   51     def wl_update(self):
   52         self.wl_updated = datetime.datetime.now()
   53 
   54 
   55 class BaseEngine(object):
   56     """Base class for Pyzor engines."""
   57     absolute_source = True
   58     handles_one_step = False
   59 
   60     def __iter__(self):
   61         """Iterate over all keys"""
   62         raise NotImplementedError()
   63 
   64     def iteritems(self):
   65         """Iterate over pairs of (key, record)."""
   66         raise NotImplementedError()
   67 
   68     def items(self):
   69         """Return a list of (key, record)."""
   70         raise NotImplementedError()
   71 
   72     def __getitem__(self, key):
   73         """Get the record for this corresponding key."""
   74         raise NotImplementedError()
   75 
   76     def __setitem__(self, key, value):
   77         """Set the record for this corresponding key. 'value' should be a
   78         instance of the ``Record`` class.
   79         """
   80         raise NotImplementedError()
   81 
   82     def __delitem__(self, key):
   83         """Remove the corresponding record from the database."""
   84         raise NotImplementedError()
   85 
   86     def report(self, keys):
   87         """Report the corresponding key as spam, incrementing the report count.
   88 
   89         Engines that implement don't implement this method should have
   90         handles_one_step set to False.
   91         """
   92         raise NotImplementedError()
   93 
   94     def whitelist(self, keys):
   95         """Report the corresponding key as ham, incrementing the whitelist
   96         count.
   97 
   98         Engines that implement don't implement this method should have
   99         handles_one_step set to False.
  100         """
  101 
  102         raise NotImplementedError()
  103 
  104     @classmethod
  105     def get_prefork_connections(cls, fn, mode, max_age=None):
  106         """Yields an unlimited number of partial functions that return a new
  107         engine instance, suitable for using toghether with the Pre-Fork server.
  108         """
  109         raise NotImplementedError()