summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--AUTHORS1
-rw-r--r--README.rst4
-rw-r--r--docs/source/changes.rst15
-rw-r--r--docs/source/configuration.rst18
-rw-r--r--docs/source/index.rst4
-rw-r--r--docs/source/simple_ajax.rst286
-rw-r--r--docs/source/simple_forms_processing.rst195
-rw-r--r--openstack-common.conf7
-rw-r--r--pecan/commands/serve.py45
-rw-r--r--pecan/core.py18
-rw-r--r--pecan/jsonify.py6
-rw-r--r--pecan/log.py54
-rw-r--r--pecan/middleware/errordocument.py2
-rw-r--r--pecan/middleware/recursive.py2
-rw-r--r--pecan/scaffolds/base/+package+/templates/index.html2
-rw-r--r--pecan/scaffolds/base/config.py_tmpl9
-rw-r--r--pecan/scaffolds/base/public/images/logo.pngbin35094 -> 20596 bytes
-rw-r--r--pecan/scaffolds/rest-api/config.py_tmpl9
-rw-r--r--pecan/testing.py4
-rw-r--r--pecan/tests/test_base.py4
-rw-r--r--requirements.txt1
-rw-r--r--setup.py11
-rwxr-xr-xtools/run_cross_tests.sh70
23 files changed, 729 insertions, 38 deletions
diff --git a/AUTHORS b/AUTHORS
index 27abd07..3e8f667 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -13,3 +13,4 @@ Mike Perez
Justin Barber
Wesley Spikes
Steven Berler
+Chad Lung
diff --git a/README.rst b/README.rst
index 758815d..60a38ea 100644
--- a/README.rst
+++ b/README.rst
@@ -4,8 +4,8 @@ Pecan
A WSGI object-dispatching web framework, designed to be lean and fast with few
dependencies.
-.. image:: https://pypip.in/v/pecan/badge.png
- :target: https://crate.io/packages/pecan/
+.. image:: https://badge.fury.io/py/pecan.png
+ :target: https://pypi.python.org/pypi/pecan/
:alt: Latest PyPI version
Installing
diff --git a/docs/source/changes.rst b/docs/source/changes.rst
index 59fc150..75f22ec 100644
--- a/docs/source/changes.rst
+++ b/docs/source/changes.rst
@@ -1,9 +1,22 @@
+0.5.0
+=====
+* This release adds formal support for pypy.
+* Added colored request logging to the `pecan serve` command.
+* Added a scaffold for easily generating a basic REST API.
+* Added the ability to pass arbitrary keyword arguments to
+ `pecan.testing.load_test_app`.
+* Fixed a recursion-related bug in the error document middleware.
+* Fixed a bug in the `gunicorn_pecan` command that caused `threading.local`
+ data to leak between eventlet/gevent green threads.
+* Improved documentation through fixes and narrative tutorials for sample pecan
+ applications.
+
0.4.5
=====
* Fixed a trailing slash bug for `RestController`s that have a `_lookup` method.
* Cleaned up the WSGI app reference from the threadlocal state on every request
(to avoid potential memory leaks, especially when testing).
-* Improved pecan documentation and correctd intersphinx references.
+* Improved pecan documentation and corrected intersphinx references.
* pecan supports Python 3.4.
0.4.4
diff --git a/docs/source/configuration.rst b/docs/source/configuration.rst
index 1118c72..d1f9f83 100644
--- a/docs/source/configuration.rst
+++ b/docs/source/configuration.rst
@@ -182,13 +182,14 @@ string argument if you need to prefix the keys in the returned dictionary.
{'prefixed_app': {'prefixed_errors': {}, 'prefixed_template_path': '', 'prefixed_static_root': 'prefixed_public', [...]
-Dotted Keys and Native Dictionaries
------------------------------------
+Dotted Keys, Non-Python Idenfitiers, and Native Dictionaries
+------------------------------------------------------------
-Sometimes you want to specify a configuration option that includes dotted keys.
-This is especially common when configuring Python logging. By passing
-a special key, ``__force_dict__``, individual configuration blocks can be
-treated as native dictionaries.
+Sometimes you want to specify a configuration option that includes dotted keys
+or is not a valid Python idenfitier, such as ``()``. These situations are
+especially common when configuring Python logging. By passing a special key,
+``__force_dict__``, individual configuration blocks can be treated as native
+dictionaries.
::
@@ -197,6 +198,11 @@ treated as native dictionaries.
'root': {'level': 'INFO', 'handlers': ['console']},
'sqlalchemy.engine': {'level': 'INFO', 'handlers': ['console']},
'__force_dict__': True
+ },
+ 'formatters': {
+ 'custom': {
+ '()': 'my.package.customFormatter'
+ }
}
}
diff --git a/docs/source/index.rst b/docs/source/index.rst
index 614baee..8a4ef37 100644
--- a/docs/source/index.rst
+++ b/docs/source/index.rst
@@ -54,7 +54,9 @@ Cookbook and Common Patterns
sessions.rst
databases.rst
errors.rst
-
+ simple_forms_processing.rst
+ simple_ajax.rst
+
API Documentation
=================
diff --git a/docs/source/simple_ajax.rst b/docs/source/simple_ajax.rst
new file mode 100644
index 0000000..fcebb3a
--- /dev/null
+++ b/docs/source/simple_ajax.rst
@@ -0,0 +1,286 @@
+.. _simple_ajax:
+
+Example Application: Simple AJAX
+================================
+
+This guide will walk you through building a simple Pecan web application that uses AJAX to fetch JSON data from a server.
+
+Project Setup
+-------------
+
+First, you'll need to install Pecan:
+
+::
+
+$ pip install pecan
+
+Use Pecan's basic template support to start a new project:
+
+::
+
+$ pecan create myajax
+$ cd myajax
+
+Install the new project in development mode:
+
+::
+
+$ python setup.py develop
+
+Adding JavaScript AJAX Support
+------------------------------
+
+For this project we will need to add `jQuery <http://jquery.com/>`_ support. To add jQuery go into the ``templates`` folder and edit the ``layout.html`` file.
+
+Adding jQuery support is easy, we actually only need one line of code:
+
+.. code-block:: html
+
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
+
+The JavaScript to make the AJAX call is a little more in depth but shouldn't be unfamiliar if you've ever worked with jQuery before.
+
+The ``layout.html`` file will look like this:
+
+.. code-block:: html
+
+ <html>
+ <head>
+ <title>${self.title()}</title>
+ ${self.style()}
+ ${self.javascript()}
+ </head>
+ <body>
+ ${self.body()}
+ </body>
+ </html>
+
+ <%def name="title()">
+ Default Title
+ </%def>
+
+ <%def name="style()">
+ <link rel="stylesheet" type="text/css" media="screen" href="/css/style.css" />
+ </%def>
+
+ <%def name="javascript()">
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
+ <script language="text/javascript" src="/javascript/shared.js"></script>
+
+ <script>
+ function onSuccess(data, status, jqXHR) {
+ // Use a template or something here instead
+ // Just for demo purposes
+ $("#result").html("<div>" +
+ "<p></p><strong>Project Name: " + data.name + "</strong></p>" +
+ "<p>Project License: " + data.licensing + "</p>" +
+ "<p><a href='" + data.repository + "'>Project Repository: " + data.repository + "</a></p>" +
+ "<p><a href='" + data.documentation + "'>Project Documentation: " + data.documentation + "</a></p>" +
+ "</div>");
+ }
+
+ function onError(jqXHR, textStatus, errorThrown) {
+ alert('HTTP Status Code: ' + jqXHR.status + ', ' + errorThrown);
+ }
+
+ $(document).ready(function () {
+ $("#submit").click(function () {
+ $.ajax({
+ url: "/projects/",
+ data: "id=" + $("#projects").val(),
+ contentType: 'application/json',
+ dataType: 'json',
+ success: onSuccess,
+ error: onError
+ });
+
+ return false;
+ });
+ });
+ </script>
+ </%def>
+
+**What did we just do?**
+
+#. In the ``head`` section we added jQuery support via the `Google CDN <https://developers.google.com/speed/libraries/devguide>`_
+#. Added JavaScript to make an AJAX call to the server via an HTTP ``GET`` passing in the ``id`` of the project to fetch more information on
+#. Once the ``onSuccess`` event is triggered by the returning data we take that and display it on the web page below the controls
+
+Adding Additional HTML
+----------------------
+
+Let's edit the ``index.html`` file next. We will add HTML to support the AJAX interaction between the web page and Pecan. Modify ``index.html`` to look like this:
+
+.. code-block:: html
+
+ <%inherit file="layout.html" />
+
+ <%def name="title()">
+ Welcome to Pecan!
+ </%def>
+
+ <header>
+ <h1><img src="/images/logo.png"/></h1>
+ </header>
+
+ <div id="content">
+ <p>Select a project to get details:</p>
+ <select id="projects">
+ <option value="0">OpenStack</option>
+ <option value="1">Pecan</option>
+ <option value="2">Stevedore</option>
+ </select>
+ <button id="submit" type="submit">Submit</button>
+
+ <div id="result"></div>
+
+ </div>
+
+**What did we just do?**
+
+#. Added a dropdown control and submit button for the user to interact with. Users can pick an open source project and get more details on it
+
+Building the Model with JSON Support
+------------------------------------
+
+The HTML and JavaScript work is now taken care of. At this point we can add a model to our project inside of the ``model`` folder. Create a file in there called ``projects.py`` and add the following to it:
+
+.. code-block:: python
+
+ class Project(object):
+ def __init__(self, name, licensing, repository, documentation):
+ self.name = name
+ self.licensing = licensing
+ self.repository = repository
+ self.documentation = documentation
+
+ def __json__(self):
+ return dict(
+ name=self.name,
+ licensing=self.licensing,
+ repository=self.repository,
+ documentation=self.documentation
+ )
+
+**What did we just do?**
+
+#. Created a model called ``Project`` that can hold project specific data
+#. Added a ``__json__`` method so an instance of the ``Project class`` can be easily represented as JSON. The controller we will soon build will make use of that JSON capability
+
+.. note::
+
+ There are other ways to return JSON with Pecan, check out :ref:`jsonify` for more information.
+
+Working with the Controllers
+----------------------------
+
+We don't need to do anything major to the ``root.py`` file in the ``controllers`` folder except to add support for a new controller we will call ``ProjectsController``. Modify the ``root.py`` like this:
+
+.. code-block:: python
+
+ from pecan import expose
+
+ from myajax.controllers.projects import ProjectsController
+
+
+ class RootController(object):
+
+ projects = ProjectsController()
+
+ @expose(generic=True, template='index.html')
+ def index(self):
+ return dict()
+
+**What did we just do?**
+
+#. Removed some of the initial boilerplate code since we won't be using it
+#. Add support for the upcoming ``ProjectsController``
+
+The final piece is to add a file called ``projects.py`` to the ``controllers`` folder. This new file will host the ``ProjectsController`` which will listen for incoming AJAX ``GET`` calls (in our case) and return the appropriate JSON response.
+
+Add the following code to the ``projects.py`` file:
+
+.. code-block:: python
+
+ from pecan import expose, response
+ from pecan.rest import RestController
+
+ from myajax.model.projects import Project
+
+
+ class ProjectsController(RestController):
+
+ # Note: You would probably store this information in a database
+ # This is just for simplicity and demonstration purposes
+ def __init__(self):
+ self.projects = [
+ Project(name='OpenStack',
+ licensing='Apache 2',
+ repository='http://github.com/openstack',
+ documentation='http://docs.openstack.org'),
+ Project(name='Pecan',
+ licensing='BSD',
+ repository='http://github.com/stackforge/pecan',
+ documentation='http://pecan.readthedocs.org'),
+ Project(name='stevedore',
+ licensing='Apache 2',
+ repository='http://github.com/dreamhost/pecan',
+ documentation='http://stevedore.readthedocs.org')
+ ]
+
+
+ @expose('json', content_type='application/json')
+ def get(self, id):
+ response.status = 200
+ return self.projects[int(id)]
+
+**What did we just do?**
+
+#. Created a local class variable called ``projects`` that holds three open source projects and their details. Typically this kind of information would probably reside in a database
+#. Added code for the new controller that will listen on the ``projects`` endpoint and serve back JSON based on the ``id`` passed in from the web page
+
+Run the application:
+
+::
+
+$ pecan serve config.py
+
+Open a web browser: `http://127.0.0.1:8080/ <http://127.0.0.1:8080/>`_
+
+There is something else we could add. What if an ``id`` is passed that is not found? A proper ``HTTP 404`` should be sent back. For this we will modify the ``ProjectsController``.
+
+Change the ``get`` function to look like this:
+
+.. code-block:: python
+
+ @expose('json', content_type='application/json')
+ def get(self, id):
+ try:
+ response.status = 200
+ return self.projects[int(id)]
+ except (IndexError, ValueError) as ex:
+ abort(404)
+
+To test this out we need to pass an invalid ``id`` to the ``ProjectsController``. This can be done by going into the ``index.html`` and adding an additional ``option`` tag with an ``id`` value that is outside of 0-2.
+
+.. code-block:: html
+
+ <p>Select a project to get details:</p>
+ <select id="projects">
+ <option value="0">OpenStack</option>
+ <option value="1">Pecan</option>
+ <option value="2">Stevedore</option>
+ <option value="3">WSME</option>
+ </select>
+
+You can see that we added ``WSME`` to the list and the value is 3.
+
+Run the application:
+
+::
+
+$ pecan serve config.py
+
+Open a web browser: `http://127.0.0.1:8080/ <http://127.0.0.1:8080/>`_
+
+Select ``WSME`` from the list. You should see the error dialog box triggered.
diff --git a/docs/source/simple_forms_processing.rst b/docs/source/simple_forms_processing.rst
new file mode 100644
index 0000000..d7b73de
--- /dev/null
+++ b/docs/source/simple_forms_processing.rst
@@ -0,0 +1,195 @@
+.. _simple_forms_processing:
+
+Example Application: Simple Forms Processing
+============================================
+
+This guide will walk you through building a simple Pecan web application that will do some simple forms processing.
+
+Project Setup
+-------------
+
+First, you'll need to install Pecan:
+
+::
+
+$ pip install pecan
+
+Use Pecan's basic template support to start a new project:
+
+::
+
+$ pecan create mywebsite
+$ cd mywebsite
+
+Install the new project in development mode:
+
+::
+
+$ python setup.py develop
+
+With the project ready, go into the ``templates`` folder and edit the ``index.html`` file. Modify it so that it resembles this:
+
+.. code-block:: html
+
+ <%inherit file="layout.html" />
+
+ <%def name="title()">
+ Welcome to Pecan!
+ </%def>
+ <header>
+ <h1><img src="/images/logo.png" /></h1>
+ </header>
+ <div id="content">
+ <form method="POST" action="/">
+ <fieldset>
+ <p>Enter a message: <input name="message" /></p>
+ <p>Enter your first name: <input name="first_name" /></p>
+ <input type="submit" value="Submit" />
+ </fieldset>
+ </form>
+ % if not form_post_data is UNDEFINED:
+ <p>${form_post_data['first_name']}, your message is: ${form_post_data['message']}</p>
+ % endif
+ </div>
+
+**What did we just do?**
+
+#. Modified the contents of the ``form`` tag to have two ``input`` tags. The first is named ``message`` and the second is named ``first_name``
+#. Added a check if ``form_post_data`` has not been defined so we don't show the message or wording
+#. Added code to display the message from the user's ``POST`` action
+
+Go into the ``controllers`` folder now and edit the ``root.py`` file. There will be two functions inside of the ``RootController`` class which will display the ``index.html`` file when your web browser hits the ``'/'`` endpoint. If the user puts some data into the textbox and hits the submit button then they will see the personalized message displayed back at them.
+
+Modify the ``root.py`` to look like this:
+
+.. code-block:: python
+
+ from pecan import expose
+
+
+ class RootController(object):
+
+ @expose(generic=True, template='index.html')
+ def index(self):
+ return dict()
+
+ @index.when(method='POST', template='index.html')
+ def index_post(self, **kwargs):
+ return dict(form_post_data=kwargs)
+
+**What did we just do?**
+
+#. Modified the ``index`` function to render the initial ``index.html`` webpage
+#. Modified the ``index_post`` function to return the posted data via keyword arguments
+
+Run the application:
+
+::
+
+$ pecan serve config.py
+
+Open a web browser: `http://127.0.0.1:8080/ <http://127.0.0.1:8080/>`_
+
+Adding Validation
+-----------------
+
+Enter a message into the textbox along with a name in the second textbox and press the submit button. You should see a personalized message displayed below the form once the page posts back.
+
+One problem you might have noticed is if you don't enter a message or a first name then you simply see no value entered for that part of the message. Let's add a little validation to make sure a message and a first name was actually entered. For this, we will use `WTForms <http://wtforms.simplecodes.com/>`_ but you can substitute anything else for your projects.
+
+Add support for the `WTForms <http://wtforms.simplecodes.com/>`_ library:
+
+::
+
+$ pip install wtforms
+
+.. note::
+
+ Keep in mind that Pecan is not opinionated when it comes to a particular library when working with form generation, validation, etc. Choose which libraries you prefer and integrate those with Pecan. This is one way of doing this, there are many more ways so feel free to handle this however you want in your own projects.
+
+Go back to the ``root.py`` files and modify it like this:
+
+.. code-block:: python
+
+ from pecan import expose, request
+ from wtforms import Form, TextField, validators
+
+
+ class PersonalizedMessageForm(Form):
+ message = TextField(u'Enter a message',
+ validators=[validators.required()])
+ first_name = TextField(u'Enter your first name',
+ validators=[validators.required()])
+
+
+ class RootController(object):
+
+ @expose(generic=True, template='index.html')
+ def index(self):
+ return dict(form=PersonalizedMessageForm())
+
+ @index.when(method='POST', template='index.html')
+ def index_post(self):
+ form = PersonalizedMessageForm(request.POST)
+ if form.validate():
+ return dict(message=form.message.data,
+ first_name=form.first_name.data)
+ else:
+ return dict(form=form)
+
+**What did we just do?**
+
+#. Added the ``PersonalizedMessageForm`` with two textfields and a required field validator for each
+#. Modified the ``index`` function to create a new instance of the ``PersonalizedMessageForm`` class and return it
+#. In the ``index_post`` function modify it to gather the posted data and validate it. If its valid, then set the returned data to be displayed on the webpage. If not valid, send the form which will contain the data plus the error message(s)
+
+Modify the ``index.html`` like this:
+
+.. code-block:: html
+
+ <%inherit file="layout.html" />
+
+ ## provide definitions for blocks we want to redefine
+ <%def name="title()">
+ Welcome to Pecan!
+ </%def>
+ <header>
+ <h1><img src="/images/logo.png" /></h1>
+ </header>
+ <div id="content">
+ % if not form:
+ <p>${first_name}, your message is: ${message}</p>
+ % else:
+ <form method="POST" action="/">
+ <div>
+ ${form.message.label}:
+ ${form.message}
+ % if form.message.errors:
+ <strong>${form.message.errors[0]}</strong>
+ % endif
+ </div>
+ <div>
+ ${form.first_name.label}:
+ ${form.first_name}
+ % if form.first_name.errors:
+ <strong>${form.first_name.errors[0]}</strong>
+ % endif
+ </div>
+ <input type="submit" value="Submit">
+ </form>
+ % endif
+ </div>
+
+.. note::
+
+ Keep in mind when using the `WTForms <http://wtforms.simplecodes.com/>`_ library you can customize the error messages and more. Also, you have multiple validation rules so make sure to catch all the errors which will mean you need a loop rather than the simple example above which grabs the first error item in the list. See the `documentation <http://wtforms.simplecodes.com/>`_ for more information.
+
+Run the application:
+
+::
+
+$ pecan serve config.py
+
+Open a web browser: `http://127.0.0.1:8080/ <http://127.0.0.1:8080/>`_
+
+Try the form with valid data and with no data entered.
diff --git a/openstack-common.conf b/openstack-common.conf
new file mode 100644
index 0000000..3fffac5
--- /dev/null
+++ b/openstack-common.conf
@@ -0,0 +1,7 @@
+[DEFAULT]
+
+# The list of modules to copy from oslo-incubator.git
+script = tools/run_cross_tests.sh
+
+# The base module to hold the copy of openstack.common
+base=oslotest
diff --git a/pecan/commands/serve.py b/pecan/commands/serve.py
index 0d1b14a..72a536d 100644
--- a/pecan/commands/serve.py
+++ b/pecan/commands/serve.py
@@ -2,12 +2,19 @@
Serve command for Pecan.
"""
from __future__ import print_function
+import logging
import os
import sys
import time
import subprocess
+from wsgiref.simple_server import WSGIRequestHandler
+
from pecan.commands import BaseCommand
+from pecan import util
+
+
+logger = logging.getLogger(__name__)
class ServeCommand(BaseCommand):
@@ -100,7 +107,12 @@ class ServeCommand(BaseCommand):
from wsgiref.simple_server import make_server
host, port = conf.server.host, int(conf.server.port)
- srv = make_server(host, port, app)
+ srv = make_server(
+ host,
+ port,
+ app,
+ handler_class=PecanWSGIRequestHandler,
+ )
print('Starting server in PID %s' % os.getpid())
@@ -178,3 +190,34 @@ def gunicorn_run():
return deploy(self.cfgfname)
PecanApplication("%(prog)s [OPTIONS] config.py").run()
+
+
+class PecanWSGIRequestHandler(WSGIRequestHandler, object):
+ """
+ A wsgiref request handler class that allows actual log output depending on
+ the application configuration.
+ """
+
+ def __init__(self, *args, **kwargs):
+ # We set self.path to avoid crashes in log_message() on unsupported
+ # requests (like "OPTIONS").
+ self.path = ''
+ super(PecanWSGIRequestHandler, self).__init__(*args, **kwargs)
+
+ def log_message(self, format, *args):
+ """
+ overrides the ``log_message`` method from the wsgiref server so that
+ normal logging works with whatever configuration the application has
+ been set to.
+
+ Levels are inferred from the HTTP status code, 4XX codes are treated as
+ warnings, 5XX as errors and everything else as INFO level.
+ """
+ code = args[1][0]
+ levels = {
+ '4': 'warning',
+ '5': 'error'
+ }
+
+ log_handler = getattr(logger, levels.get(code, 'info'))
+ log_handler(format % args)
diff --git a/pecan/core.py b/pecan/core.py
index 8964b4f..5804e19 100644
--- a/pecan/core.py
+++ b/pecan/core.py
@@ -2,7 +2,6 @@ try:
from simplejson import loads
except ImportError: # pragma: no cover
from json import loads # noqa
-from threading import local
from itertools import chain
from mimetypes import guess_type, add_type
from os.path import splitext
@@ -24,7 +23,7 @@ from .middleware.recursive import ForwardRequestException
# make sure that json is defined in mimetypes
add_type('application/json', '.json', True)
-state = local()
+state = None
logger = logging.getLogger(__name__)
@@ -142,7 +141,7 @@ def render(template, namespace):
return state.app.render(template, namespace)
-def load_app(config):
+def load_app(config, **kwargs):
'''
Used to load a ``Pecan`` application and its environment based on passed
configuration.
@@ -158,7 +157,7 @@ def load_app(config):
for package_name in getattr(_runtime_conf.app, 'modules', []):
module = __import__(package_name, fromlist=['app'])
if hasattr(module, 'app') and hasattr(module.app, 'setup_app'):
- app = module.app.setup_app(_runtime_conf)
+ app = module.app.setup_app(_runtime_conf, **kwargs)
app.config = _runtime_conf
return app
raise RuntimeError(
@@ -200,7 +199,10 @@ class Pecan(object):
def __init__(self, root, default_renderer='mako',
template_path='templates', hooks=lambda: [],
custom_renderers={}, extra_template_vars={},
- force_canonical=True, guess_content_type_from_ext=True, **kw):
+ force_canonical=True, guess_content_type_from_ext=True,
+ context_local_factory=None, **kw):
+
+ self.init_context_local(context_local_factory)
if isinstance(root, six.string_types):
root = self.__translate_root__(root)
@@ -221,6 +223,12 @@ class Pecan(object):
self.force_canonical = force_canonical
self.guess_content_type_from_ext = guess_content_type_from_ext
+ def init_context_local(self, local_factory):
+ global state
+ if local_factory is None:
+ from threading import local as local_factory
+ state = local_factory()
+
def __translate_root__(self, item):
'''
Creates a root controller instance from a string root, e.g.,
diff --git a/pecan/jsonify.py b/pecan/jsonify.py
index 726213a..8eb310e 100644
--- a/pecan/jsonify.py
+++ b/pecan/jsonify.py
@@ -89,9 +89,9 @@ class GenericJSON(JSONEncoder):
elif isinstance(obj, (date, datetime)):
return str(obj)
elif isinstance(obj, Decimal):
- # XXX What to do about JSONEncoder crappy handling of Decimals?
- # SimpleJSON has better Decimal encoding than the std lib
- # but only in recent versions
+ # XXX What to do about JSONEncoder crappy handling of Decimals?
+ # SimpleJSON has better Decimal encoding than the std lib
+ # but only in recent versions
return float(obj)
elif is_saobject(obj):
props = {}
diff --git a/pecan/log.py b/pecan/log.py
new file mode 100644
index 0000000..133fdf5
--- /dev/null
+++ b/pecan/log.py
@@ -0,0 +1,54 @@
+import logging
+
+from logutils.colorize import ColorizingStreamHandler
+
+
+class DefaultColorizer(ColorizingStreamHandler):
+
+ level_map = {
+ logging.DEBUG: (None, 'blue', True),
+ logging.INFO: (None, None, True),
+ logging.WARNING: (None, 'yellow', True),
+ logging.ERROR: (None, 'red', True),
+ logging.CRITICAL: (None, 'red', True),
+ }
+
+
+class ColorFormatter(logging.Formatter):
+ """
+ A very basic logging formatter that not only applies color to the
+ levels of the ouput but can also add padding to the the level names so that
+ they do not alter the visuals of logging when presented on the terminal.
+
+ The padding is provided by a convenient keyword that adds padding to the
+ ``levelname`` so that log output is easier to follow::
+
+ %(padded_color_levelname)s
+
+ Which would result in log level output that looks like::
+
+ [INFO ]
+ [WARNING ]
+ [ERROR ]
+ [DEBUG ]
+ [CRITICAL]
+
+ If colored output is not supported, it falls back to non-colored output
+ without any extra settings.
+ """
+
+ def __init__(self, _logging=None, colorizer=None, *a, **kw):
+ self.logging = _logging or logging
+ self.color = colorizer or DefaultColorizer()
+ logging.Formatter.__init__(self, *a, **kw)
+
+ def format(self, record):
+ levelname = record.levelname
+ padded_level = '%-8s' % levelname
+
+ record.color_levelname = self.color.colorize(levelname, record)
+ record.padded_color_levelname = self.color.colorize(
+ padded_level,
+ record
+ )
+ return self.logging.Formatter.format(self, record)
diff --git a/pecan/middleware/errordocument.py b/pecan/middleware/errordocument.py
index f08d536..12a3c9e 100644
--- a/pecan/middleware/errordocument.py
+++ b/pecan/middleware/errordocument.py
@@ -24,7 +24,7 @@ class StatusPersist(object):
try:
return self.app(environ, keep_status_start_response)
except RecursionLoop as e:
- environ['wsgi.errors'].errors.write(
+ environ['wsgi.errors'].write(
'Recursion error getting error page: %s\n' % e
)
keep_status_start_response(
diff --git a/pecan/middleware/recursive.py b/pecan/middleware/recursive.py
index b1296b8..fb9d19c 100644
--- a/pecan/middleware/recursive.py
+++ b/pecan/middleware/recursive.py
@@ -142,7 +142,7 @@ class ForwardRequestException(Exception):
self.path_info = path_info
# If the url can be treated as a path_info do that
- if url and not '?' in str(url):
+ if url and '?' not in str(url):
self.path_info = url
# Base middleware
diff --git a/pecan/scaffolds/base/+package+/templates/index.html b/pecan/scaffolds/base/+package+/templates/index.html
index ce01e11..f17c386 100644
--- a/pecan/scaffolds/base/+package+/templates/index.html
+++ b/pecan/scaffolds/base/+package+/templates/index.html
@@ -27,7 +27,7 @@
<fieldset>
<input name="q" />
<input type="submit" value="Search" />
- <fieldset>
+ </fieldset>
<small>Enter search terms or a module, class or function name.</small>
</form>
diff --git a/pecan/scaffolds/base/config.py_tmpl b/pecan/scaffolds/base/config.py_tmpl
index 16b70f3..696cc6a 100644
--- a/pecan/scaffolds/base/config.py_tmpl
+++ b/pecan/scaffolds/base/config.py_tmpl
@@ -21,6 +21,7 @@ logging = {
'loggers': {
'root': {'level': 'INFO', 'handlers': ['console']},
'${package}': {'level': 'DEBUG', 'handlers': ['console']},
+ 'pecan.commands.serve': {'level': 'DEBUG', 'handlers': ['console']},
'py.warnings': {'handlers': ['console']},
'__force_dict__': True
},
@@ -28,13 +29,19 @@ logging = {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
- 'formatter': 'simple'
+ 'formatter': 'color'
}
},
'formatters': {
'simple': {
'format': ('%(asctime)s %(levelname)-5.5s [%(name)s]'
'[%(threadName)s] %(message)s')
+ },
+ 'color': {
+ '()': 'pecan.log.ColorFormatter',
+ 'format': ('%(asctime)s [%(padded_color_levelname)s] [%(name)s]'
+ '[%(threadName)s] %(message)s'),
+ '__force_dict__': True
}
}
}
diff --git a/pecan/scaffolds/base/public/images/logo.png b/pecan/scaffolds/base/public/images/logo.png
index 5994d22..a8f403e 100644
--- a/pecan/scaffolds/base/public/images/logo.png
+++ b/pecan/scaffolds/base/public/images/logo.png
Binary files differ
diff --git a/pecan/scaffolds/rest-api/config.py_tmpl b/pecan/scaffolds/rest-api/config.py_tmpl
index bd4d29d..b0b3839 100644
--- a/pecan/scaffolds/rest-api/config.py_tmpl
+++ b/pecan/scaffolds/rest-api/config.py_tmpl
@@ -15,6 +15,7 @@ logging = {
'loggers': {
'root': {'level': 'INFO', 'handlers': ['console']},
'${package}': {'level': 'DEBUG', 'handlers': ['console']},
+ 'pecan.commands.serve': {'level': 'DEBUG', 'handlers': ['console']},
'py.warnings': {'handlers': ['console']},
'__force_dict__': True
},
@@ -22,13 +23,19 @@ logging = {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
- 'formatter': 'simple'
+ 'formatter': 'color'
}
},
'formatters': {
'simple': {
'format': ('%(asctime)s %(levelname)-5.5s [%(name)s]'
'[%(threadName)s] %(message)s')
+ },
+ 'color': {
+ '()': 'pecan.log.ColorFormatter',
+ 'format': ('%(asctime)s [%(padded_color_levelname)s] [%(name)s]'
+ '[%(threadName)s] %(message)s'),
+ '__force_dict__': True
}
}
}
diff --git a/pecan/testing.py b/pecan/testing.py
index ee2896e..14986ff 100644
--- a/pecan/testing.py
+++ b/pecan/testing.py
@@ -2,7 +2,7 @@ from pecan import load_app
from webtest import TestApp
-def load_test_app(config=None):
+def load_test_app(config=None, **kwargs):
"""
Used for functional tests where you need to test your
literal application and its integration with the framework.
@@ -32,4 +32,4 @@ def load_test_app(config=None):
resp = app.get('/path/to/some/resource').status_int
assert resp.status_int == 200
"""
- return TestApp(load_app(config))
+ return TestApp(load_app(config, **kwargs))
diff --git a/pecan/tests/test_base.py b/pecan/tests/test_base.py
index 279f63b..96f7757 100644
--- a/pecan/tests/test_base.py
+++ b/pecan/tests/test_base.py
@@ -867,9 +867,9 @@ class TestRedirect(PecanTestCase):
res = app.get(
'/child', extra_environ=dict(HTTP_X_FORWARDED_PROTO='https')
)
- ##non-canonical url will redirect, so we won't get a 301
+ # non-canonical url will redirect, so we won't get a 301
assert res.status_int == 302
- ##should add trailing / and changes location to https
+ # should add trailing / and changes location to https
assert res.location == 'https://localhost/child/'
assert res.request.environ['HTTP_X_FORWARDED_PROTO'] == 'https'
diff --git a/requirements.txt b/requirements.txt
index 9205775..d38259b 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -2,3 +2,4 @@ WebOb>=1.2dev
Mako>=0.4.0
WebTest>=1.3.1
six
+logutils>=0.3
diff --git a/setup.py b/setup.py
index e32f5ad..02cbc6b 100644
--- a/setup.py
+++ b/setup.py
@@ -3,7 +3,7 @@ import platform
from setuptools import setup, find_packages
-version = '0.4.5'
+version = '0.5.0'
#
# determine requirements
@@ -23,15 +23,6 @@ except:
requirements.append("simplejson >= 2.1.1")
try:
- from logging.config import dictConfig # noqa
-except ImportError:
- #
- # This was introduced in Python 2.7 - the logutils package contains
- # a backported replacement for 2.6
- #
- requirements.append('logutils')
-
-try:
import argparse # noqa
except:
#
diff --git a/tools/run_cross_tests.sh b/tools/run_cross_tests.sh
new file mode 100755
index 0000000..fb21e65
--- /dev/null
+++ b/tools/run_cross_tests.sh
@@ -0,0 +1,70 @@
+#!/bin/bash
+#
+# Run cross-project tests
+
+# Fail the build if any command fails
+set -e
+
+project_dir="$1"
+venv="$2"
+
+# Set up the virtualenv without running the tests
+(cd $project_dir && tox --notest -e $venv)
+
+tox_envbin=$project_dir/.tox/$venv/bin
+
+our_name=$(python setup.py --name)
+
+# Replace the pip-installed package with the version in our source
+# tree. Look to see if we are already installed before trying to
+# uninstall ourselves, to avoid failures from packages that do not use us
+# yet.
+if $tox_envbin/pip freeze | grep -q $our_name
+then
+ $tox_envbin/pip uninstall -y $our_name
+fi
+$tox_envbin/pip install -U .
+
+# Run the tests
+(cd $project_dir && tox -e $venv)
+result=$?
+
+
+# The below checks are modified from
+# openstack-infra/config/modules/jenkins/files/slave_scripts/run-unittests.sh.
+
+# They expect to be run in the project being tested.
+cd $project_dir
+
+echo "Begin pip freeze output from test virtualenv:"
+echo "======================================================================"
+.tox/$venv/bin/pip freeze
+echo "======================================================================"
+
+# We only want to run the next check if the tool is installed, so look
+# for it before continuing.
+if [ -f /usr/local/jenkins/slave_scripts/subunit2html.py -a -d ".testrepository" ] ; then
+ if [ -f ".testrepository/0.2" ] ; then
+ cp .testrepository/0.2 ./subunit_log.txt
+ elif [ -f ".testrepository/0" ] ; then
+ .tox/$venv/bin/subunit-1to2 < .testrepository/0 > ./subunit_log.txt
+ fi
+ .tox/$venv/bin/python /usr/local/jenkins/slave_scripts/subunit2html.py ./subunit_log.txt testr_results.html
+ gzip -9 ./subunit_log.txt
+ gzip -9 ./testr_results.html
+
+ export PYTHON=.tox/$venv/bin/python
+ set -e
+ rancount=$(.tox/$venv/bin/testr last | sed -ne 's/Ran \([0-9]\+\).*tests in.*/\1/p')
+ if [ "$rancount" -eq "0" ] ; then
+ echo
+ echo "Zero tests were run. At least one test should have been run."
+ echo "Failing this test as a result"
+ echo
+ exit 1
+ fi
+fi
+
+# If we make it this far, report status based on the tests that were
+# run.
+exit $result