forked from camvine/fakesimpledb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fakesimpledb.py
executable file
·267 lines (212 loc) · 8.44 KB
/
fakesimpledb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python
#
# Test implementation of simpledb's API that maps on an sqlite database
# per domain. It follows the API specified here:
#
# http://docs.amazonwebservices.com/AmazonSimpleDB/2009-04-15/DeveloperGuide/
#
# Requires jinja2, cherrypy, and sqlite3
#
# Michael Dales ([email protected])
#
# Copyright (c) 2010 Cambridge Visual Networks Ltd.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import cherrypy
import jinja2
import os
import re
import sqlite3
import uuid
BIND_ADDR = '0.0.0.0' # Address to bind() and listen() on
try:
SERVER_PORT = int(os.environ['FAKESIMPLEDB_PORT'])
except KeyError:
SERVER_PORT = 8080
try:
DATA_DIR = os.environ['FAKESIMPLEDB_DATA_DIR']
except KeyError:
DATA_DIR = os.path.join(os.getcwd(), 'fakesimpledbdata')
try:
TEMPLATE_DIR = os.environ['FAKESIMPELDB_TEMPLATE_DIR']
except KeyError:
TEMPLATE_DIR = os.path.join(os.getcwd(), 'templates')
##############################################################################
##############################################################################
def render_to_string(template_name, params={}):
# stock param
params['request_id'] = uuid.uuid4()
filename = os.path.join(TEMPLATE_DIR, template_name)
template = jinja2.Template(open(filename).read())
result = template.render(**params)
return result
##############################################################################
##############################################################################
def create_domain(DomainName):
db_name = os.path.join(DATA_DIR, DomainName)
conn = sqlite3.connect(db_name)
conn.commit()
def delete_domain(DomainName):
db_name = os.path.join(DATA_DIR, DomainName)
try:
os.unlink(db_name)
except OSError:
pass
def list_domains():
return os.listdir(DATA_DIR)
def delete_attributes(DomainName, ItemName):
db_name = os.path.join(DATA_DIR, DomainName)
conn = sqlite3.connect(db_name)
c = conn.cursor()
c.execute('delete from datatable where sdbkey=?', (ItemName,))
conn.commit()
def put_attributes(DomainName, ItemName, **kwargs):
# step 1 is to turn the attributes into a sane dictionary we can work with
# the args are listed Attribute.0.name and Attribute.0.Value etc.
attributes = {}
index = 0
while 1:
if not kwargs.has_key('Attribute.%d.Name' % index):
break
attributes[kwargs['Attribute.%d.Name' % index]] = kwargs['Attribute.%d.Value' % index]
index += 1
if len(attributes) == 0:
return
# now to shove these into sqlite. we lazily create the table if there's
# no data in there yet
db_name = os.path.join(DATA_DIR, DomainName)
conn = sqlite3.connect(db_name)
sorted_keys = attributes.keys()
sorted_keys.sort()
c = conn.cursor()
table_info = 'sdbkey text, '
for key in sorted_keys:
table_info += '%s text, ' % key
query = 'create table if not exists datatable (%s)' % table_info[:-2]
c.execute(query)
value_tempate = '?, '
value_list = [ItemName,]
for key in sorted_keys:
value_tempate += "?, "
value_list.append(attributes[key])
c.execute('insert into datatable values (%s)' % value_tempate[:-2], value_list)
conn.commit()
c.close()
def batch_put_attributes(DomainName, **kwargs):
# here we just slice it up and call put_attributes repeatedly
index = 0
while 1:
if not kwargs.has_key('Item.%d.ItemName' % index):
break
ItemName = kwargs['Item.%d.ItemName' % index]
attributes = {}
for key in kwargs:
if key.startswith('Item.%d' % index):
attributes[key.replace('Item.%d.' % index, '')] = kwargs[key]
put_attributes(DomainName, **attributes)
index += 1
def get_attributes(DomainName, ItemName):
db_name = os.path.join(DATA_DIR, DomainName)
conn = sqlite3.connect(db_name)
attrs = {}
c = conn.cursor()
c.execute('select * from datatable where sdbkey=?', (ItemName,))
# should be a unique response
try:
row = c.fetchall()[0]
except IndexError:
return attrs
# we also need the column names
c.execute('PRAGMA table_info(datatable)')
columns = [x[1] for x in c.fetchall()]
for i in xrange(len(columns)):
if columns[i] == 'sdbkey':
continue
attrs[columns[i]] = row[i]
conn.commit()
c.close()
return attrs
def select_items(SelectExpression):
# the domain name is hidden in the expression
r = re.compile("""(select|SELECT).*(from|FROM)\s['"`]{0,1}([a-zA-Z_0-9]+)['"`]{0,1}\s.*""")
parts = r.match(SelectExpression).groups()
DomainName = parts[2]
db_name = os.path.join(DATA_DIR, DomainName)
conn = sqlite3.connect(db_name)
c = conn.cursor()
query = SelectExpression.replace(DomainName, 'datatable')
c.execute(query)
query_results = c.fetchall()
# we also need the column names
c.execute('PRAGMA table_info(datatable)')
columns = [x[1] for x in c.fetchall()]
results = []
for row in query_results:
attrs = {}
for i in xrange(len(columns)):
attrs[columns[i]] = row[i]
results.append(attrs)
conn.commit()
c.close()
return results
##############################################################################
##############################################################################
class SimpleDBServer(object):
def index(self, Action, **kwargs):
if Action == 'CreateDomain':
create_domain(kwargs['DomainName'])
return render_to_string('CreateDomain.xml')
elif Action == 'DeleteDomain':
delete_domain(kwargs['DomainName'])
return render_to_string('DeleteDomain.xml')
elif Action == 'ListDomains':
domains = list_domains()
return render_to_string('ListDomains.xml', {'domain_list': domains})
elif Action == 'DeleteAttributes':
delete_attributes(kwargs['DomainName'], kwargs['ItemName'])
return render_to_string('DeleteAttributes.xml')
elif Action == 'PutAttributes':
put_attributes(**kwargs)
return render_to_string('PutAttributes.xml')
elif Action == 'GetAttributes':
attibutes = get_attributes(kwargs['DomainName'], kwargs['ItemName'])
return render_to_string('GetAttributes.xml', {'attrs': attibutes})
elif Action == 'BatchPutAttributes':
batch_put_attributes(**kwargs)
return render_to_string('BatchPutAttributes.xml')
elif Action == 'Select':
items = select_items(kwargs['SelectExpression'])
return render_to_string('Select.xml', {'items': items})
else:
print Action
print kwargs
return "like, whatever."
index.exposed = True
##############################################################################
##############################################################################
if __name__ == "__main__":
try:
os.makedirs(DATA_DIR)
except OSError:
pass
# Global configuration
cherrypy.config.update({'server.socket_host': BIND_ADDR,
'server.socket_port': SERVER_PORT})
# Run server
cherrypy.quickstart(SimpleDBServer())