mirror of https://github.com/fail2ban/fail2ban
DOC: adjust docs in mytime to place docs into docstrings
parent
caa6006a31
commit
e1a5decc00
|
@ -26,46 +26,48 @@ import time, datetime
|
||||||
##
|
##
|
||||||
# MyTime class.
|
# MyTime class.
|
||||||
#
|
#
|
||||||
# This class is a wrapper around time.time() and time.gmtime(). When
|
|
||||||
# performing unit test, it is very useful to get a fixed value from these
|
|
||||||
# functions.
|
|
||||||
# Thus, time.time() and time.gmtime() should never be called directly.
|
|
||||||
# This wrapper should be called instead. The API are equivalent.
|
|
||||||
|
|
||||||
class MyTime:
|
class MyTime:
|
||||||
|
"""A wrapper around time module primarily for testing purposes
|
||||||
|
|
||||||
|
This class is a wrapper around time.time() and time.gmtime(). When
|
||||||
|
performing unit test, it is very useful to get a fixed value from
|
||||||
|
these functions. Thus, time.time() and time.gmtime() should never
|
||||||
|
be called directly. This wrapper should be called instead. The API
|
||||||
|
are equivalent.
|
||||||
|
"""
|
||||||
|
|
||||||
myTime = None
|
myTime = None
|
||||||
|
|
||||||
##
|
|
||||||
# Sets the current time.
|
|
||||||
#
|
|
||||||
# Use None in order to always get the real current time.
|
|
||||||
#
|
|
||||||
# @param t the time to set or None
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def setTime(t):
|
def setTime(t):
|
||||||
|
"""Set current time.
|
||||||
|
|
||||||
|
Use None in order to always get the real current time.
|
||||||
|
|
||||||
|
@param t the time to set or None
|
||||||
|
"""
|
||||||
|
|
||||||
MyTime.myTime = t
|
MyTime.myTime = t
|
||||||
|
|
||||||
##
|
|
||||||
# Equivalent to time.time()
|
|
||||||
#
|
|
||||||
# @return time.time() if setTime was called with None
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def time():
|
def time():
|
||||||
|
"""Decorate time.time() for the purpose of testing mocking
|
||||||
|
|
||||||
|
@return time.time() if setTime was called with None
|
||||||
|
"""
|
||||||
|
|
||||||
if MyTime.myTime is None:
|
if MyTime.myTime is None:
|
||||||
return time.time()
|
return time.time()
|
||||||
else:
|
else:
|
||||||
return MyTime.myTime
|
return MyTime.myTime
|
||||||
|
|
||||||
##
|
|
||||||
# Equivalent to time.gmtime()
|
|
||||||
#
|
|
||||||
# @return time.gmtime() if setTime was called with None
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def gmtime():
|
def gmtime():
|
||||||
|
"""Decorate time.gmtime() for the purpose of testing mocking
|
||||||
|
|
||||||
|
@return time.gmtime() if setTime was called with None
|
||||||
|
"""
|
||||||
if MyTime.myTime is None:
|
if MyTime.myTime is None:
|
||||||
return time.gmtime()
|
return time.gmtime()
|
||||||
else:
|
else:
|
||||||
|
@ -73,6 +75,10 @@ class MyTime:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def now():
|
def now():
|
||||||
|
"""Decorate datetime.now() for the purpose of testing mocking
|
||||||
|
|
||||||
|
@return datetime.now() if setTime was called with None
|
||||||
|
"""
|
||||||
if MyTime.myTime is None:
|
if MyTime.myTime is None:
|
||||||
return datetime.datetime.now()
|
return datetime.datetime.now()
|
||||||
else:
|
else:
|
||||||
|
@ -80,6 +86,10 @@ class MyTime:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def localtime(x=None):
|
def localtime(x=None):
|
||||||
|
"""Decorate time.localtime() for the purpose of testing mocking
|
||||||
|
|
||||||
|
@return time.localtime() if setTime was called with None
|
||||||
|
"""
|
||||||
if MyTime.myTime is None or x is not None:
|
if MyTime.myTime is None or x is not None:
|
||||||
return time.localtime(x)
|
return time.localtime(x)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue