This repository has been archived by the owner on Jul 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfql.py
353 lines (285 loc) · 10.2 KB
/
fql.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
"""FQL request handler and support classes.
Based on https://developers.facebook.com/docs/reference/fql/ .
"""
__author__ = ['Ryan Barrett <mockfacebook@ryanb.org>']
import logging
import re
import json
import sqlite3
import time
import sqlparse
from sqlparse import sql
from sqlparse import tokens
import webapp2
import oauth
import schemautil
class FqlError(Exception):
"""Base error class.
Attributes:
code: integer error_code
msg: string error_msg
"""
code = None
msg = None
def __init__(self, *args):
self.msg = self.msg % args
class UnexpectedError(FqlError):
code = 601
msg = "Parser error: unexpected '%s' at position <not implemented>."
class UnexpectedEndError(FqlError):
code = 601
msg = 'Parser error: unexpected end of query.'
class WildcardError(FqlError):
code = 601
msg = 'Parser error: SELECT * is not supported. Please manually list the columns you are interested in.'
class NotIndexableError(FqlError):
code = 604
msg = 'Your statement is not indexable. The WHERE clause must contain an indexable column. Such columns are marked with * in the tables linked from http://developers.facebook.com/docs/reference/fql '
class InvalidFunctionError(FqlError):
code = 605
msg = '%s is not a valid function name.'
class ParamMismatchError(FqlError):
code = 606
msg = '%s function expects %d parameters; %d given.'
class SqliteError(FqlError):
code = -1
msg = 'SQLite error: %s'
class MissingParamError(FqlError):
code = -1
msg = 'The parameter %s is required'
class InvalidAccessTokenError(FqlError):
code = 190
msg = 'Invalid access token signature.'
class Fql(object):
"""A parsed FQL statement. Just a thin wrapper around sqlparse.sql.Statement.
Attributes:
query: original FQL query string
me: integer, the user id that me() should return
schema: schemautil.FqlSchema
Statement: sqlparse.sql.Statement
table: sql.Token or None
where: sql.Where or None
"""
# FQL functions. Maps function name to expected number of parameters.
FUNCTIONS = {
'me': 0,
'now': 0,
'strlen': 1,
'substr': 3,
'strpos': 2,
}
def __init__(self, schema, query, me):
"""Args:
query: FQL statement
me: integer, the user id that me() should return
"""
logging.debug('parsing %s' % query)
self.schema = schema
self.query = query
self.me = me
self.statement = stmt = sqlparse.parse(query)[0]
# extract table and WHERE clause, if any
self.table = None
self.where = None
from_ = stmt.token_next_match(0, tokens.Keyword, 'FROM')
if from_:
index = stmt.token_index(from_)
self.table = stmt.token_next(index)
if self.table.is_group():
self.table = self.table.token_first()
self.where = stmt.token_next_by_instance(0, sql.Where)
logging.debug('table %s, where %s' % (self.table, self.where))
def table_name(self):
"""Returns the table name, or '' if None.
"""
if self.table:
return self.table.value
else:
return ''
def validate(self):
"""Checks the query for Facebook API semantic errors.
Returns the error response string if there is an error, otherwise None.
"""
first = self.statement.tokens[0].value
if first != 'SELECT':
raise UnexpectedError(first)
elif self.statement.token_next(1).match(tokens.Wildcard, '*'):
raise WildcardError()
elif not self.where:
raise UnexpectedEndError()
elif not self.table:
raise UnexpectedError('WHERE')
def check_indexable(token_list):
"""Recursive function that checks for non-indexable columns."""
for tok in token_list.tokens:
if tok.ttype == tokens.Name:
col = self.schema.get_column(self.table.value, tok.value)
if col and not col.indexable:
raise NotIndexableError()
elif isinstance(tok, (sql.Comparison, sql.Identifier)):
check_indexable(tok)
check_indexable(self.where)
def to_sqlite(self):
"""Converts to a SQLite query.
Specifically:
- validates
- processes functions
- prefixes table names with underscores
"""
self.validate()
self.process_functions()
self.table.value = '`%s`' % self.table.value
return self.statement.to_unicode()
def process_functions(self, group=None):
"""Recursively parse and process FQL functions in the given group token.
TODO: switch to sqlite3.Connection.create_function().
Currently handles: me(), now()
"""
if group is None:
group = self.statement
for tok in group.tokens:
if isinstance(tok, sql.Function):
assert isinstance(tok.tokens[0], sql.Identifier)
name = tok.tokens[0].tokens[0]
if name.value not in Fql.FUNCTIONS:
raise InvalidFunctionError(name.value)
# check number of params
#
# i wish i could use tok.get_parameters() here, but it doesn't work
# with string parameters for some reason. :/
assert isinstance(tok.tokens[1], sql.Parenthesis)
params = [t for t in tok.tokens[1].flatten()
if t.ttype not in (tokens.Punctuation, tokens.Whitespace)]
actual_num = len(params)
expected_num = Fql.FUNCTIONS[name.value]
if actual_num != expected_num:
raise ParamMismatchError(name.value, expected_num, actual_num)
# handle each function
replacement = None
if name.value == 'me':
replacement = str(self.me)
elif name.value == 'now':
replacement = str(int(time.time()))
elif name.value == 'strlen':
# pass through to sqlite's length() function
name.value = 'length'
elif name.value == 'substr':
# the index param is 0-based in FQL but 1-based in sqlite
params[1].value = str(int(params[1].value) + 1)
elif name.value == 'strpos':
# strip quote chars
string = params[0].value[1:-1]
sub = params[1].value[1:-1]
replacement = str(string.find(sub))
else:
# shouldn't happen
assert False, 'unknown function: %s' % name.value
if replacement is not None:
tok.tokens = [sql.Token(tokens.Number, replacement)]
elif tok.is_group():
self.process_functions(tok)
class FqlHandler(webapp2.RequestHandler):
"""The FQL request handler.
Not thread safe!
Class attributes:
conn: sqlite3.Connection
me: integer, the user id that me() should return
schema: schemautil.FqlSchema
"""
XML_TEMPLATE = """\
<?xml version="1.0" encoding="UTF-8"?>
<fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
%s
</fql_query_response>"""
XML_ERROR_TEMPLATE = """\
<?xml version="1.0" encoding="UTF-8"?>
<error_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">
%s
</error_response>"""
ROUTES = [(r'/method/fql.query/?', 'fql.FqlHandler'),
('/fql', 'fql.FqlHandler'),
]
@classmethod
def init(cls, conn, me):
"""Args:
conn: sqlite3.Connection
me: integer, the user id that me() should return
"""
cls.conn = conn
cls.me = me
cls.schema = schemautil.FqlSchema.read()
def get(self):
table = ''
graph_endpoint = (self.request.path == '/fql')
try:
query_arg = 'q' if graph_endpoint else 'query'
query = self.request.get(query_arg)
if not query:
raise MissingParamError(query_arg)
token = self.request.get('access_token')
if token and not oauth.AccessTokenHandler.is_valid_token(self.conn, token):
raise InvalidAccessTokenError()
logging.debug('Received FQL query: %s' % query)
fql = Fql(self.schema, query, self.me)
# grab the table name before it gets munged
table = fql.table_name()
sqlite = fql.to_sqlite()
logging.debug('Running SQLite query: %s' % sqlite)
try:
cursor = self.conn.execute(sqlite)
except sqlite3.OperationalError, e:
logging.debug('SQLite error: %s', e)
raise SqliteError(unicode(e))
results = self.schema.sqlite_to_json(cursor, table)
except FqlError, e:
results = self.error(self.request.GET, e.code, e.msg)
if self.request.get('format') == 'json' or graph_endpoint:
json.dump(results, self.response.out, indent=2)
else:
self.response.out.write(self.render_xml(results, table))
self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
def render_xml(self, results, table):
"""Renders a query result into an XML string response.
Args:
results: dict mapping strings to strings or lists of (key, value) tuples
table: string table name
"""
if 'error_code' in results:
template = self.XML_ERROR_TEMPLATE
results['request_args'] = [{'arg': elem} for elem in results['request_args']]
else:
template = self.XML_TEMPLATE
results = [{table: row} for row in results]
return template % self.render_xml_part(results)
def render_xml_part(self, results):
"""Recursively renders part of a query result into an XML string response.
Args:
results: dict or list or primitive
"""
if isinstance(results, (list, tuple)):
return '\n'.join([self.render_xml_part(elem) for elem in results])
elif isinstance(results, dict):
elems = []
for key, val in results.iteritems():
list_attr = ' list="true"' if isinstance(val, list) else ''
br = '\n' if isinstance(val, (list, dict)) else ''
rendered = self.render_xml_part(val)
elems.append('<%(key)s%(list_attr)s>%(br)s%(rendered)s%(br)s</%(key)s>' %
locals())
return '\n'.join(elems)
else:
return unicode(results)
def error(self, args, code, msg):
"""Renders an error response.
Args:
args: dict, the parsed URL query string arguments
code: integer, the error_code
msg: string, the error_msg
Returns: the response string
"""
args['method'] = 'fql.query' # (always)
request_args = [{'key': key, 'value': val} for key, val in args.items()]
return {'error_code': code,
'error_msg': msg,
'request_args': request_args,
}