Package gofer :: Package rmi :: Module window
[hide private]
[frames] | no frames]

Source Code for Module gofer.rmi.window

  1  # 
  2  # Copyright (c) 2011 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU Lesser General Public 
  5  # License as published by the Free Software Foundation; either version 
  6  # 2 of the License (LGPLv2) or (at your option) any later version. 
  7  # There is NO WARRANTY for this software, express or implied, 
  8  # including the implied warranties of MERCHANTABILITY, 
  9  # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should 
 10  # have received a copy of LGPLv2 along with this software; if not, see 
 11  # http://www.gnu.org/licenses/old-licenses/lgpl-2.0.txt. 
 12  # 
 13  # Jeff Ortel <jortel@redhat.com> 
 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   
26 -class Window(Options):
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
41 - def __init__(self, *D, **window):
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
64 - def match(self):
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
79 - def future(self):
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
93 - def past(self):
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
107 - def __setbegin(self, window):
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
130 - def __setend(self, window):
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
156 - def __hasduration(self, window):
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
170 - def __dates(self, now=None):
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
192 - def __str__(self):
193 if self: 194 return str(self.__dates()) 195 else: 196 return 'Empty'
197 198
199 -class WindowMissed(Exception):
200 """ 201 Request window missed. 202 """ 203
204 - def __init__(self, sn):
205 Exception.__init__(self, sn)
206 207
208 -class WindowPending(Exception):
209 """ 210 Request window pending. 211 """ 212
213 - def __init__(self, sn):
214 Exception.__init__(self, sn)
215