Created
August 9, 2012 00:56
-
-
Save daira/3300082 to your computer and use it in GitHub Desktop.
ExpirationPolicy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExpirationPolicy(object): | |
def __init__(self, enabled=False, mode="age", override_lease_duration=None, | |
cutoff_date=None, sharetypes=("mutable", "immutable")): | |
precondition(isinstance(enabled, bool), enabled=enabled) | |
precondition(mode in ("age", "cutoff-date"), mode=mode) | |
precondition(isinstance(override_lease_duration, (int, NoneType)), | |
override_lease_duration=override_lease_duration) | |
precondition(isinstance(cutoff_date, int) or (mode != "cutoff-date" and cutoff_date is None), | |
cutoff_date=cutoff_date) | |
precondition(isinstance(sharetypes, tuple), sharetypes=sharetypes) | |
self._mode = mode | |
self._override_lease_duration = override_lease_duration | |
self._cutoff_date = cutoff_date | |
if enabled: | |
self._sharetypes_to_expire = sharetypes | |
else: | |
self._sharetypes_to_expire = () | |
def should_expire(self, current_time, renewal_time, expiration_time, sharetype): | |
if sharetype not in self._sharetypes_to_expire: | |
return False | |
if self._mode == "age": | |
if self._override_lease_duration is None: | |
expiry_time = expiration_time # from lease | |
else: | |
expiry_time = renewal_time + self._override_lease_duration | |
else: | |
expiry_time = self._cutoff_date | |
return current_time >= expiry_time | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment