Skip to content
Browse files

Updated with Flask 0.8 structure and 0.9 _app_ctx_stack

  • Loading branch information...
1 parent 2270a8e commit eef0f242a3119f470598b207a7897128a5e9933d @jzempel committed Apr 9, 2012
Showing with 42 additions and 32 deletions.
  1. +6 −0 CHANGES
  2. +2 −2 docs/conf.py
  3. +2 −2 docs/index.rst
  4. +28 −23 flaskext/exceptional.py → flask_exceptional.py
  5. +0 −1 flaskext/__init__.py
  6. +2 −3 setup.py
  7. +2 −1 tests.py
View
6 CHANGES
@@ -1,6 +1,12 @@
Changelog
---------
+Version 0.5
+~~~~~~~~~~~
+
+* Updated with Flask 0.8 extension structure recommendations and 0.9
+ _app_ctx_stack.
+
Version 0.4.9
^^^^^^^^^^^^^
View
4 docs/conf.py
@@ -41,7 +41,7 @@
# General information about the project.
project = u'Flask-Exceptional'
-copyright = u'2011, Jonathan Zempel'
+copyright = u'2012, Jonathan Zempel'
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
@@ -225,4 +225,4 @@
man_pages = [
('index', 'flask-exceptional', u'Flask-Exceptional Documentation',
[u'Jonathan Zempel'], 1)
-]
+]
View
4 docs/index.rst
@@ -1,7 +1,7 @@
Flask-Exceptional
=================
-.. module:: flaskext.exceptional
+.. module:: flask.ext.exceptional
Flask-Exceptional adds `Exceptional`_ support to `Flask`_. Exceptional
tracks errors in your application, reports them real-time, and gathers
@@ -28,7 +28,7 @@ application, configure the Exceptional API key, and create the
:class:`Exceptional` object. It's this easy::
from flask import Flask
- from flaskext.exceptional import Exceptional
+ from flask.ext.exceptional import Exceptional
app = Flask(__name__)
app.config["EXCEPTIONAL_API_KEY"] = "exceptional_forty_character_unique_key"
View
51 flaskext/exceptional.py → flask_exceptional.py
@@ -1,18 +1,18 @@
# -*- coding: utf-8 -*-
"""
- flaskext.exceptional
- ~~~~~~~~~~~~~~~~~~~~
+ flask_exceptional
+ ~~~~~~~~~~~~~~~~~
Adds Exceptional support to Flask.
- :copyright: (c) 2011 by Jonathan Zempel.
+ :copyright: (c) 2012 by Jonathan Zempel.
:license: BSD, see LICENSE for more details.
"""
from __future__ import with_statement
from Cookie import SimpleCookie
from datetime import datetime
-from flask import _request_ctx_stack, Config, Flask, g, json
+from flask import Config, Flask, g, json
from functools import wraps
from re import match
from urllib2 import HTTPError, Request, urlopen, URLError
@@ -22,6 +22,11 @@
import os
import sys
+try:
+ from flask import _app_ctx_stack as stack
+except ImportError:
+ from flask import _request_ctx_stack as stack
+
EXCEPTIONAL_URL = "http://api.exceptional.io/api/errors"
@@ -40,8 +45,6 @@ def __init__(self, app=None):
"""
if app is not None:
self.init_app(app)
- else:
- self.app = None
@property
def __version__(self):
@@ -59,8 +62,6 @@ def init_app(self, app):
:param app: The Flask application to track errors for.
"""
- self.app = app
-
if "EXCEPTIONAL_API_KEY" in app.config:
app.config.setdefault("EXCEPTIONAL_COOKIE_FILTER", None)
app.config.setdefault("EXCEPTIONAL_ENVIRONMENT_FILTER",
@@ -90,8 +91,8 @@ def init_app(self, app):
if "exceptional" in app.extensions:
app.logger.warning("Repeated Exceptional initialization attempt.")
else:
- app.handle_exception = self._get_exception_handler()
- app.handle_http_exception = self._get_http_exception_handler()
+ app.handle_exception = self._get_exception_handler(app)
+ app.handle_http_exception = self._get_http_exception_handler(app)
app.extensions["exceptional"] = self
else:
app.logger.warning("Missing 'EXCEPTIONAL_API_KEY' configuration.")
@@ -105,11 +106,11 @@ def context(data=None, **kwargs):
:param data: Default ``None``. A dictionary of context data.
:param kwargs: A series of keyword arguments to use as context data.
"""
- context = getattr(_request_ctx_stack.top, "exceptional_context", None)
+ context = getattr(stack.top, "exceptional_context", None)
if context is None:
context = {}
- setattr(_request_ctx_stack.top, "exceptional_context", context)
+ setattr(stack.top, "exceptional_context", context)
if data is not None:
context.update(data)
@@ -151,7 +152,7 @@ def test(config):
Accepts either :class:`flask.Config` or the object
types allowed by :meth:`flask.Config.from_object`.
"""
- context = getattr(_request_ctx_stack.top, "exceptional_context", None)
+ context = getattr(stack.top, "exceptional_context", None)
app = Flask(__name__)
exceptional = Exceptional()
@@ -168,7 +169,7 @@ def test(config):
@app.route("/exception")
def exception():
- setattr(_request_ctx_stack.top, "exceptional_context", context)
+ setattr(stack.top, "exceptional_context", context)
message = "Congratulations! Your application is configured for Exceptional error tracking."
raise Exception(message)
@@ -177,34 +178,38 @@ def exception():
client.get("/exception")
json.loads(g.exceptional)
- def _get_exception_handler(self):
+ def _get_exception_handler(self, app):
"""Get a wrapped exception handler. Returns a handler that can be
used to override Flask's ``app.handle_exception``. The wrapped
handler posts error data to Exceptional and then passes the exception
to the wrapped method.
+
+ :param app: The app for which the exception handler is being wrapped.
"""
- handle_exception = self.app.handle_exception
+ handle_exception = app.handle_exception
@wraps(handle_exception)
def ret_val(exception):
- context = _request_ctx_stack.top
- self._post_data(context)
+ self._post_data(stack.top)
return handle_exception(exception)
return ret_val
- def _get_http_exception_handler(self):
+ def _get_http_exception_handler(self, app):
"""Get a wrapped HTTP exception handler. Returns a handler that can
be used to override Flask's ``app.handle_http_exception``. The wrapped
handler posts HTTP error (i.e. '400: Bad Request') data to Exceptional
and then passes the exception to the wrapped method.
+
+ :param app: The app for which the HTTP exception handler is being
+ wrapped.
"""
- handle_http_exception = self.app.handle_http_exception
+ handle_http_exception = app.handle_http_exception
@wraps(handle_http_exception)
def ret_val(exception):
- context = _request_ctx_stack.top
+ context = stack.top
if exception.code in context.app.config["EXCEPTIONAL_HTTP_CODES"]:
self._post_data(context)
@@ -222,7 +227,7 @@ def _post_data(self, context=None, traceback=None):
:param context: Default ``None``. The current application context.
:param traceback: Default ``None``. The exception stack trace.
"""
- app = context.app if context else self.app
+ app = context.app if context else stack.top.app
application_data = self.__get_application_data(app)
client_data = {
"name": "flask-exceptional",
@@ -286,7 +291,7 @@ def _encode_basestring(value):
except URLError:
message = "Unable to connect to %s. See \
http://status.exceptional.io for details. Error data:\n%s" % (self.url, error_data)
- self.app.logger.warning(message, exc_info=True)
+ stack.top.app.logger.warning(message, exc_info=True)
@staticmethod
def __filter(app, data, filter_name):
View
1 flaskext/__init__.py
@@ -1 +0,0 @@
-__import__('pkg_resources').declare_namespace(__name__)
View
5 setup.py
@@ -25,15 +25,14 @@
setup(
name='Flask-Exceptional',
- version='0.4.9',
+ version='0.5',
url='http://github.com/jzempel/flask-exceptional',
license='BSD',
author='Jonathan Zempel',
author_email='jzempel@gmail.com',
description='Adds Exceptional support to Flask applications',
long_description=__doc__,
- packages=['flaskext'],
- namespace_packages=['flaskext'],
+ py_modules=['flask_exceptional'],
zip_safe=False,
platforms='any',
install_requires=install_requires,
View
3 tests.py
@@ -12,14 +12,15 @@
from __future__ import with_statement
from contextlib import closing
from flask import abort, Flask, g, json
-from flaskext.exceptional import Exceptional
+from flask.ext.exceptional import Exceptional
from functools import wraps
from os import environ
from sys import exc_info
from urllib2 import urlopen
from werkzeug.debug.tbtools import Traceback
import unittest
+
class ExceptionalTestCase(unittest.TestCase):
"""Exceptional extension test cases.
"""

0 comments on commit eef0f24

Please sign in to comment.
Something went wrong with that request. Please try again.