{ "info": { "author": "Adeel Ahmad Khan", "author_email": "adeel2@umbc.edu", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "License :: OSI Approved :: MIT License" ], "description": "Berry is a minimal DSL for building a WSGI application.\n\nBasically, you map some routes to functions. Berry takes this mapping and\ngenerates a WSGI app, which you can serve however you like.\n\nExample\n-------\n\n Here is an example using the wsgiref server, included in Python's stdlib\n and ideal for development use.\n\n import berry\n from wsgiref.simple_server import make_server\n \n @berry.get('^$')\n def index(req):\n return \"Welcome to the home page.\"\n \n @berry.get('^hello/(.+)/?$')\n def hello(req, name):\n return \"Hello, %s!\" % name\n \n # generate a WSGI app\n wsgi_app = berry.app\n \n # start a WSGI server\n make_server('127.0.0.1', 8000, wsgi_app).serve_forever()\n\nHow to use it\n-------------\n\n Decorate a function with berry.get(route) or berry.post(route) to serve\n GET/POST requests that match a route. Routes must be regular expressions.\n Your function will be passed a Request object as the first argument.\n \n Example:\n \n @berry.get('^$')\n @berry.get('^home$')\n def home(req):\n return 'This is the home page.'\n \n As above, you can map multiple routes to the same function.\n \n Request objects\n ---------------\n \n Useful attributes of Request objects are:\n \n - env: the WSGI environ variable.\n - params: parameters passed through both GET and POST.\n - path: the path requested, minus the initial '/' and the query string\n - query: the query string, if any\n - fullpath: the full path requested, including the initial '/' and the\n query string\n - method: the method (GET or POST)\n \n Example:\n \n @berry.post('^login$')\n def login(req):\n username = req.params.get('username')\n password = req.params.get('password')\n # ...\n\n Note: if you have a field like 'a[b]' with value 'c', Berry will parse it\n into a dictionary. For example:\n\n \n \n\n will result in req.params being:\n\n {'person': {'name': 'James', 'age': '20'}}.\n \n Also,\n \n \n \n\n will result in req.params being:\n\n {'person': {'friends': ['James', 'John']}}.\n\n Handling errors\n ---------------\n \n Using the berry.error(code) decorator, you can make custom error pages.\n \n Example:\n \n @berry.error(404)\n def notfound(req):\n return \"%s was not found.\" % req.fullpath\n \n Berry has Redirect, Forbidden, NotFound, and AppError classes, which\n are exceptions that inherit berry.HTTPError. Just raise one of them:\n \n if not user.is_logged_in():\n raise berry.Forbidden()\n \n To add an exception for a new HTTP status code you can do something like:\n \n class Unauthorized(berry.HTTPError):\n status = (401, 'Unauthorized')\n content = \"