Revision: | 1.2 |
Committed: | Wed Jan 17 18:17:59 2007 UTC (18 years, 3 months ago) by slacapra |
Content type: | text/x-python |
Branch: | MAIN |
CVS Tags: | CRAB_2_9_1, CRAB_2_9_1_pre2, CRAB_2_9_1_pre1, CRAB_2_9_0, CRAB_2_9_0_pre2, CRAB_2_9_0_pre1, CRAB_2_8_8, CRAB_2_8_8_pre1, CRAB_2_8_7_patch3, CRAB_2_8_7_patch2, CRAB_2_8_7_patch1, CRAB_2_8_7, CRAB_2_8_7_pre2, CRAB_2_8_7_pre1, CRAB_2_8_6, CRAB_2_8_6_pre1, CRAB_2_8_5_patch3, CRAB_2_8_5_patch2, CRAB_2_8_5_patch1, CRAB_2_8_5, CRAB_2_8_5_pre5, CRAB_2_8_5_pre4, CRAB_2_8_5_pre3, CRAB_2_8_4_patch3, CRAB_2_8_5_pre2, CRAB_2_8_4_patch2, CRAB_2_8_5_pre1, CRAB_2_8_4_patch1, CRAB_2_8_4, CRAB_2_8_4_pre5, CRAB_2_8_4_pre4, CRAB_2_8_4_pre3, CRAB_2_8_4_pre2, CRAB_2_8_4_pre1, CRAB_2_8_3, CRAB_2_8_3_pre4, CRAB_2_8_3_pre3, CRAB_2_8_3_pre2, CRAB_2_8_3_pre1, CRAB_2_8_2_patch1, CRAB_2_8_2, CRAB_2_8_2_pre5, CRAB_2_8_2_pre4, CRAB_2_8_2_pre3, CRAB_2_8_2_pre2, CRAB_2_8_2_pre1, CRAB_2_8_1, CRAB_2_8_0, CRAB_2_8_0_pre1, CRAB_2_7_10_pre3, CRAB_2_7_9_patch2_pre1, CRAB_2_7_10_pre2, CRAB_2_7_10_pre1, CRAB_2_7_9_patch1, CRAB_2_7_9, CRAB_2_7_9_pre5, CRAB_2_7_9_pre4, CRAB_2_7_9_pre3, CRAB_2_7_9_pre2, CRAB_2_7_8_patch2, CRAB_2_7_9_pre1, CRAB_2_7_8_patch2_pre1, CRAB_2_7_8_patch1, CRAB_2_7_8_patch1_pre1, CRAB_2_7_8, CRAB_2_7_8_pre3, CRAB_2_7_8_pre2, CRAB_2_7_8_dash3, CRAB_2_7_8_dash2, CRAB_2_7_8_dash, CRAB_2_7_7_patch1, CRAB_2_7_7_patch1_pre1, CRAB_2_7_8_pre1, CRAB_2_7_7, CRAB_2_7_7_pre2, CRAB_2_7_7_pre1, CRAB_2_7_6_patch1, CRAB_2_7_6, CRAB_2_7_6_pre1, CRAB_2_7_5_patch1, CRAB_2_7_5, CRAB_2_7_5_pre3, CRAB_2_7_5_pre2, CRAB_2_7_5_pre1, CRAB_2_7_4_patch1, CRAB_2_7_4, CRAB_2_7_4_pre6, CRAB_2_7_4_pre5, CRAB_2_7_4_pre4, CRAB_2_7_4_pre3, CRAB_2_7_4_pre2, CRAB_2_7_4_pre1, CRAB_2_7_3, CRAB_2_7_3_pre3, CRAB_2_7_3_pre3_beta, CRAB_2_7_3_pre2, CRAB_2_7_3_pre2_beta, CRAB_2_7_3_pre1, CRAB_2_7_3_beta3, CRAB_2_7_3_beta2, CRAB_2_7_3_beta1, CRAB_2_7_3_beta, CRAB_2_7_2_p1, CRAB_2_7_1_branch_firstMERGE, CRAB_2_7_2, CRAB_2_7_2_pre4, CRAB_2_7_2_pre3, CRAB_2_7_2_pre2, CRAB_2_7_2_pre1, CRAB_2_7_1, fede_170310, CRAB_2_7_1_pre12, CRAB_2_7_1_pre11, CRAB_2_7_1_pre10, CRAB_2_7_1_pre9, CRAB_LumiMask, CRAB_2_7_lumi, from_LimiMask, CRAB_2_7_1_pre8, CRAB_2_7_1_pre6, CRAB_2_7_1_pre5, CRAB_2_7_1_wmbs_pre4, CRAB_2_7_1_pre4, CRAB_2_7_1_pre3, CRAB_2_6_6_pre6, CRAB_2_7_1_pre2, CRAB_2_6_6_pre5, CRAB_2_7_1_pre1, CRAB_2_6_6_pre4, CRAB_2_6_6_pre3, CRAB_2_6_6_pre2, CRAB_2_6_6_check, CRAB_2_6_6, CRAB_2_6_6_pre1, CRAB_2_7_0, CRAB_2_6_5, CRAB_2_7_0_pre8, CRAB_2_6_5_pre1, CRAB_2_7_0_pre7, CRAB_2_6_4, CRAB_2_7_0_pre6, CRAB_2_6_4_pre1, CRAB_2_7_0_pre5, CRAB_2_6_3_patch_2, CRAB_2_6_3_patch_2_pre2, CRAB_2_6_3_patch_2_pre1, CRAB_2_6_3_patch_1, CRAB_2_7_0_pre4, CRAB_2_7_0_pre3, CRAB_2_6_3, CRAB_2_6_3_pre5, CRAB_2_6_3_pre4, CRAB_2_6_3_pre3, CRAB_2_6_3_pre2, CRAB_2_7_0_pre2, CRAB_2_6_3_pre1, test_1, CRAB_2_7_0_pre1, CRAB_2_6_2, CRAB_2_6_2_pre2, CRAB_2_6_2_pre1, CRAB_2_6_1_pre4, CRAB_2_6_1_pre3, CRAB_2_6_1_pre2, CRAB_2_6_1_pre1, CRAB_2_6_1, CRAB_2_6_0, CRAB_2_6_0_pre14, CRAB_2_6_0_pre13, CRAB_2_6_0_pre12, CRAB_2_6_0_pre11, CRAB_2_6_0_pre10, CRAB_2_6_0_pre9, CRAB_2_6_0_pre8, CRAB_2_6_0_pre7, CRAB_2_6_0_pre6, CRAB_2_6_0_pre5, CRAB_2_6_0_pre4, CRAB_2_6_0_pre3, CRAB_2_6_0_pre2, CRAB_2_6_0_pre1, CRAB_2_5_1, CRAB_2_5_1_pre4, CRAB_2_5_1_pre3, CRAB_2_5_1_pre2, CRAB_2_5_1_pre1, CRAB_2_5_0, CRAB_2_5_0_pre7, CRAB_2_5_0_pre6, CRAB_2_5_0_pre5, CRAB_2_5_0_pre4, CRAB_2_5_0_pre3, CRAB_2_5_0_pre2, CRAB_2_5_0_pre1, CRAB_2_4_4, CRAB_2_4_4_pre6, CRAB_2_4_4_pre5, CRAB_2_4_4_pre4, CRAB_2_4_4_pre3, CRAB_2_4_4_pre2, CRAB_2_4_4_pre1, CRAB_2_4_3, CRAB_2_4_3_pre8, CRAB_2_4_3_pre7, CRAB_2_4_3_pre6, CRAB_2_4_3_pre5, CRAB_2_4_3_pre3, CRAB_2_4_3_pre2, CRAB_2_4_3_pre1, CRAB_2_4_2, CRAB_2_4_2_pre3, CRAB_2_4_2_pre2, CRAB_2_4_2_pre1, CRAB_2_4_1, CRAB_2_4_1_pre4, CRAB_2_4_1_pre3, CRAB_2_4_1_pre2, CRAB_2_4_1_pre1, CRAB_2_4_0_Tutorial, CRAB_2_4_0_Tutorial_pre1, CRAB_2_4_0, CRAB_2_4_0_pre9, CRAB_2_4_0_pre8, CRAB_2_4_0_pre7, CRAB_2_4_0_pre6, CRAB_2_4_0_pre5, CRAB_2_4_0_pre4, CRAB_2_4_0_pre3, CRAB_2_4_0_pre2, CRAB_2_4_0_pre1, CRAB_DLS_PHED1, CRAB_DLS_PHED, CRAB_2_3_2_Fnal, CRAB_2_3_2, CRAB_2_3_2_pre7, CRAB_2_3_2_pre5, CRAB_2_3_2_pre4, CRAB_2_3_2_pre3, CRAB_2_3_2_pre2, CRAB_2_3_2_pre1, CRAB_2_4_0_test, CRAB_2_3_1, CRAB_2_3_1_pre6, CRAB_2_3_1_pre5, CRAB_2_3_1_pre4, CRAB_2_3_1_pre3, CRAB_2_3_1_pre2, CRAB_2_3_1_pre1, CRAB_2_3_0, CRAB_2_3_0_pre6, CRAB_2_3_0_pre1, CRAB_2_2_2_pre5, CRAB_2_2_2_pre4, CRAB_2_2_2_pre3, CRAB_2_2_2_pre2, CRAB_2_2_2_pre1, CRAB_2_2_1, CRAB_2_2_1_pre6, CRAB_2_2_1_pre5, CRAB_2_2_1_pre4, PRODCOMMON_0_10_7_testCS2, CRAB_2_2_1_pre3, CRAB_2_2_1_pre2, CRAB_2_2_1_pre1, CRAB_2_2_0, CRAB_2_2_0_pre21, CRAB_2_2_0_pre19, CRAB_2_2_0_pre18, CRAB_2_2_0_pre17, CRAB_2_2_0_pre16, CRAB_2_2_0_pre15, CRAB_2_2_0_pre13, CRAB_2_2_0_pre12, CRAB_2_2_0_pre11, CRAB_2_2_0_pre10, bp_osg_bdii, CRAB_2_2_0_pre9, CRAB_2_2_0_pre8, CRAB_2_2_0_pre7, CRAB_2_1_2, CRAB_2_2_0_pre5, CRAB_2_1_2_pre2, CRAB_2_1_2_pre1, CRAB_2_2_0_pre4, CRAB_2_2_0_pre2, CRAB_2_1_1, CRAB_2_1_1_pre3, CRAB_2_2_0_pre1, CRAB_2_1_1_pre1, CRAB_2_1_0, CRAB_2_1_0_pre6, CRAB_2_1_0_pre5, CRAB_2_1_0_pre4, CRAB_2_1_0_pre3, CRAB_2_1_0_pre2, CRAB_2_1_0_pre1, CRAB_2_0_4, CRAB_2_0_4_pre2, CRAB_2_0_4_pre1, CRAB_2_0_3, CRAB_2_0_3_pre1, CRAB_2_0_2, CRAB_2_0_2_pre6, CRAB_2_0_2_pre5, CRAB_2_0_2_pre4, CRAB_2_0_2_pre3, CRAB_1_5_4_SLC3, CRAB_1_5_4_SLC3_pre4, CRAB_2_0_2_pre2, CRAB_2_0_2_pre1, CRAB_1_5_4_SLC3_pre3, CRAB_2_0_1, CRAB_1_5_4_SLC3_pre2, CRAB_2_0_1_pre1, CRAB_1_5_4_SLC3_pre1, CRAB_2_0_0, CRAB_2_0_0_pre10, CRAB_2_0_0_pre9, CRAB_1_5_4, CRAB_1_5_4_pre2, CRAB_1_5_4_pre1, CRAB_2_0_0_pre7, CRAB_2_0_0_pre6, CRAB_1_5_3, CRAB_1_5_3_pre5, CRAB_1_5_3_pre4, CRAB_2_0_0_pre5, CRAB_1_5_3_pre3, configure, CRAB_2_0_0_pre4, CRAB_1_5_3_pre2, CRAB_1_5_3_pre1, CRAB_2_0_0_pre3, CRAB_1_5_2, CRAB_2_0_0_pre2, CRAB_2_0_0_pre1, CRAB_1_5_1, CRAB_1_5_1_pre4, CRAB_1_5_1_pre3, CRAB_1_5_1_pre2, CRAB_1_5_1_pre1, CRAB_1_5_0, CRAB_1_5_0_pre9, CRAB_1_5_0_pre8, CRAB_1_5_0_pre7, CRAB_1_5_0_pre6, CRAB_1_5_0_pre5, CRAB_1_5_0_pre4, HEAD |
Branch point for: | CRAB_multiout, CRAB_2_7_1_branch, Lumi2_8, CRAB_2_6_X_br, AnaDataSet, CRAB_2_3_0_br, osg_bdii, CRAB_2_1_2_br, CRAB_2_1_1_pre2, CRAB_1_5_4_SLC3_pre4_br, CRAB_1_5_4_SLC3_start |
Changes since 1.1: | +3 -4 lines |
Log Message: | many minor fixes reported by pychecker, mostly unsude import and unused variables |
# | User | Rev | Content |
---|---|---|---|
1 | slacapra | 1.2 | import string |
2 | slacapra | 1.1 | |
3 | """ | ||
4 | Unserialize class for the PHP RefDB serialization format. | ||
5 | |||
6 | @version v1.0 | ||
7 | @@author Giovanni Ciraolo | ||
8 | |||
9 | @ From A Project of | ||
10 | @Scott Hurring; scott at hurring dot com | ||
11 | @copyright Copyright (c) 2005 Scott Hurring | ||
12 | @license http://opensource.org/licenses/gpl-license.php GNU Public License | ||
13 | slacapra | 1.2 | $Id: UnserializePHP.py,v 1.1 2005/07/25 14:31:24 slacapra Exp $ |
14 | slacapra | 1.1 | |
15 | Most recent version can be found at: | ||
16 | http://hurring.com/code/python/phpserialize/ | ||
17 | |||
18 | Usage: | ||
19 | # Create an instance of the unserialize engine | ||
20 | u = PHPUnserialize() | ||
21 | # unserialize some string into python data | ||
22 | data = u.unserialize(serialized_string) | ||
23 | |||
24 | Please see README.txt for more information. | ||
25 | """ | ||
26 | class PHPUnserializeError: | ||
27 | def __init__(self,line): | ||
28 | print '\nERROR Unserializing: '+line+'\n' | ||
29 | pass | ||
30 | |||
31 | class PHPUnserialize(object): | ||
32 | """ | ||
33 | Class to unserialize something from the PHP Serialize format. | ||
34 | |||
35 | Usage: | ||
36 | u = PHPUnserialize() | ||
37 | data = u.unserialize(serialized_string) | ||
38 | """ | ||
39 | |||
40 | def __init__(self): | ||
41 | pass | ||
42 | |||
43 | def unserialize(self, data): | ||
44 | return self._unserialize(data, 0)[2] | ||
45 | |||
46 | def _unserialize(self, data, offset=0): | ||
47 | """ | ||
48 | Find the next token and unserialize it. | ||
49 | Recurse on array. | ||
50 | |||
51 | offset = raw offset from start of data | ||
52 | """ | ||
53 | |||
54 | dtype = string.lower(data[offset:offset+1]) | ||
55 | |||
56 | #print "# dtype =", dtype | ||
57 | |||
58 | # 't:' = 2 chars | ||
59 | dataoffset = offset + 2 | ||
60 | typeconvert = lambda x : x | ||
61 | slacapra | 1.2 | chars = 0 |
62 | slacapra | 1.1 | |
63 | # int => Integer | ||
64 | if dtype == 'i': | ||
65 | typeconvert = lambda x : int(x) | ||
66 | (chars, readdata) = self.read_until(data, dataoffset, ';') | ||
67 | # +1 for end semicolon | ||
68 | dataoffset += chars + 1 | ||
69 | elif dtype == 'o': | ||
70 | (chars, keys) = self.read_until(data, dataoffset, ':') | ||
71 | dataoffset += chars + 1 | ||
72 | (chars, keys) = self.read_until(data, dataoffset, ':') | ||
73 | dataoffset += chars + 1 | ||
74 | (chars, keys) = self.read_until(data, dataoffset, ':') | ||
75 | #print chars,keys | ||
76 | dataoffset += chars + 2 | ||
77 | readdata = {} | ||
78 | for i in range(0, int(keys)): | ||
79 | (ktype, kchars, key) = self._unserialize(data, dataoffset) | ||
80 | dataoffset += kchars | ||
81 | # print "Key(%i) = (%s, %i, %s) %i" % (i, ktype, kchars, key, dataoffset) | ||
82 | |||
83 | # Read value of the key | ||
84 | (vtype, vchars, value) = self._unserialize(data, dataoffset) | ||
85 | dataoffset += vchars | ||
86 | # print "Value(%i) = (%s, %i, %s) %i" % (i, vtype, vchars, value, dataoffset) | ||
87 | |||
88 | # Set the list element | ||
89 | readdata[key] = value | ||
90 | |||
91 | |||
92 | # +1 for end semicolon | ||
93 | |||
94 | dataoffset += chars - 1 | ||
95 | # bool => Boolean | ||
96 | elif dtype == 'b': | ||
97 | typeconvert = lambda x : (int(x) == 1) | ||
98 | (chars, readdata) = self.read_until(data, dataoffset, ';') | ||
99 | # +1 for end semicolon | ||
100 | dataoffset += chars + 1 | ||
101 | |||
102 | # double => Floating Point | ||
103 | elif dtype == 'd': | ||
104 | typeconvert = lambda x : float(x) | ||
105 | (chars, readdata) = self.read_until(data, dataoffset, ';') | ||
106 | # +1 for end semicolon | ||
107 | dataoffset += chars + 1 | ||
108 | |||
109 | # n => None | ||
110 | elif dtype == 'n': | ||
111 | readdata = None | ||
112 | |||
113 | # s => String | ||
114 | elif dtype == 's': | ||
115 | (chars, stringlength) = self.read_until(data, dataoffset, ':') | ||
116 | # +2 for colons around length field | ||
117 | dataoffset += chars + 2 | ||
118 | |||
119 | # +1 for start quote | ||
120 | (chars, readdata) = self.read_chars(data, dataoffset+1, int(stringlength)) | ||
121 | # +2 for endquote semicolon | ||
122 | dataoffset += chars + 2 | ||
123 | |||
124 | if chars != int(stringlength) != int(readdata): | ||
125 | raise Exception("String length mismatch") | ||
126 | |||
127 | # array => Dict | ||
128 | # If you originally serialized a Tuple or List, it will | ||
129 | # be unserialized as a Dict. PHP doesn't have tuples or lists, | ||
130 | # only arrays - so everything has to get converted into an array | ||
131 | # when serializing and the original type of the array is lost | ||
132 | elif dtype == 'a': | ||
133 | readdata = {} | ||
134 | |||
135 | # How many keys does this list have? | ||
136 | (chars, keys) = self.read_until(data, dataoffset, ':') | ||
137 | # +2 for colons around length field | ||
138 | dataoffset += chars + 2 | ||
139 | |||
140 | # Loop through and fetch this number of key/value pairs | ||
141 | for i in range(0, int(keys)): | ||
142 | # Read the key | ||
143 | (ktype, kchars, key) = self._unserialize(data, dataoffset) | ||
144 | dataoffset += kchars | ||
145 | #print "Key(%i) = (%s, %i, %s) %i" % (i, ktype, kchars, key, dataoffset) | ||
146 | |||
147 | # Read value of the key | ||
148 | (vtype, vchars, value) = self._unserialize(data, dataoffset) | ||
149 | dataoffset += vchars | ||
150 | #print "Value(%i) = (%s, %i, %s) %i" % (i, vtype, vchars, value, dataoffset) | ||
151 | |||
152 | # Set the list element | ||
153 | readdata[key] = value | ||
154 | |||
155 | # +1 for end semicolon | ||
156 | dataoffset += 1 | ||
157 | #chars = int(dataoffset) - start | ||
158 | |||
159 | # I don't know how to unserialize this | ||
160 | |||
161 | |||
162 | else: | ||
163 | raise Exception("Unknown / Unhandled data type (%s)!" % dtype) | ||
164 | |||
165 | |||
166 | return (dtype, dataoffset-offset, typeconvert(readdata)) | ||
167 | |||
168 | def read_until(self, data, offset, stopchar): | ||
169 | """ | ||
170 | Read from data[offset] until you encounter some char 'stopchar'. | ||
171 | """ | ||
172 | buf = [] | ||
173 | char = data[offset:offset+1] | ||
174 | i = 2 | ||
175 | while char != stopchar: | ||
176 | # Consumed all the characters and havent found ';' | ||
177 | if i+offset > len(data): | ||
178 | raise Exception("Invalid") | ||
179 | buf.append(char) | ||
180 | char = data[offset+(i-1):offset+i] | ||
181 | i += 1 | ||
182 | |||
183 | # (chars_read, data) | ||
184 | return (len(buf), "".join(buf)) | ||
185 | |||
186 | def read_chars(self, data, offset, length): | ||
187 | """ | ||
188 | Read 'length' number of chars from data[offset]. | ||
189 | """ | ||
190 | buf = [] | ||
191 | # Account for the starting quote char | ||
192 | #offset += 1 | ||
193 | for i in range(0, length): | ||
194 | char = data[offset+(i-1):offset+i] | ||
195 | buf.append(char) | ||
196 | |||
197 | # (chars_read, data) | ||
198 | return (len(buf), "".join(buf)) | ||
199 | |||
200 |