Permalink
Browse files

add in arecibo, remove simplejson, clean license

  • Loading branch information...
1 parent f763b47 commit 89c8d18203097c01d23e82f0f5061cc41c1792e7 @andymckay committed Jul 15, 2011
Showing with 118 additions and 55 deletions.
  1. +1 −51 LICENSE.txt
  2. +109 −0 django_arecibo/arecibo.py
  3. +8 −2 django_arecibo/middleware.py
  4. +0 −2 setup.py
View
@@ -1,51 +1 @@
-This library is under the BSD license, with the exception of simplejson from http://simplejson.googlecode.com/ its, license is reproduced below:
-
- Copyright 2008 ClearWind Consulting Ltd.
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without modification,
- are permitted provided that the following conditions are met:
-
- Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
-
- Neither the name of the ClearWind nor the names of its contributors may be
- used to endorse or promote products derived from this software without
- specific prior written permission
- .
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-simplejson license:
-
- Copyright (c) 2006 Bob Ippolito
-
- 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.
+BSD
View
@@ -0,0 +1,109 @@
+# -*- coding: utf-8 -*-
+from httplib import HTTPConnection
+has_https = False
+try:
+ from httplib import HTTPSConnection
+ has_https = True
+except ImportError:
+ pass
+
+from urllib import urlencode
+from urlparse import urlparse
+try:
+ from socket import gethostname, getdefaulttimeout, setdefaulttimeout
+except ImportError:
+ # App Engine doesn't have these
+ # so here are a some replacements
+ def gethostname(): return "unknown"
+ def getdefaulttimeout(): return 60
+ def setdefaulttimeout(num): pass
+
+from email.Utils import formatdate
+
+import smtplib
+import json
+
+keys = ["account", "ip", "priority", "uid",
+ "type", "msg", "traceback", "user_agent",
+ "url", "status", "server", "timestamp",
+ "request", "username"]
+
+default_route = "/v/1/"
+
+class post:
+ def __init__(self):
+ self._data = {}
+ self.transport = "http"
+ self.smtp_server = "localhost"
+ self.smtp_from = "noreply@clearwind.ca"
+ self.url = None
+ self.smtp_to = None
+ self.set("server", gethostname())
+ self.set("timestamp", formatdate())
+
+ # public
+ def set(self, key, value):
+ """ Sets the variable named key, with the value """
+ if key not in keys:
+ raise ValueError, "Unknown value: %s" % key
+ self._data[key] = value
+
+ def server(self, url=None, email=None):
+ """ Sets the URL or address so we know where to post the error """
+ if url: self.url = urlparse(url)
+ if email: self.smtp_to = email
+
+ def send(self):
+ """ Sends the data to the arecibo server """
+ self._send()
+
+ def as_json(self):
+ return json.dumps(self._data)
+
+ # private
+ def _data_encoded(self):
+ data = {}
+ for k in keys:
+ if self._data.get(k):
+ data[k] = self._data.get(k)
+ return urlencode(data)
+
+ def _send(self):
+ key = self.transport.lower()
+ assert key in ["http", "smtp", "https"]
+ if key in ["http", "https"]:
+ assert self.url, "No URL is set to post the error to."
+ self._send_http()
+ elif key == "smtp":
+ assert self.smtp_to, "No destination email is set to post the error to."
+ self._send_smtp()
+
+ def _msg_body(self):
+ msg = "From: %s\r\nTo: %s\r\n\r\n%s" % (self.smtp_from, self.smtp_to, self.as_json())
+ return msg
+
+ def _send_smtp(self):
+ msg = self._msg_body()
+ s = smtplib.SMTP(self.smtp_server)
+ s.sendmail(self.smtp_from, self.smtp_to, msg)
+ s.quit()
+
+ def _send_http(self):
+ if self.transport == "https" and has_https:
+ h = HTTPSConnection(self.url[1])
+ else:
+ h = HTTPConnection(self.url[1])
+ headers = {
+ "Content-type": 'application/x-www-form-urlencoded; charset="utf-8"',
+ "Accept": "text/plain"}
+ data = self._data_encoded()
+ oldtimeout = getdefaulttimeout()
+ try:
+ setdefaulttimeout(10)
+ h.request("POST", default_route, data, headers)
+
+ reply = h.getresponse()
+ if reply.status != 200:
+ raise ValueError, "%s (%s)" % (reply.read(), reply.status)
+ finally:
+ setdefaulttimeout(oldtimeout)
@@ -1,13 +1,19 @@
from django.http import Http404
from wrapper import post
+from tasks import post as delayed_post
class AreciboMiddleware(object):
+ post = post
+
def process_exception(self, request, exception):
""" This is middleware to process a request
and pass the value off to Arecibo. """
# we keep the 404 check in there case
if isinstance(exception, Http404):
- post(request, 404)
+ self.post(request, 404)
else:
# do we need to be finer grained on the exception status?
- post(request, 500)
+ self.post(request, 500)
+
+class AreciboMiddlewareCelery(AreciboMiddleware):
+ post = delayed_post
View
@@ -23,8 +23,6 @@
zip_safe=False,
install_requires=[
'setuptools',
- 'arecibo'
- # -*- Extra requirements: -*-
],
entry_points="""
# -*- Entry points: -*-

0 comments on commit 89c8d18

Please sign in to comment.