1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """
17 utility functions to handle differences in pysqlite versions
18 These are from Wichert Akkerman <wichert@deephackmode.org>'s python-dhm
19 http://www.wiggy.net/code/python-dhm
20 """
21
22 try:
23 import sqlite3 as sqlite
24 except ImportError:
25 import sqlite
26
28 """Tokenizer error class"""
29 pass
30
31 -def Tokenize(str, whitespace=" \t\r\n", quotes="\"", escapes="\\"):
32 """String tokenizer
33
34 This function tokenizes a string while taking quotation and
35 escaping into account.
36
37 >>> import dhm.strtools
38 >>> dhm.strtools.Tokenize("this is a test")
39 ['this', 'is', 'a', 'test']
40 >>> dhm.strtools.Tokenize("this \"is a\" test")
41 ['this', 'is a', 'test']
42 >>> dhm.strtools.Tokenize("this \\\"is\\\" a test")
43 ['this', '"is"', 'a', 'test']
44 >>> dhm.strtools.Tokenize("this \"is a test")
45 Traceback (most recent call last):
46 File "<stdin>", line 1, in ?
47 File "/usr/local/lib/python2.2/site-packages/dhm/strtools.py", line 80, in Tokenize
48 raise TokenizeError, "Unexpected end of string in quoted text"
49 dhm.strtools.TokenizeError: Unexecpted end of string in quoted text
50
51 @param str: string to tokenize
52 @type str: string
53 @param whitespace: whitespace characters seperating tokens
54 @type whitespace: string
55 @param quotes: legal quoting characters
56 @type quotes: string
57 @param escapes: characters which can escape quoting characters
58 @type escapes: string
59 @return: list of tokens
60 @rtype: sequence of strings
61 """
62 (buffer, tokens, curtoken, quote)=(str, [], None, None)
63
64 try:
65 while buffer:
66 if buffer[0]==quote:
67 quote=None
68 elif (quote==None) and (buffer[0] in quotes):
69 quote=buffer[0]
70 elif buffer[0] in whitespace:
71 if quote!=None:
72 curtoken+=buffer[0]
73 else:
74 tokens.append(curtoken)
75 curtoken=None
76 while buffer[1] in whitespace:
77 buffer=buffer[1:]
78 elif buffer[0] in escapes:
79 if curtoken==None:
80 curtoken=buffer[1]
81 else:
82 curtoken+=buffer[1]
83 buffer=buffer[1:]
84 else:
85 if curtoken==None:
86 curtoken=buffer[0]
87 else:
88 curtoken+=buffer[0]
89
90 buffer=buffer[1:]
91 except IndexError:
92 raise TokenizeError, "Unexpected end of string"
93
94 if quote:
95 raise TokenizeError, "Unexpected end of string in quoted text"
96
97 if curtoken!=None:
98 tokens.append(curtoken)
99
100 return tokens
101
102
139
140
156
158 """
159 Execute a python 2.5 (sqlite3) style query.
160
161 @param cursor: A sqlite cursor
162 @param query: The query to execute
163 @param params: An optional list of parameters to the query
164 """
165 if params is None:
166 return cursor.execute(query)
167
168 return cursor.execute(query, params)
169
170 if sqlite.version_info[0] > 1:
171 executeSQL = executeSQLQmark
172 else:
173 executeSQL = executeSQLPyFormat
174
175
177 """ Apply SQLite escaping, if needed. Returns pattern and esc. """
178 esc = ''
179 if "_" in pattern or "%" in pattern:
180 esc = ' ESCAPE "!"'
181 pattern = pattern.replace("!", "!!")
182 pattern = pattern.replace("%", "!%")
183 pattern = pattern.replace("_", "!_")
184 return (pattern, esc)
185
187 """ Converts patterns to SQL LIKE format, if required (or gives up if
188 not possible). """
189 ret = []
190 for pattern in patterns:
191 if '[' in pattern:
192 return []
193
194
195 (pattern, esc) = sql_esc(pattern)
196 pattern = pattern.replace("*", "%")
197 pattern = pattern.replace("?", "_")
198 ret.append((pattern, esc))
199 return ret
200