/
main.py
124 lines (102 loc) · 3.96 KB
/
main.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
#!/usr/bin/env python
#
# Copyright 2013 Nick Freear.
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import webapp2
import sys, os
PROJECT_DIR = os.path.dirname(__file__)
sys.path.append(PROJECT_DIR + '/lib')
import validator, accessifyquery
import json, re, time, datetime
def test_jsonp_callback(callback):
result = None
if callback and not re.match('^[\$a-zA-Z_][\w\._]+$', callback):
result = {
'stat': 'fail',
'code': 400.1,
'message': "Error, invalid 'callback' parameter."
}
return result
def make_http_time_string(dt = None, minutes = 0):
'''Input struct_time and output HTTP header-type time string'''
if not dt:
dt = datetime.datetime.utcnow()
dt = dt + datetime.timedelta(minutes = minutes)
return time.strftime('%a, %d %b %Y %H:%M:%S GMT',
dt.timetuple())
class MainHandler(webapp2.RequestHandler):
def get(self):
self.redirect('http://accessify.wikia.com/wiki/Tools')
class AccessifyQueryHandler(webapp2.RequestHandler):
def get(self):
req = self.request
rsp = self.response
format = req.get('format', default_value='json')
callback= req.get('callback', default_value=None)
min = req.get('min', default_value=None)
url = req.get('url', default_value=None)
if not url:
url = req.get('q', default_value=None)
lang = req.get('lang', default_value=None)
accept_lang = req.headers.get('Accept-Language')
rsp.headers['Content-Type'] = 'application/json; charset=utf-8'
rsp.headers['Access-Control-Allow-Origin'] = '*'
rsp.headers['Cache-Control'] = 'public, max-age=300'
rsp.headers['Expires'] = make_http_time_string(minutes = 5)
#rsp.headers['Last-Modified'] = make_http_time_string()
#Vary: Accept-Encoding
result = test_jsonp_callback(callback)
if result:
rsp.write(json.dumps(result))
return
result = accessifyquery.query(url, min, lang)
#if format == 'json':
if callback:
rsp.headers['Content-Type'] = 'text/javascript; charset=utf-8'
rsp.write(''.join([ callback, '(', json.dumps(result), ');' ]))
else:
rsp.write(json.dumps(result))
class ValidateHandler(webapp2.RequestHandler):
def get(self):
req = self.request
rsp = self.response
format = req.get('format', default_value='html') #Or 'json'
callback= req.get('callback', default_value=None)
schema_url = req.get('schema_url', default_value=None)
url = req.get('url', default_value=None)
yaml = req.get('yaml', default_value=None) # POST or GET?
result = None
if format == 'json':
if not url and not yaml:
result = {
"stat": "fail",
"code": 400,
"message": "Error, expecting either 'url' or 'yaml' parameter."
}
else:
result = validator.validate(url, yaml, schema_url)
rsp.headers["Content-Type"] = 'application/json; charset=utf-8'
rsp.write(json.dumps(result))
else:
rsp.write('Validate!')
rsp.write(format)
app = webapp2.WSGIApplication([
('/', MainHandler),
('/query', AccessifyQueryHandler), # Deprecated
('/fix', AccessifyQueryHandler),
('/validate', ValidateHandler)
], debug=True)