Package concurrent_tree_crawler :: Package common :: Module activity_schedule
[hide private]
[frames] | no frames]

Source Code for Module concurrent_tree_crawler.common.activity_schedule

 1  import datetime 
2 3 -class AbstractActivitySchedule:
4 """ 5 Activity and inactivity time schedule. 6 7 We Assume that the time axis consists of two types of periods: activity and 8 inactivity. Each time point belongs either to activity or inactivity 9 period. 10 """ 11
12 - def get_activity_info(self, now=None):
13 """ 14 @param now: current time, by default, it is the value of 15 L{datetime.datetime.now()} 16 @type now: L{datetime.datetime} 17 @return: activity information relative to C{now} 18 @rtype: L{ActivityInfo} 19 """ 20 raise NotImplementedError()
21
22 -class ActivityInfo:
23 - def __init__(self, is_in_activity_period, time_to_mode_change):
24 self.is_in_activity_period = is_in_activity_period 25 """C{True} iff we are in activity period""" 26 27 self.future_mode_change = time_to_mode_change 28 """Time (L{date.datetime}) of the closest future mode change 29 (i.e. change from the activity to inactivity period or 30 from inactivity to activity period). It can have C{None} value when 31 the mode will not change in the future."""
32
33 -class AlwaysActiveSchedule(AbstractActivitySchedule):
34 - def get_activity_info(self, now=None):
35 return ActivityInfo(True, None)
36
37 -class DaySchedule(AbstractActivitySchedule):
38
39 - def __init__(self, activity_start_time, activity_end_time):
40 """ 41 @type activity_start_time: L{datetime.time} 42 @type activity_end_time: L{datetime.time} 43 """ 44 self.__start = activity_start_time 45 self.__end = activity_end_time
46
47 - def __get_closest_event_interval(self, now):
48 """ 49 Return current event interval (if C{now} lies in event interval) or 50 future closest event interval (if C{now} doesn't lie in event interval) 51 52 @param now: current time (L{datetime.datetime}) 53 @return: (start, end), where both values are of type 54 L{date.datetime}. 55 """ 56 ## Legend to the comments drawings: 57 ## **** : activity time 58 ## ---- : inactivity time 59 ## |0| : 0th hour 60 ## |23| : 23rd hour 61 ## |s| : schedule.__start 62 ## |e| : schedule.__end 63 today = now 64 tomorrow = today+datetime.timedelta(1) 65 yesterday = today-datetime.timedelta(1) 66 ## |0|----|s|****|e|----|23| 67 if self.__start < self.__end : 68 if ((today.time() < self.__start) or 69 (today.time() > self.__start and today.time() < self.__end)): 70 return (self.__get_datetime(today, self.__start), 71 self.__get_datetime(today, self.__end)) 72 else: 73 return (self.__get_datetime(tomorrow, self.__start), 74 self.__get_datetime(tomorrow, self.__end)) 75 ## |0|****|e|----|s|****|23| 76 else: 77 if ((today.time() > self.__end and today.time() < self.__start) or 78 today.time() > self.__start): 79 return (self.__get_datetime(today, self.__start), 80 self.__get_datetime(tomorrow, self.__end)) 81 else: 82 return (self.__get_datetime(yesterday, self.__start), 83 self.__get_datetime(today, self.__end))
84
85 - def get_activity_info(self, now=None):
86 if now is None: 87 now = datetime.datetime.now() 88 (start, end) = self.__get_closest_event_interval(now) 89 if now < start: 90 return ActivityInfo(False, start) 91 else: 92 return ActivityInfo(True, end)
93 94 @staticmethod
95 - def __get_datetime(date, time_):
96 return datetime.datetime(date.year, date.month, date.day, 97 time_.hour, time_.minute, time_.second, time_.microsecond)
98