2 ################################################################################
3 # Copyright (c) 2003, Pfizer
4 # Copyright (c) 2001, Cayce Ullman.
5 # Copyright (c) 2001, Brian Matthews.
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions are met:
11 # Redistributions of source code must retain the above copyright notice, this
12 # list of conditions and the following disclaimer.
14 # Redistributions in binary form must reproduce the above copyright notice,
15 # this list of conditions and the following disclaimer in the documentation
16 # and/or other materials provided with the distribution.
18 # Neither the name of actzero, inc. nor the names of its contributors may
19 # be used to endorse or promote products derived from this software without
20 # specific prior written permission.
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
26 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ################################################################################
36 ident = '$Id: Utilities.py 1298 2006-11-07 00:54:15Z sanxiyn $'
37 from version import __version__
47 ################################################################################
49 ################################################################################
50 def collapseWhiteSpace(s):
51 return re.sub('\s+', ' ', s).strip()
53 def decodeHexString(data):
55 '0': 0x0, '1': 0x1, '2': 0x2, '3': 0x3, '4': 0x4,
56 '5': 0x5, '6': 0x6, '7': 0x7, '8': 0x8, '9': 0x9,
58 'a': 0xa, 'b': 0xb, 'c': 0xc, 'd': 0xd, 'e': 0xe,
61 'A': 0xa, 'B': 0xb, 'C': 0xc, 'D': 0xd, 'E': 0xe,
65 ws = string.whitespace
81 if c in string.whitespace:
88 "invalid hex string character `%s'" % c
91 bin += chr(high * 16 + c)
100 raise ValueError, "invalid hex string length"
103 if data[i] not in string.whitespace:
105 "invalid hex string character `%s'" % c
111 def encodeHexString(data):
119 def leapMonth(year, month):
120 return month == 2 and \
122 (year % 100 != 0 or year % 400 == 0)
124 def cleanDate(d, first = 0):
125 ranges = (None, (1, 12), (1, 31), (0, 23), (0, 59), (0, 61))
126 months = (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
127 names = ('year', 'month', 'day', 'hours', 'minutes', 'seconds')
130 raise ValueError, "date must have 6 elements"
132 for i in range(first, 6):
135 if type(s) == FloatType:
139 except OverflowError:
145 raise ValueError, "%s must be integral" % names[i]
148 elif type(s) == LongType:
151 elif type(s) != IntType:
152 raise TypeError, "%s isn't a valid type" % names[i]
154 if i == first and s < 0:
157 if ranges[i] != None and \
158 (s < ranges[i][0] or ranges[i][1] < s):
159 raise ValueError, "%s out of range" % names[i]
161 if first < 6 and d[5] >= 61:
162 raise ValueError, "seconds out of range"
165 leap = first < 1 and leapMonth(d[0], d[1])
167 if d[2] > months[d[1]] + leap:
168 raise ValueError, "day out of range"
170 def debugHeader(title):
171 s = '*** ' + title + ' '
172 print s + ('*' * (72 - len(s)))
174 def debugFooter(title):