1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """
17 Contains maintenance window classes.
18 """
19
20 from gofer.messaging import *
21 from datetime import datetime as dt
22 from datetime import timedelta as delta
23
24
25
27 """
28 Represents a maintenance (time) window.
29 An empty L{Window} defines an unbounded window.
30 A I{begin} of 'None' = UTC now.
31 An I{end} of 'None' = begin plus 1 hour.
32 @cvar FORMAT: The datetime format. ISO 8601
33 @type FORMAT: str
34 @cvar DURATION: The duration keywords.
35 @type DURATION: [str,..]
36 """
37
38 FORMAT = '%Y-%m-%dT%H:%M:%S'
39 DURATION = ('days', 'seconds', 'minutes', 'hours', 'weeks')
40
42 """
43 @param D: A (optional) dictionary.
44 @type D: [dict,..]
45 @note: An empty I{window} indicates an unbounded window.
46 @keyword window: The window specification:
47 - begin
48 One of:
49 - end
50 - days
51 - seconds
52 - minutes
53 - hours
54 - weeks
55 """
56 if D:
57 Options.__init__(self, *D)
58 return
59 if window:
60 self.__setbegin(window)
61 self.__setend(window)
62 Options.__init__(self, **window)
63
65 """
66 Get whether the current datetime (UTC) falls
67 within the window.
68 @note: Empty = match ALL.
69 @return: True when matched.
70 @rtype: bool
71 """
72 if self:
73 now = dt.utcnow()
74 begin, end = self.__dates(now)
75 return ( now >= begin and now <= end )
76 else:
77 return True
78
80 """
81 Get whether window is in the future.
82 @note: Empty = match ALL.
83 @return: True if I{begin} > I{utcnow()}.
84 @rtype: bool
85 """
86 if self:
87 now = dt.utcnow()
88 begin, end = self.__dates(now)
89 return ( now < begin )
90 else:
91 return False
92
94 """
95 Get whether window is in the past.
96 @note: Empty = match ALL.
97 @return: True if I{utcnow()} > I{end}.
98 @rtype: bool
99 """
100 if self:
101 now = dt.utcnow()
102 begin, end = self.__dates(now)
103 return ( now > end )
104 else:
105 return False
106
108 """
109 Set the proper window beginning.
110 Performs:
111 - Convert to string if L{dt} object.
112 - Default to UTC (now) when value is (None).
113 @param window: The window specification.
114 @type window: dict
115 @return: The updated I{window}.
116 @rtype: dict
117 """
118 BEGIN = 'begin'
119 if BEGIN in window:
120 v = window[BEGIN]
121 if not v:
122 v = dt.utcnow()
123 if isinstance(v, dt):
124 v = v.strftime(self.FORMAT)
125 window[BEGIN] = v
126 else:
127 raise Exception, 'Window() must specify "begin"'
128 return window
129
131 """
132 Set the proper window ending.
133 Performs:
134 - Convert to string if L{dt} object.
135 - Default begin plus 1 hour when value is (None).
136 @param window: The window specification.
137 @type window: dict
138 @return: The updated I{window}.
139 @rtype: dict
140 """
141 END = 'end'
142 if END in window:
143 v = window[END]
144 if not v:
145 v = dt.utcnow()+delta(hours=1)
146 if isinstance(v, dt):
147 v = v.strftime(self.FORMAT)
148 window[END] = v
149 else:
150 if not self.__hasduration(window):
151 raise Exception,\
152 'Window() must have "end" or one of: %s' % \
153 str(self.DURATION)
154 return window
155
157 """
158 Get whether one of the duration keywords are specified
159 in the I{window} definition.
160 @param window: The window specification.
161 @type window: dict
162 @return: True if found.
163 @rtype: bool
164 """
165 for k in self.DURATION:
166 if k in window:
167 return True
168 return False
169
171 """
172 Convert to datetime objects.
173 @param now: The current UTC time.
174 @type now: datetime
175 @return: (begin, end)
176 @rtype: (datetime, datetime)
177 """
178 DURATION = ('days', 'seconds', 'minutes', 'hours', 'weeks')
179 if self.begin:
180 begin = dt.strptime(self.begin, self.FORMAT)
181 else:
182 begin = (now or td.utcnow())
183 if self.end:
184 end = dt.strptime(self.begin, self.FORMAT)
185 else:
186 end = begin
187 for k,v in self.__dict__.items():
188 if k in DURATION:
189 end = end+delta(**{k:v})
190 return (begin, end)
191
193 if self:
194 return str(self.__dates())
195 else:
196 return 'Empty'
197
198
200 """
201 Request window missed.
202 """
203
206
207
209 """
210 Request window pending.
211 """
212
215