Package pulp :: Package messaging :: Module window
[hide private]
[frames] | no frames]

Source Code for Module pulp.messaging.window

  1  # 
  2  # Copyright (c) 2010 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15   
 16  """ 
 17  Contains maintenance window classes. 
 18  """ 
 19   
 20  from pulp.messaging import * 
 21  from datetime import datetime as dt 
 22  from datetime import timedelta as delta 
 23   
 24   
 25   
26 -class Window(Envelope):
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 dict.__init__(self, *D) 58 return 59 if window: 60 self.__setbegin(window) 61 self.__setend(window) 62 dict.__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() 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() 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() 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):
171 """ 172 Convert to datetime objects. 173 @return: (begin, end) 174 @rtype: (datetime, datetime) 175 """ 176 DURATION = ('days', 'seconds', 'minutes', 'hours', 'weeks') 177 if self.begin: 178 begin = dt.strptime(self.begin, self.FORMAT) 179 else: 180 begin = dt.utcnow() 181 if self.end: 182 end = dt.strptime(self.begin, self.FORMAT) 183 else: 184 end = begin 185 for k,v in self.items(): 186 if k in DURATION: 187 end = end+delta(**{k:v}) 188 return (begin, end)
189
190 - def __str__(self):
191 if self: 192 return str(self.__dates()) 193 else: 194 return 'Empty'
195 196
197 -class WindowMissed(Exception):
198 """ 199 Request window missed. 200 """ 201
202 - def __init__(self, sn):
203 Exception.__init__(self, sn)
204 205
206 -class WindowPending(Exception):
207 """ 208 Request window pending. 209 """ 210
211 - def __init__(self, sn):
212 Exception.__init__(self, sn)
213