From df95128fed50d6da62835dd040e45beb958ff7aa Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Mon, 2 Dec 2013 23:51:16 +0530 Subject: [PATCH] [minor] use mixin class for Timed_POP3 and Timed_POP3_SSL --- webnotes/utils/email_lib/receive.py | 34 ++++++++++------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/webnotes/utils/email_lib/receive.py b/webnotes/utils/email_lib/receive.py index 715b8839fe..cc941d7f8e 100644 --- a/webnotes/utils/email_lib/receive.py +++ b/webnotes/utils/email_lib/receive.py @@ -238,38 +238,28 @@ class POP3Mailbox: return error_msg -class Timed_POP3(poplib.POP3): +class TimerMixin(object): def __init__(self, *args, **kwargs): self.timeout = kwargs.pop('timeout', 0.0) self.elapsed_time = 0.0 - poplib.POP3.__init__(self, *args, **kwargs) - + self._super.__init__(self, *args, **kwargs) + def _getline(self, *args, **kwargs): start_time = time.time() - ret = poplib.POP3._getline(self, *args, **kwargs) + ret = self._super._getline(self, *args, **kwargs) + self.elapsed_time += time.time() - start_time if self.timeout and self.elapsed_time > self.timeout: raise EmailTimeoutError + return ret def quit(self, *args, **kwargs): self.elapsed_time = 0.0 - return poplib.POP3.quit(self, *args, **kwargs) + return self._super.quit(self, *args, **kwargs) -class Timed_POP3_SSL(poplib.POP3_SSL): - def __init__(self, *args, **kwargs): - self.timeout = kwargs.pop('timeout', 0.0) - self.elapsed_time = 0 - poplib.POP3_SSL.__init__(self, *args, **kwargs) - - def _getline(self, *args, **kwargs): - start_time = time.time() - ret = poplib.POP3_SSL._getline(self, *args, **kwargs) - self.elapsed_time += time.time() - start_time - if self.timeout and self.elapsed_time > self.timeout: - raise EmailTimeoutError - return ret - - def quit(self, *args, **kwargs): - self.elapsed_time = 0.0 - return poplib.POP3_SSL.quit(self, *args, **kwargs) +class Timed_POP3(TimerMixin, poplib.POP3): + _super = poplib.POP3 + +class Timed_POP3_SSL(TimerMixin, poplib.POP3_SSL): + _super = poplib.POP3_SSL