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