|
41 |
def func_name(): |
41 |
def func_name(): |
42 |
return inspect.currentframe().f_back.f_code.co_name |
42 |
return inspect.currentframe().f_back.f_code.co_name |
43 |
|
43 |
|
44 |
def _is_base64(val): |
|
|
45 |
try: |
46 |
# It is not sufficient to run base64.decodestring to detect a base64 string. |
47 |
# When the ascii decode is not possible, it is not a base4 string. |
48 |
val.decode('ascii') |
49 |
except UnicodeDecodeError: |
50 |
return False |
51 |
try: |
52 |
# The string must be casted as str otherwise we saw something like this: |
53 |
# 11.02.2014 03:53:44,141 LDAP (INFO): _is_base64 returns True for: Í8^Ml%'<U+0097>A²ôâ/! ^RÃ |
54 |
# 11.02.2014 03:53:44,142 LDAP (WARNING): S4Cache: sqlite: near "<U+0097>A²ôâ": syntax error. SQL command was: [u"SELECT id FROM GUIDS WHERE guid='\xcd8\rl%'\x97A\xb2\xf4\xe2/! \x12\xc3';" |
55 |
base64.decodestring(str(val)) |
56 |
return True |
57 |
except binascii.Error: |
58 |
return False |
59 |
|
60 |
def _decode_base64(val): |
44 |
def _decode_base64(val): |
61 |
return base64.decodestring(val) |
45 |
return base64.decodestring(val) |
62 |
|
46 |
|
|
63 |
def _encode_base64(val): |
47 |
def _encode_base64(val): |
64 |
return base64.encodestring(val) |
48 |
return base64.encodestring(val) |
65 |
|
49 |
|
66 |
def _encode_guid(guid): |
|
|
67 |
# guid may be unicode |
68 |
|
69 |
if _is_base64(guid): |
70 |
return guid |
71 |
|
72 |
if type(guid) == type(u''): |
73 |
return guid.encode('ISO-8859-1').encode('base64') |
74 |
else: |
75 |
return unicode(guid,'latin').encode('ISO-8859-1').encode('base64') |
76 |
|
77 |
def _decode_guid(guid): |
78 |
try: |
79 |
return base64.decodestring(guid) |
80 |
except binascii.Error: |
81 |
return guid |
82 |
|
83 |
|
84 |
class EntryDiff(object): |
50 |
class EntryDiff(object): |
85 |
def __init__(self, old, new): |
51 |
def __init__(self, old, new): |
86 |
self.old = old |
52 |
self.old = old |
|
121 |
def add_entry(self, guid, entry): |
87 |
def add_entry(self, guid, entry): |
122 |
_d = ud.function('S4Cache.%s' % func_name()) |
88 |
_d = ud.function('S4Cache.%s' % func_name()) |
123 |
|
89 |
|
124 |
guid = _encode_guid(guid).strip() |
|
|
125 |
|
126 |
if not self._guid_exists(guid): |
90 |
if not self._guid_exists(guid): |
127 |
self._add_entry(guid, entry) |
91 |
self._add_entry(guid, entry) |
128 |
else: |
92 |
else: |
|
148 |
|
112 |
|
149 |
entry = {} |
113 |
entry = {} |
150 |
|
114 |
|
151 |
guid = _encode_guid(guid) |
|
|
152 |
|
153 |
guid_id = self._get_guid_id(guid) |
115 |
guid_id = self._get_guid_id(guid) |
154 |
|
116 |
|
155 |
if not guid_id: |
117 |
if not guid_id: |
|
157 |
|
119 |
|
158 |
sql_commands = [ |
120 |
sql_commands = [ |
159 |
"SELECT ATTRIBUTES.attribute,data.value from data \ |
121 |
"SELECT ATTRIBUTES.attribute,data.value from data \ |
160 |
inner join ATTRIBUTES ON data.attribute_id=attributes.id where guid_id = %s;" % (guid_id) |
122 |
inner join ATTRIBUTES ON data.attribute_id=attributes.id where guid_id = %s;" % (guid_id,) |
161 |
] |
123 |
] |
162 |
|
124 |
|
163 |
rows = self.__execute_sql_commands(sql_commands, fetch_result=True) |
125 |
rows = self.__execute_sql_commands(sql_commands, fetch_result=True) |
|
175 |
def remove_entry(self, guid): |
137 |
def remove_entry(self, guid): |
176 |
_d = ud.function('S4Cache.%s' % func_name()) |
138 |
_d = ud.function('S4Cache.%s' % func_name()) |
177 |
|
139 |
|
178 |
guid = _encode_guid(guid) |
|
|
179 |
|
180 |
guid_id = self._get_guid_id(guid) |
140 |
guid_id = self._get_guid_id(guid) |
181 |
|
141 |
|
182 |
if not guid_id: |
142 |
if not guid_id: |
|
183 |
return None |
143 |
return None |
184 |
|
144 |
|
185 |
sql_commands = [ |
145 |
sql_commands = [ |
186 |
"DELETE FROM data WHERE guid_id = '%(guid_id)s';" % ({'guid_id': guid_id}), |
146 |
"DELETE FROM data WHERE guid_id = '%(guid_id)s';" % {'guid_id': guid_id}, |
187 |
"DELETE FROM guids WHERE id = '%(guid_id)s';" % ({'guid_id': guid_id}) |
147 |
"DELETE FROM guids WHERE id = '%(guid_id)s';" % {'guid_id': guid_id} |
188 |
] |
148 |
] |
189 |
|
149 |
|
190 |
self.__execute_sql_commands(sql_commands, fetch_result=False) |
150 |
self.__execute_sql_commands(sql_commands, fetch_result=False) |
|
237 |
_d = ud.function('S4Cache.%s' % func_name()) |
197 |
_d = ud.function('S4Cache.%s' % func_name()) |
238 |
|
198 |
|
239 |
sql_commands = [ |
199 |
sql_commands = [ |
240 |
"SELECT id FROM GUIDS WHERE guid='%s';" % (_encode_guid(guid).strip()) |
200 |
"SELECT id FROM GUIDS WHERE guid='%s';" % (guid,) |
241 |
] |
201 |
] |
242 |
|
202 |
|
243 |
rows = self.__execute_sql_commands(sql_commands, fetch_result=True) |
203 |
rows = self.__execute_sql_commands(sql_commands, fetch_result=True) |
|
252 |
_d = ud.function('S4Cache.%s' % func_name()) |
212 |
_d = ud.function('S4Cache.%s' % func_name()) |
253 |
|
213 |
|
254 |
sql_commands = [ |
214 |
sql_commands = [ |
255 |
"INSERT INTO GUIDS(guid) VALUES('%s');" % (_encode_guid(guid).strip()) |
215 |
"INSERT INTO GUIDS(guid) VALUES('%s');" % (guid,) |
256 |
] |
216 |
] |
257 |
|
217 |
|
258 |
rows = self.__execute_sql_commands(sql_commands, fetch_result=False) |
218 |
rows = self.__execute_sql_commands(sql_commands, fetch_result=False) |
|
262 |
_d = ud.function('S4Cache.%s' % func_name()) |
222 |
_d = ud.function('S4Cache.%s' % func_name()) |
263 |
|
223 |
|
264 |
sql_commands = [ |
224 |
sql_commands = [ |
265 |
"SELECT id FROM ATTRIBUTES WHERE attribute='%s';" % (attr) |
225 |
"SELECT id FROM ATTRIBUTES WHERE attribute='%s';" % (attr,) |
266 |
] |
226 |
] |
267 |
|
227 |
|
268 |
rows = self.__execute_sql_commands(sql_commands, fetch_result=True) |
228 |
rows = self.__execute_sql_commands(sql_commands, fetch_result=True) |
|
282 |
_d = ud.function('S4Cache.%s' % func_name()) |
242 |
_d = ud.function('S4Cache.%s' % func_name()) |
283 |
|
243 |
|
284 |
sql_commands = [ |
244 |
sql_commands = [ |
285 |
"INSERT INTO ATTRIBUTES(attribute) VALUES('%s');" % (attr) |
245 |
"INSERT INTO ATTRIBUTES(attribute) VALUES('%s');" % (attr,) |
286 |
] |
246 |
] |
287 |
|
247 |
|
288 |
self.__execute_sql_commands(sql_commands, fetch_result=False) |
248 |
self.__execute_sql_commands(sql_commands, fetch_result=False) |