Commits

Jason R. Coombs committed aafe871

Fleshed out stubbed implementation in locking module

Comments (0)

Files changed (1)

cherrypy/lib/locking.py

+import datetime
 
-class Timer:
-    "stubbed"
-class NeverExpires:
-    "stubbed"
-class LockTimeout:
-    "stubbed"
+
+class NeverExpires(object):
+    def expired(self):
+        return False
+
+
+class Timer(object):
+    """
+    A simple timer that will indicate when an expiration time has passed.
+    """
+    def __init__(self, expiration):
+        "Create a timer that expires at `expiration` (UTC datetime)"
+        self.expiration = expiration
+
+    @classmethod
+    def after(cls, elapsed):
+        """
+        Return a timer that will expire after `elapsed` passes.
+        """
+        return cls(datetime.datetime.utcnow() + elapsed)
+
+    def expired(self):
+        return datetime.datetime.utcnow() >= self.expiration
+
+
+class LockTimeout(Exception):
+    "An exception when a lock could not be acquired before a timeout period"
+
 
 class LockChecker(object):
     """