-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·340 lines (276 loc) · 9.54 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
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
#!/usr/bin/env python
#
# Copyright 2011 Oli McCormack. All Rights Reserved.
#
# 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.
"""Main Controller for Weather app."""
import os
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util
from google.appengine.api import memcache
from django.utils import simplejson
import models
import logging
import urllib
import re
import StringIO
import csv
import time
import random
import urllib2
import settings
import hashlib
from urlparse import urlparse
from google.appengine.api import urlfetch
class Error(Exception):
"""Base class for error"""
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class InvalidProtocolError(Error):
"""An invalid protocol is provided for a urlfetch call."""
pass
class ContentFetchError(Error):
"""Fetching content failed."""
pass
BASE_KEY_LEN = 15
SETTINGS = {"DEVELOPER": "oli (dev@olimcc.com)",
"VERSION": 1,
"APP_NAME": "WeatherDev"}
def Render(handler, tname="index.html", values=0):
"""A function to render data in templates.
All Request Handlers send data through this function to a template page.
Passes a dictionary to a template, which holds data that used
on the template page. Below, this dictionary is called 'set_values'. It
becomes populate it with some defaults: user, developer, etc as these
are things used on every page of a site (thus, there is no
need to perform the checks in every request handler).
This dictionary is updated by 'values', which are passed in when the function
is called.
Args:
handler: The current handler
tname: The template name (html file) that should be used with this handler
values: A dictionary of items that are used in this template
Returns:
True
"""
temp = os.path.join(os.path.dirname(__file__),
"templates"+"/"+tname)
if not os.path.isfile(temp):
return False
set_values = {"developer": SETTINGS["DEVELOPER"],
"version": SETTINGS["VERSION"],
"app_name": SETTINGS["APP_NAME"]}
if values:
set_values.update(values)
outstr = template.render(temp, set_values)
handler.response.out.write(outstr)
return True
def ValidateCallUrl(url, check_content=False):
"""Validate URL.
Args:
url: (string) the url at which files are stored.
check_content: (string) the filename of a specific file to test.
Raises:
InvalidProtocolError: if protocol not one of http/https.
ContentFetchError: If content not fetchable.
"""
url_res = urlparse(url)
if not url_res.scheme:
raise InvalidProtocolError('Protocol (http, https) must be included')
if check_content:
try:
urllib2.urlopen(url + check_content)
except urlfetch.DownloadError:
raise ContentFetchError('Error retrieving testing value from url: ' +
url + check_content)
return
class BaseHandler(webapp.RequestHandler):
"""Base."""
def GetSuccess(self, data):
"""Get a success response object.
Args:
data: (dictionary) package to return with response.
Returns:
(string) json formatted result object.
"""
return simplejson.dumps({
'status': 'success',
'data': data,
})
def GetError(self, error):
"""Get an error response object.
Args:
error: (string) string describing error to return with response.
Returns:
(string) json formatted result object.
"""
return simplejson.dumps({
'status': 'error',
'description': str(error),
})
def GetUrlParams(self, arguments=None):
"""Obtain URL parameters sent with request."""
vals = {}
arguments = arguments if arguments else self.request.arguments()
for argument in arguments:
vals[argument] = self.request.get(argument, None)
return vals
def __init__(self):
pass
class AdminHandler(BaseHandler):
"""Handler for admin functionality."""
def GetKey(self, path):
"""Gets a key representing the site in question.
The key will be of minimum length: BASE_KEY_LEN
Args:
path: (string) the url for which a key should be generated.
Returns:
key (string).
"""
n = BASE_KEY_LEN
while True:
key = hashlib.sha224(path).hexdigest()[:n]
if not models.Site.get_by_key_name(key):
return key
n += 1
def get(self):
"""GET handler."""
sites = models.User.current().site_set
Render(self, '/admin.html', {'sites': sites})
def post(self):
"""POST handler."""
path = self.request.get('urlpath')
try:
ValidateCallUrl(path, 'clientraw.txt')
except Error, err:
self.response.out.write(self.GetError(err))
return
key = self.GetKey(path)
models.Site(
key_name = key,
path = path,
owner = models.User.current()).put()
self.redirect('/admin')
class ApiHandler(BaseHandler):
"""Handler for API calls.
Args:
path: (string) url path at which files are stored.
file_name: (string) file name to fetch.
mc_time: (int) duration to store results in memcache (unused).
Returns:
(string) file content.
"""
def GetData(self, path, file_name, mc_time):
try:
file_path = path + file_name + "?" + str(time.time())
resource = urllib2.urlopen(file_path)
return resource.readlines()[0].split()
except IndexError, e:
self.response.out.write(
self.GetError('unable to fetch data'))
return
def GetResponse(self):
"""Get an API response dict.
Returns:
(dictionary) response dict.
"""
return {
'current': {},
'lasthour': {},
'lastday': {},
'lastmonth': {},
'core': {}}
def get(self):
"""GET handler."""
#print 'hi'
#print self.request.remote_addr
#requester = self.request.headers['Referer']
mandatory_params = ['id', 'callback']
params = self.GetUrlParams(mandatory_params)
if None in params.values():
self.response.out.write(
self.GetError('id and callback parameter must be provided'))
return
site = models.Site.get_by_key_name(params['id'])
if site is None:
self.response.out.write(
self.GetError('site does not exist'))
return
response = self.GetResponse()
# fetch all current metrics from clientraw.txt
# reflects the moment recent snapshot of data
# settings.cr_fields describes what will be returned here
clientraw = self.GetData(site.path, 'clientraw.txt', 5)
for i, config in enumerate(settings.cr_fields):
response['current'][config[0]] = {
'value': clientraw[i],
'unit': config[1],
}
# select a set of core current metrics to return, from clientraw.txt
# settings.core describes what will be included here
for config in settings.core:
response['core'][config[0]] = response['current'][config[1]]
# fetch data for the last hour from clientrawhour.txt
# returned data is per minute, for the past 60 mins
# settings.hourly_fields describes what will be included here
clientrawhour = self.GetData(site.path, 'clientrawhour.txt', 3600)
for config in settings.hourly_fields:
response['lasthour'][config[0]] = {
'value': clientrawhour[config[1]:config[1]+config[2]],
'unit': settings.units[config[0]],
}
# fetch data for the last hour from clientrawextra.txt
# returned data is per hour, for the past 24 hours
# settings.daily_fields describes what will be included here
clientrawextra = self.GetData(site.path, 'clientrawextra.txt', 3600*20)
for config in settings.daily_fields:
ind = range(config[1][0], config[1][0]+config[1][1]) + range(config[2][0], config[2][0]+config[2][1])
response['lastday'][config[0]] = {
'value': [clientrawextra[i] for i in ind],
'unit': settings.units[config[0]]
}
# fetch data for the last hour from clientrawdaily.txt
# returned data is per day, for the past 31 hours
# settings.monthly_fields describes what will be included here
clientrawdaily = self.GetData(site.path, 'clientrawdaily.txt', 3600*24)
for config in settings.monthly_fields:
response['lastmonth'][config[0]] = {
'value': clientrawdaily[config[1]:config[1]+config[2]],
'unit': settings.units[config[0]],
}
self.response.out.write(
params['callback'] + "(" + self.GetSuccess(response) + ")")
def real_main():
application = webapp.WSGIApplication([("/admin", AdminHandler),
("/api", ApiHandler),],
debug=True)
util.run_wsgi_app(application)
def profile_main():
# This is the for profiling page run time
# We've renamed our original main() above to real_main()
import cProfile
import pstats
prof = cProfile.Profile()
prof = prof.runctx("real_main()", globals(), locals())
print "<pre>"
stats = pstats.Stats(prof)
stats.sort_stats("cumulative")
stats.print_stats("main")
print "</pre>"
main = real_main
if __name__ == "__main__":
main()