|
| 1 | +from .property_decorators import property_is_valid_time, property_not_nullable |
| 2 | + |
| 3 | + |
| 4 | +class IntervalItem(object): |
| 5 | + class Frequency: |
| 6 | + Hourly = "Hourly" |
| 7 | + Daily = "Daily" |
| 8 | + Weekly = "Weekly" |
| 9 | + Monthly = "Monthly" |
| 10 | + |
| 11 | + class Occurrence: |
| 12 | + Minutes = "minutes" |
| 13 | + Hours = "hours" |
| 14 | + WeekDay = "weekDay" |
| 15 | + MonthDay = "monthDay" |
| 16 | + |
| 17 | + class Day: |
| 18 | + Sunday = "Sunday" |
| 19 | + Monday = "Monday" |
| 20 | + Tuesday = "Tuesday" |
| 21 | + Wednesday = "Wednesday" |
| 22 | + Thursday = "Thursday" |
| 23 | + Friday = "Friday" |
| 24 | + Saturday = "Saturday" |
| 25 | + LastDay = "LastDay" |
| 26 | + |
| 27 | + |
| 28 | +class HourlyInterval(object): |
| 29 | + def __init__(self, start_time, end_time, interval_value): |
| 30 | + |
| 31 | + self.start_time = start_time |
| 32 | + self.end_time = end_time |
| 33 | + self.interval = interval_value |
| 34 | + |
| 35 | + @property |
| 36 | + def _frequency(self): |
| 37 | + return IntervalItem.Frequency.Hourly |
| 38 | + |
| 39 | + @property |
| 40 | + def start_time(self): |
| 41 | + return self._start_time |
| 42 | + |
| 43 | + @start_time.setter |
| 44 | + @property_is_valid_time |
| 45 | + @property_not_nullable |
| 46 | + def start_time(self, value): |
| 47 | + self._start_time = value |
| 48 | + |
| 49 | + @property |
| 50 | + def end_time(self): |
| 51 | + return self._end_time |
| 52 | + |
| 53 | + @end_time.setter |
| 54 | + @property_is_valid_time |
| 55 | + @property_not_nullable |
| 56 | + def end_time(self, value): |
| 57 | + self._end_time = value |
| 58 | + |
| 59 | + @property |
| 60 | + def interval(self): |
| 61 | + return self._interval |
| 62 | + |
| 63 | + @interval.setter |
| 64 | + def interval(self, interval): |
| 65 | + VALID_INTERVALS = {.25, .5, 1, 2, 4, 6, 8, 12} |
| 66 | + if float(interval) not in VALID_INTERVALS: |
| 67 | + error = "Invalid interval {} not in {}".format(interval, str(VALID_INTERVALS)) |
| 68 | + raise ValueError(error) |
| 69 | + |
| 70 | + self._interval = interval |
| 71 | + |
| 72 | + def _interval_type_pairs(self): |
| 73 | + |
| 74 | + # We use fractional hours for the two minute-based intervals. |
| 75 | + # Need to convert to minutes from hours here |
| 76 | + if self.interval in {.25, .5}: |
| 77 | + calculated_interval = int(self.interval * 60) |
| 78 | + interval_type = IntervalItem.Occurrence.Minutes |
| 79 | + else: |
| 80 | + calculated_interval = self.interval |
| 81 | + interval_type = IntervalItem.Occurrence.Hours |
| 82 | + |
| 83 | + return [(interval_type, str(calculated_interval))] |
| 84 | + |
| 85 | + |
| 86 | +class DailyInterval(object): |
| 87 | + def __init__(self, start_time): |
| 88 | + self.start_time = start_time |
| 89 | + |
| 90 | + @property |
| 91 | + def _frequency(self): |
| 92 | + return IntervalItem.Frequency.Daily |
| 93 | + |
| 94 | + @property |
| 95 | + def start_time(self): |
| 96 | + return self._start_time |
| 97 | + |
| 98 | + @start_time.setter |
| 99 | + @property_is_valid_time |
| 100 | + @property_not_nullable |
| 101 | + def start_time(self, value): |
| 102 | + self._start_time = value |
| 103 | + |
| 104 | + |
| 105 | +class WeeklyInterval(object): |
| 106 | + def __init__(self, start_time, *interval_values): |
| 107 | + self.start_time = start_time |
| 108 | + self.interval = interval_values |
| 109 | + |
| 110 | + @property |
| 111 | + def _frequency(self): |
| 112 | + return IntervalItem.Frequency.Weekly |
| 113 | + |
| 114 | + @property |
| 115 | + def start_time(self): |
| 116 | + return self._start_time |
| 117 | + |
| 118 | + @start_time.setter |
| 119 | + @property_is_valid_time |
| 120 | + @property_not_nullable |
| 121 | + def start_time(self, value): |
| 122 | + self._start_time = value |
| 123 | + |
| 124 | + @property |
| 125 | + def interval(self): |
| 126 | + return self._interval |
| 127 | + |
| 128 | + @interval.setter |
| 129 | + def interval(self, interval_values): |
| 130 | + if not all(hasattr(IntervalItem.Day, day) for day in interval_values): |
| 131 | + raise ValueError("Invalid week day defined " + str(interval_values)) |
| 132 | + |
| 133 | + self._interval = interval_values |
| 134 | + |
| 135 | + def _interval_type_pairs(self): |
| 136 | + return [(IntervalItem.Occurrence.WeekDay, day) for day in self.interval] |
| 137 | + |
| 138 | + |
| 139 | +class MonthlyInterval(object): |
| 140 | + def __init__(self, start_time, interval_value): |
| 141 | + self.start_time = start_time |
| 142 | + self.interval = str(interval_value) |
| 143 | + |
| 144 | + @property |
| 145 | + def _frequency(self): |
| 146 | + return IntervalItem.Frequency.Monthly |
| 147 | + |
| 148 | + @property |
| 149 | + def start_time(self): |
| 150 | + return self._start_time |
| 151 | + |
| 152 | + @start_time.setter |
| 153 | + @property_is_valid_time |
| 154 | + @property_not_nullable |
| 155 | + def start_time(self, value): |
| 156 | + self._start_time = value |
| 157 | + |
| 158 | + @property |
| 159 | + def interval(self): |
| 160 | + return self._interval |
| 161 | + |
| 162 | + @interval.setter |
| 163 | + def interval(self, interval_value): |
| 164 | + error = "Invalid interval value for a monthly frequency: {}.".format(interval_value) |
| 165 | + |
| 166 | + # This is weird because the value could be a str or an int |
| 167 | + # The only valid str is 'LastDay' so we check that first. If that's not it |
| 168 | + # try to convert it to an int, if that fails because it's an incorrect string |
| 169 | + # like 'badstring' we catch and re-raise. Otherwise we convert to int and check |
| 170 | + # that it's in range 1-31 |
| 171 | + |
| 172 | + if interval_value != "LastDay": |
| 173 | + try: |
| 174 | + if not (1 <= int(interval_value) <= 31): |
| 175 | + raise ValueError(error) |
| 176 | + except ValueError as e: |
| 177 | + if interval_value != "LastDay": |
| 178 | + raise ValueError(error) |
| 179 | + |
| 180 | + self._interval = str(interval_value) |
| 181 | + |
| 182 | + def _interval_type_pairs(self): |
| 183 | + return [(IntervalItem.Occurrence.MonthDay, self.interval)] |
0 commit comments