Another WSGI Framework
Project description
![TravisCI Status](https://api.travis-ci.org/jd-boyd/corker.png)
# About
I wanted a framework that was based around
[routes](https://github.com/bbangert/routes) and
[webob](http://webob.org/) without opinionation about database and
templating.
Some of the specifics of this are inspired/copied from: http://www.ianbicking.org/blog/2010/03/12/a-webob-app-example/index.html
# Installation
`pip install corker`
# Usage
The basics are that you create controllers (classes that subclass
`BaseController`). In a controller, you label methods with the `@route`
decorator to expose them. You then invoke your controller's
`setup_routes` method (inheritted from `BaseController`), passing it a
routes mapper to add itself to. Then, to create the actual wsgi app,
you create an `Application` and pass it the mapper.
In a controller, `self.request` gives you the current WebOb request
object. The controller can return a string containing HTML (in which
case it will be given a status code of 200), or a WebOb response object.
Additionally, it is safe to raise any
[WebOb exception](http://webob.readthedocs.org/en/latest/modules/exceptions.html).
On an exposed method, the arguments (after self) are from routes
positional/regex arguments. GET and POST arguments are accessed via
`self.request`.
The arguments for the `@route` decorator largely match the arguments for
route's `mapper.connect` method. The decorated method is automatically
used as the action argument to `mapper.connect` and all other arguments
to `@route` are passed through to `mapper.connect` as is.
Arguments passed to `Application` after the mapper are inserted into
controllers with the same name. So, is the `Application` was
instantiated with `Application(mapper, x=5)`, then in an exposed method
on the controller, `self.x == 5` would be `True`.
## Example
```python
from routes import Mapper
from corker.controller import BaseController, route
from corker.app import Application
from webob import Response
class Index(BaseController):
@route('')
def index(self, request):
return 'Hi index!\n'
@route('view/{item}')
def view(self, request, item):
return 'Hi view %d!\n' % int(item)
class Sub(BaseController):
@route('')
def index(self, request):
return Response('Hi sub!\n')
mapper = Mapper()
Index.setup_routes(mapper)
with mapper.submapper(path_prefix='/sub') as sub:
Sub.setup_routes(sub)
example_app = Application(mapper)
# At that point `example_app` is a wsgi app ready to be mounted by the
# server of your choice. For example with `wsgiref`:
from wsgiref.util import setup_testing_defaults
from wsgiref.simple_server import make_server
httpd = make_server('', 8000, example_app)
print "Serving on port 8000..."
httpd.serve_forever()
```
# Bugs/Feature Requests
See [github issues](https://github.com/jd-boyd/corker/issues).
# Copyright
This is distributed as BSD. Copyright Joshua D. Boyd
# About
I wanted a framework that was based around
[routes](https://github.com/bbangert/routes) and
[webob](http://webob.org/) without opinionation about database and
templating.
Some of the specifics of this are inspired/copied from: http://www.ianbicking.org/blog/2010/03/12/a-webob-app-example/index.html
# Installation
`pip install corker`
# Usage
The basics are that you create controllers (classes that subclass
`BaseController`). In a controller, you label methods with the `@route`
decorator to expose them. You then invoke your controller's
`setup_routes` method (inheritted from `BaseController`), passing it a
routes mapper to add itself to. Then, to create the actual wsgi app,
you create an `Application` and pass it the mapper.
In a controller, `self.request` gives you the current WebOb request
object. The controller can return a string containing HTML (in which
case it will be given a status code of 200), or a WebOb response object.
Additionally, it is safe to raise any
[WebOb exception](http://webob.readthedocs.org/en/latest/modules/exceptions.html).
On an exposed method, the arguments (after self) are from routes
positional/regex arguments. GET and POST arguments are accessed via
`self.request`.
The arguments for the `@route` decorator largely match the arguments for
route's `mapper.connect` method. The decorated method is automatically
used as the action argument to `mapper.connect` and all other arguments
to `@route` are passed through to `mapper.connect` as is.
Arguments passed to `Application` after the mapper are inserted into
controllers with the same name. So, is the `Application` was
instantiated with `Application(mapper, x=5)`, then in an exposed method
on the controller, `self.x == 5` would be `True`.
## Example
```python
from routes import Mapper
from corker.controller import BaseController, route
from corker.app import Application
from webob import Response
class Index(BaseController):
@route('')
def index(self, request):
return 'Hi index!\n'
@route('view/{item}')
def view(self, request, item):
return 'Hi view %d!\n' % int(item)
class Sub(BaseController):
@route('')
def index(self, request):
return Response('Hi sub!\n')
mapper = Mapper()
Index.setup_routes(mapper)
with mapper.submapper(path_prefix='/sub') as sub:
Sub.setup_routes(sub)
example_app = Application(mapper)
# At that point `example_app` is a wsgi app ready to be mounted by the
# server of your choice. For example with `wsgiref`:
from wsgiref.util import setup_testing_defaults
from wsgiref.simple_server import make_server
httpd = make_server('', 8000, example_app)
print "Serving on port 8000..."
httpd.serve_forever()
```
# Bugs/Feature Requests
See [github issues](https://github.com/jd-boyd/corker/issues).
# Copyright
This is distributed as BSD. Copyright Joshua D. Boyd
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
corker-0.5.0.tar.gz
(5.4 kB
view details)
Built Distribution
File details
Details for the file corker-0.5.0.tar.gz
.
File metadata
- Download URL: corker-0.5.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5ca8c1ac760a8a7ea118b56e12ba73cb194b3597d2d4d4e574412658bdad6fb |
|
MD5 | b1f73492ac725a77903a115b14dfaec8 |
|
BLAKE2b-256 | f03b907219bec4d3ef2e24759272512e26e773d5339694568b1f0859c693a315 |
File details
Details for the file corker-0.5.0-py2-none-any.whl
.
File metadata
- Download URL: corker-0.5.0-py2-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 431f389b74305193331d702413e42d09fe08f5b28f817a5e031104f4d8ed6aa1 |
|
MD5 | 788e1d7e66185e6c165db188d32a14cb |
|
BLAKE2b-256 | 12e19d77868390d4c776da0e5493587c00465e6341407f648fb1b6545d67a1ae |