bobo-0.2.2/ 0000755 0000765 0000765 00000000000 11325342655 010574 5 ustar jim jim bobo-0.2.2/PKG-INFO 0000644 0000765 0000765 00000003237 11325342655 011676 0 ustar jim jim Metadata-Version: 1.0
Name: bobo
Version: 0.2.2
Summary: Web application framework for the impatient
Home-page: http://www.python.org/pypi/bobo
Author: Jim Fulton
Author-email: jim@zope.com
License: ZPL 2.1
Description: Bobo
====
Bobo is a light-weight framework for creating WSGI web applications.
Its goal is to be easy to use and remember.
It addresses 2 problems:
- Mapping URLs to objects
- Calling objects to generate HTTP responses
Bobo doesn't have a templating language, a database integration layer,
or a number of other features that are better provided by WSGI
middle-ware or application-specific libraries.
Bobo builds on other frameworks, most notably WSGI and WebOb.
To learn more. visit: http://bobo.digicool.com
Change History
==============
0.2.2 2010-01-19
----------------
Bugs fixed:
- An intended optmization to cache resource decorator computations
didn't work, making request handling slower than it should have
been.
- URLs were sometimes treated as if they had extra slashes when
traversing subroutes.
- boboserver.File must explicitly open files in binary mode, which is not
the default on Windows.
0.2.1 2009-06-16
----------------
Packaging update to update documentation.
0.2.0 2009-05-26
----------------
Initial Public Release
Keywords: WSGI
Platform: UNKNOWN
bobo-0.2.2/README.txt 0000644 0000765 0000765 00000002051 11325342322 012257 0 ustar jim jim Bobo
====
Bobo is a light-weight framework for creating WSGI web applications.
Its goal is to be easy to use and remember.
It addresses 2 problems:
- Mapping URLs to objects
- Calling objects to generate HTTP responses
Bobo doesn't have a templating language, a database integration layer,
or a number of other features that are better provided by WSGI
middle-ware or application-specific libraries.
Bobo builds on other frameworks, most notably WSGI and WebOb.
To learn more. visit: http://bobo.digicool.com
Change History
==============
0.2.2 2010-01-19
----------------
Bugs fixed:
- An intended optmization to cache resource decorator computations
didn't work, making request handling slower than it should have
been.
- URLs were sometimes treated as if they had extra slashes when
traversing subroutes.
- boboserver.File must explicitly open files in binary mode, which is not
the default on Windows.
0.2.1 2009-06-16
----------------
Packaging update to update documentation.
0.2.0 2009-05-26
----------------
Initial Public Release
bobo-0.2.2/ez_setup.py 0000644 0000765 0000765 00000022764 11325342322 013006 0 ustar jim jim #!python
"""Bootstrap setuptools installation
If you want to use setuptools in your package's setup.py, just include this
file in the same directory with it, and add this to the top of your setup.py::
from ez_setup import use_setuptools
use_setuptools()
If you want to require a specific version of setuptools, set a download
mirror, or use an alternate download directory, you can do so by supplying
the appropriate options to ``use_setuptools()``.
This file can also be run as a script to install or upgrade setuptools.
"""
import sys
DEFAULT_VERSION = "0.6c9"
DEFAULT_URL = "http://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3]
md5_data = {
'setuptools-0.6b1-py2.3.egg': '8822caf901250d848b996b7f25c6e6ca',
'setuptools-0.6b1-py2.4.egg': 'b79a8a403e4502fbb85ee3f1941735cb',
'setuptools-0.6b2-py2.3.egg': '5657759d8a6d8fc44070a9d07272d99b',
'setuptools-0.6b2-py2.4.egg': '4996a8d169d2be661fa32a6e52e4f82a',
'setuptools-0.6b3-py2.3.egg': 'bb31c0fc7399a63579975cad9f5a0618',
'setuptools-0.6b3-py2.4.egg': '38a8c6b3d6ecd22247f179f7da669fac',
'setuptools-0.6b4-py2.3.egg': '62045a24ed4e1ebc77fe039aa4e6f7e5',
'setuptools-0.6b4-py2.4.egg': '4cb2a185d228dacffb2d17f103b3b1c4',
'setuptools-0.6c1-py2.3.egg': 'b3f2b5539d65cb7f74ad79127f1a908c',
'setuptools-0.6c1-py2.4.egg': 'b45adeda0667d2d2ffe14009364f2a4b',
'setuptools-0.6c2-py2.3.egg': 'f0064bf6aa2b7d0f3ba0b43f20817c27',
'setuptools-0.6c2-py2.4.egg': '616192eec35f47e8ea16cd6a122b7277',
'setuptools-0.6c3-py2.3.egg': 'f181fa125dfe85a259c9cd6f1d7b78fa',
'setuptools-0.6c3-py2.4.egg': 'e0ed74682c998bfb73bf803a50e7b71e',
'setuptools-0.6c3-py2.5.egg': 'abef16fdd61955514841c7c6bd98965e',
'setuptools-0.6c4-py2.3.egg': 'b0b9131acab32022bfac7f44c5d7971f',
'setuptools-0.6c4-py2.4.egg': '2a1f9656d4fbf3c97bf946c0a124e6e2',
'setuptools-0.6c4-py2.5.egg': '8f5a052e32cdb9c72bcf4b5526f28afc',
'setuptools-0.6c5-py2.3.egg': 'ee9fd80965da04f2f3e6b3576e9d8167',
'setuptools-0.6c5-py2.4.egg': 'afe2adf1c01701ee841761f5bcd8aa64',
'setuptools-0.6c5-py2.5.egg': 'a8d3f61494ccaa8714dfed37bccd3d5d',
'setuptools-0.6c6-py2.3.egg': '35686b78116a668847237b69d549ec20',
'setuptools-0.6c6-py2.4.egg': '3c56af57be3225019260a644430065ab',
'setuptools-0.6c6-py2.5.egg': 'b2f8a7520709a5b34f80946de5f02f53',
'setuptools-0.6c7-py2.3.egg': '209fdf9adc3a615e5115b725658e13e2',
'setuptools-0.6c7-py2.4.egg': '5a8f954807d46a0fb67cf1f26c55a82e',
'setuptools-0.6c7-py2.5.egg': '45d2ad28f9750e7434111fde831e8372',
'setuptools-0.6c8-py2.3.egg': '50759d29b349db8cfd807ba8303f1902',
'setuptools-0.6c8-py2.4.egg': 'cba38d74f7d483c06e9daa6070cce6de',
'setuptools-0.6c8-py2.5.egg': '1721747ee329dc150590a58b3e1ac95b',
'setuptools-0.6c9-py2.3.egg': 'a83c4020414807b496e4cfbe08507c03',
'setuptools-0.6c9-py2.4.egg': '260a2be2e5388d66bdaee06abec6342a',
'setuptools-0.6c9-py2.5.egg': 'fe67c3e5a17b12c0e7c541b7ea43a8e6',
'setuptools-0.6c9-py2.6.egg': 'ca37b1ff16fa2ede6e19383e7b59245a',
}
import sys, os
try: from hashlib import md5
except ImportError: from md5 import md5
def _validate_md5(egg_name, data):
if egg_name in md5_data:
digest = md5(data).hexdigest()
if digest != md5_data[egg_name]:
print >>sys.stderr, (
"md5 validation of %s failed! (Possible download problem?)"
% egg_name
)
sys.exit(2)
return data
def use_setuptools(
version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
download_delay=15
):
"""Automatically find/download setuptools and make it available on sys.path
`version` should be a valid setuptools version number that is available
as an egg for download under the `download_base` URL (which should end with
a '/'). `to_dir` is the directory where setuptools will be downloaded, if
it is not already available. If `download_delay` is specified, it should
be the number of seconds that will be paused before initiating a download,
should one be required. If an older version of setuptools is installed,
this routine will print a message to ``sys.stderr`` and raise SystemExit in
an attempt to abort the calling script.
"""
was_imported = 'pkg_resources' in sys.modules or 'setuptools' in sys.modules
def do_download():
egg = download_setuptools(version, download_base, to_dir, download_delay)
sys.path.insert(0, egg)
import setuptools; setuptools.bootstrap_install_from = egg
try:
import pkg_resources
except ImportError:
return do_download()
try:
pkg_resources.require("setuptools>="+version); return
except pkg_resources.VersionConflict, e:
if was_imported:
print >>sys.stderr, (
"The required version of setuptools (>=%s) is not available, and\n"
"can't be installed while this script is running. Please install\n"
" a more recent version first, using 'easy_install -U setuptools'."
"\n\n(Currently using %r)"
) % (version, e.args[0])
sys.exit(2)
else:
del pkg_resources, sys.modules['pkg_resources'] # reload ok
return do_download()
except pkg_resources.DistributionNotFound:
return do_download()
def download_setuptools(
version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
delay = 15
):
"""Download setuptools from a specified location and return its filename
`version` should be a valid setuptools version number that is available
as an egg for download under the `download_base` URL (which should end
with a '/'). `to_dir` is the directory where the egg will be downloaded.
`delay` is the number of seconds to pause before an actual download attempt.
"""
import urllib2, shutil
egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3])
url = download_base + egg_name
saveto = os.path.join(to_dir, egg_name)
src = dst = None
if not os.path.exists(saveto): # Avoid repeated downloads
try:
from distutils import log
if delay:
log.warn("""
---------------------------------------------------------------------------
This script requires setuptools version %s to run (even to display
help). I will attempt to download it for you (from
%s), but
you may need to enable firewall access for this script first.
I will start the download in %d seconds.
(Note: if this machine does not have network access, please obtain the file
%s
and place it in this directory before rerunning this script.)
---------------------------------------------------------------------------""",
version, download_base, delay, url
); from time import sleep; sleep(delay)
log.warn("Downloading %s", url)
src = urllib2.urlopen(url)
# Read/write all in one block, so we don't create a corrupt file
# if the download is interrupted.
data = _validate_md5(egg_name, src.read())
dst = open(saveto,"wb"); dst.write(data)
finally:
if src: src.close()
if dst: dst.close()
return os.path.realpath(saveto)
def main(argv, version=DEFAULT_VERSION):
"""Install or upgrade setuptools and EasyInstall"""
try:
import setuptools
except ImportError:
egg = None
try:
egg = download_setuptools(version, delay=0)
sys.path.insert(0,egg)
from setuptools.command.easy_install import main
return main(list(argv)+[egg]) # we're done here
finally:
if egg and os.path.exists(egg):
os.unlink(egg)
else:
if setuptools.__version__ == '0.0.1':
print >>sys.stderr, (
"You have an obsolete version of setuptools installed. Please\n"
"remove it from your system entirely before rerunning this script."
)
sys.exit(2)
req = "setuptools>="+version
import pkg_resources
try:
pkg_resources.require(req)
except pkg_resources.VersionConflict:
try:
from setuptools.command.easy_install import main
except ImportError:
from easy_install import main
main(list(argv)+[download_setuptools(delay=0)])
sys.exit(0) # try to force an exit
else:
if argv:
from setuptools.command.easy_install import main
main(argv)
else:
print "Setuptools version",version,"or greater has been installed."
print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)'
def update_md5(filenames):
"""Update our built-in md5 registry"""
import re
for name in filenames:
base = os.path.basename(name)
f = open(name,'rb')
md5_data[base] = md5(f.read()).hexdigest()
f.close()
data = [" %r: %r,\n" % it for it in md5_data.items()]
data.sort()
repl = "".join(data)
import inspect
srcfile = inspect.getsourcefile(sys.modules[__name__])
f = open(srcfile, 'rb'); src = f.read(); f.close()
match = re.search("\nmd5_data = {\n([^}]+)}", src)
if not match:
print >>sys.stderr, "Internal error!"
sys.exit(2)
src = src[:match.start(1)] + repl + src[match.end(1):]
f = open(srcfile,'w')
f.write(src)
f.close()
if __name__=='__main__':
if len(sys.argv)>2 and sys.argv[1]=='--md5update':
update_md5(sys.argv[2:])
else:
main(sys.argv[1:])
bobo-0.2.2/setup.cfg 0000644 0000765 0000765 00000000073 11325342655 012415 0 ustar jim jim [egg_info]
tag_build =
tag_date = 0
tag_svn_revision = 0
bobo-0.2.2/setup.py 0000644 0000765 0000765 00000003234 11325342353 012303 0 ustar jim jim ##############################################################################
#
# Copyright Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
name = 'bobo'
version = '0.2.2'
entry_points = """
[console_scripts]
bobo = boboserver:server
[paste.app_factory]
main = bobo:Application
[paste.filter_app_factory]
reload = boboserver:Reload
debug = boboserver:Debug
"""
from ez_setup import use_setuptools
use_setuptools()
from setuptools import setup
import sys
if sys.version_info >= (2, 5):
install_requires = ['WebOb']
else:
install_requires = ['WebOb', 'PasteDeploy', 'Paste']
setup(
name = name,
version = version,
author = "Jim Fulton",
author_email = "jim@zope.com",
description = "Web application framework for the impatient",
license = "ZPL 2.1",
keywords = "WSGI",
url='http://www.python.org/pypi/'+name,
long_description=open('README.txt').read(),
py_modules = ['bobo', 'boboserver'],
package_dir = {'':'src'},
install_requires = install_requires,
entry_points = entry_points,
tests_require = [
'bobodoctestumentation >=%s, <%s.999' % (version, version)],
test_suite = 'bobodoctestumentation.tests.test_suite',
)
bobo-0.2.2/src/ 0000755 0000765 0000765 00000000000 11325342655 011363 5 ustar jim jim bobo-0.2.2/src/bobo.egg-info/ 0000755 0000765 0000765 00000000000 11325342655 013776 5 ustar jim jim bobo-0.2.2/src/bobo.egg-info/PKG-INFO 0000644 0000765 0000765 00000003237 11325342655 015100 0 ustar jim jim Metadata-Version: 1.0
Name: bobo
Version: 0.2.2
Summary: Web application framework for the impatient
Home-page: http://www.python.org/pypi/bobo
Author: Jim Fulton
Author-email: jim@zope.com
License: ZPL 2.1
Description: Bobo
====
Bobo is a light-weight framework for creating WSGI web applications.
Its goal is to be easy to use and remember.
It addresses 2 problems:
- Mapping URLs to objects
- Calling objects to generate HTTP responses
Bobo doesn't have a templating language, a database integration layer,
or a number of other features that are better provided by WSGI
middle-ware or application-specific libraries.
Bobo builds on other frameworks, most notably WSGI and WebOb.
To learn more. visit: http://bobo.digicool.com
Change History
==============
0.2.2 2010-01-19
----------------
Bugs fixed:
- An intended optmization to cache resource decorator computations
didn't work, making request handling slower than it should have
been.
- URLs were sometimes treated as if they had extra slashes when
traversing subroutes.
- boboserver.File must explicitly open files in binary mode, which is not
the default on Windows.
0.2.1 2009-06-16
----------------
Packaging update to update documentation.
0.2.0 2009-05-26
----------------
Initial Public Release
Keywords: WSGI
Platform: UNKNOWN
bobo-0.2.2/src/bobo.egg-info/SOURCES.txt 0000644 0000765 0000765 00000000377 11325342655 015671 0 ustar jim jim README.txt
ez_setup.py
setup.py
src/bobo.py
src/boboserver.py
src/bobo.egg-info/PKG-INFO
src/bobo.egg-info/SOURCES.txt
src/bobo.egg-info/dependency_links.txt
src/bobo.egg-info/entry_points.txt
src/bobo.egg-info/requires.txt
src/bobo.egg-info/top_level.txt bobo-0.2.2/src/bobo.egg-info/dependency_links.txt 0000644 0000765 0000765 00000000001 11325342655 020044 0 ustar jim jim
bobo-0.2.2/src/bobo.egg-info/entry_points.txt 0000644 0000765 0000765 00000000251 11325342655 017272 0 ustar jim jim
[console_scripts]
bobo = boboserver:server
[paste.app_factory]
main = bobo:Application
[paste.filter_app_factory]
reload = boboserver:Reload
debug = boboserver:Debug
bobo-0.2.2/src/bobo.egg-info/requires.txt 0000644 0000765 0000765 00000000027 11325342655 016375 0 ustar jim jim WebOb
PasteDeploy
Paste bobo-0.2.2/src/bobo.egg-info/top_level.txt 0000644 0000765 0000765 00000000020 11325342655 016520 0 ustar jim jim bobo
boboserver
bobo-0.2.2/src/bobo.py 0000644 0000765 0000765 00000124104 11325342322 012647 0 ustar jim jim ##############################################################################
#
# Copyright Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Create WSGI-based web applications.
"""
# Public names:
__all__ = (
'Application',
'early',
'late',
'NotFound',
'order',
'post',
'preroute',
'query',
'redirect',
'reroute',
'resource',
'resources',
'scan_class',
'subroute',
)
__metaclass__ = type
import re
import sys
import webob
bbbbad_errors = KeyboardInterrupt, SystemExit, MemoryError
_default_content_type = 'text/html; charset=UTF-8'
_json_content_type = re.compile('application/json;?').match
class Application:
"""Create a WSGI application.
The DEFAULT argument, if given, is a dictionary of options.
Keyword options override options given in the DEFAULT options.
Option values are strings, typically read from ConfigParser files.
The values used by bobo, ``bobo_resources``, ``bobo_configure``
and ``bobo_errors``, can have comments. Lines within these
values are truncated at the first '#' characters.
The one required option is bobo_resources:
bobo_resources
Specifies resources to be used.
This option can be used to:
- Specify modules to be scanned for resources.
- Include specific resources, rather than all resources in given modules.
- Override the order of resources given in modules.
- Override routes used for resources given in modules.
Resources are specified on separate lines. Resources take one
of 4 forms:
module_name
Use the resources from the given module.
resource
Use the named resource.
The resource is of the form: modulename:expression. The
object is obtained by evaluating the expression in the named
module.
route -> resource
The given route, possibly with placeholders, is
handled by the given resource.
The resource is of the form: modulename:expression.
The object named by the resource must meet one of the following
conditions:
- It was created using one of the bobo decorators:
``resource``, ``post``, ``query``, or ``subroute``.
- It has ``bobo_reroute`` method that takes the given route
and returns a new resource. (The bobo decorators provide this.)
- It is a class, in which case it is treated as a subroute.
Newlines may be included between the"->" and the resource, allowing
the specification to be split over multiple lines.
route +> resource
The given route, which may not have placeholder, is added as
a prefix of the given resource's route.
The resource is of the form: modulename:expression, or just
modulename.
Newlines may be included between the"+>" and the resource, allowing
the specification to be split over multiple lines.
Bobo also used the following options:
bobo_configuration
Specify one or more (whitespace-delimited) callables to be
called with the configuration data passed to the application.
Each callable is of the form: module_name:global_name
bobo_errors
Specify an object to be used for generating error responses.
The value must be a module name or an object name of the form:
``modulename:expression``. The object must have the
callable attributes:
not_found(request, method)
Generate a response when a resource can't be found.
This should return a 404 response.
method_not_allowed(request, method, methods)
Generate a response when the resource found doesn't allow the
request method.
This should return a 405 response and set the ``Allowed`` response
header to the list of allowed headers.
missing_form_variable(request, method, name)
Generate a response when a form variable is missing.
The proper response in this situation isn't obvious.
exception(request, method, ex_info)
Generate a response for the exception information given by
exc_info. This method is optional. Bobo's default behavior
is to simply re-raise the exception.
"""
def __init__(self, DEFAULT=None, **config):
if DEFAULT:
DEFAULT = dict(DEFAULT)
DEFAULT.update(config)
config = DEFAULT
self.config = config
for name in filter(None, _uncomment(config, 'bobo_configure').split()):
_get_global(name)(config)
bobo_errors = _uncomment(config, 'bobo_errors')
if bobo_errors:
if ':' in bobo_errors:
bobo_errors = _get_global(bobo_errors)
else:
bobo_errors = _import(bobo_errors)
self.not_found = bobo_errors.not_found
self.method_not_allowed = bobo_errors.method_not_allowed
self.missing_form_variable = bobo_errors.missing_form_variable
try:
self.exception = bobo_errors.exception
except AttributeError:
pass
bobo_resources = _uncomment(config, 'bobo_resources', True)
if bobo_resources:
self.handlers = _route_config(bobo_resources)
else:
raise ValueError("Missing bobo_resources option.")
def bobo_response(self, request, path, method):
try:
for handler in self.handlers:
response = handler(request, path, method)
if response is not None:
return response
return self.not_found(request, method)
except BoboException, exc:
return self.build_response(request, method, exc)
except MethodNotAllowed, v:
return self.method_not_allowed(request, method, v.allowed)
except MissingFormVariable, v:
return self.missing_form_variable(request, method, v.name)
except NotFound, v:
return self.not_found(request, method)
except bbbbad_errors:
raise
except Exception:
if not hasattr(self, 'exception'):
raise
return self.exception(request, method, sys.exc_info())
def __call__(self, environ, start_response):
"""Handle a WSGI application request.
"""
request = webob.Request(environ)
if request.charset is None:
# Maybe middleware can be more tricky?
request.charset = 'utf8'
return self.bobo_response(request, request.path_info, request.method
)(environ, start_response)
def build_response(self, request, method, data):
"""Build a response object from raw data.
This method is used by bobo when an application returns data rather
than a response object. It can be overridden by subclasses to support
alternative request implementations. (For example, some implementations
may have response objects on a request that influence how a response is
generated.)
The data object has several attributes:
status
Integer HTTP status code
body
Raw body data as returned from an application
content_type
The desired content type
headers
A list of header name/value pairs.
"""
content_type = data.content_type
response = webob.Response(status=data.status,
headerlist=data.headers,
content_type=content_type)
if method == 'HEAD':
return response
body = data.body
if isinstance(body, str):
response.body = body
elif _json_content_type(content_type):
try:
import json
except ImportError:
import simplejson
sys.modules['json'] = simplejson
json = simplejson
response.body = json.dumps(body)
elif isinstance(body, unicode):
response.unicode_body = body
else:
raise TypeError('bad response', body, content_type)
return response
def not_found(self, request, method):
return _err_response(
404, method, "Not Found", "Could not find: "+request.path_info)
def missing_form_variable(self, request, method, name):
return _err_response(
403, method,
"Missing parameter", 'Missing form variable %s' % name)
def method_not_allowed(self, request, method, methods):
return _err_response(
405, method,
"Method Not Allowed", "Invalid request method: %s" % method,
[('Allow', ', '.join(sorted(methods)))])
def _err_response(status, method, title, message, headers=()):
response = webob.Response(status=status, headerlist=headers or [])
response.content_type = 'text/html; charset=UTF-8'
if method != 'HEAD':
response.unicode_body = _html_template % (title, message)
return response
_html_template = u"""
%s
%s
"""
def redirect(url, status=302, body=None,
content_type="text/html; charset=UTF-8"):
"""Generate a response to redirect to a URL.
The optional ``status`` argument can be used to supply a status other than
302. The optional ``body`` argument can be used to specify a response
body. If not specified, a default body is generated based on the URL given
in the ``url`` argument.
"""
if body is None:
body = u'See %s' % url
response = webob.Response(status=status, headerlist=[('Location', url)])
response.content_type = content_type
response.unicode_body = body
return response
class BoboException(Exception):
def __init__(self, status, body,
content_type='text/html; charset=UTF-8', headers=None):
self.status = status
self.body = body
self.content_type = content_type
self.headers = headers or []
def _scan_module(module_name):
module = _import(module_name)
bobo_response = getattr(module, 'bobo_response', None)
if bobo_response is not None:
yield bobo_response
return
resources = []
for resource in module.__dict__.itervalues():
bobo_response = getattr(resource, 'bobo_response', None)
if bobo_response is None:
continue
# Check for unbound handler and skip
if getattr(bobo_response, 'im_self', bobo_response) is None:
continue
order = getattr(resource, 'bobo_order', 0) or _late_base
resources.append((order, resource, bobo_response))
resources.sort()
by_route = {}
for order, resource, bobo_response in resources:
route = getattr(resource, 'bobo_route', None)
if route is not None:
methods = getattr(resource, 'bobo_methods', 0)
if methods != 0:
by_methods = by_route.get(route)
if not by_methods:
by_methods = by_route[route] = {}
yield _make_br_function_by_methods(route, by_methods)
if methods is None:
methods = (methods, )
for method in methods:
if method not in by_methods:
by_methods[method] = bobo_response
continue
yield bobo_response
def _make_br_function_by_methods(route, by_method):
route_data = _compile_route(route)
def bobo_response(request, path, method):
handler = by_method.get(method)
if handler is None:
handler = by_method.get(None)
if handler is None:
data = route_data(request, path)
if data is not None:
raise MethodNotAllowed(by_method)
return None
return handler(request, path, method)
return bobo_response
def _uncomment(config, name, split=False):
str = config.get(name, '')
result = filter(None, (
line.split('#', 1)[0].strip()
for line in str.strip().split('\n')
))
if split:
return result
return '\n'.join(result)
class _MultiResource(list):
def bobo_response(self, request, path, method):
for resource in self:
r = resource(request, path, method)
if r is not None:
return r
def resources(resources):
"""Create a resource from multiple resources
A new resource is returned that works by searching the given resources in
the order they're given.
"""
handlers = _MultiResource()
for resource in resources:
if isinstance(resource, basestring):
if ':' in resource:
resource = _get_global(resource)
else:
resource = _MultiResource(_scan_module(resource))
elif getattr(resource, 'bobo_response', None) is None:
resource = _MultiResource(_scan_module(resource.__name__))
handlers.append(resource.bobo_response)
return handlers
def reroute(route, resource):
"""Create a new resource from a re-routable resource.
The resource can be a string, in which case it should be a global
name, of the form ``module:expression``.
"""
if isinstance(resource, basestring):
resource = _get_global(resource)
try:
bobo_reroute = resource.bobo_reroute
except AttributeError:
import types
if isinstance(resource, (type, types.ClassType)):
return Subroute(route, resource)
raise TypeError("Expected a reroutable")
return bobo_reroute(route)
def preroute(route, resource):
"""Create a new resource by adding a route prefix
The given route is used as a subroute that is matched before
matching the given resource's route.
The resource can be a string, in which case it should be a global
name, of the form ``module:expression``, or a module name. If a
module name is given, and the module doesn't have a
bobo_response function, then a resource is computed that tries
each of the resources found in the module in order.
"""
if isinstance(resource, basestring):
if ':' in resource:
resource = _get_global(resource)
else:
resource = _MultiResource(_scan_module(resource))
elif getattr(resource, 'bobo_response', None) is None:
resource = _MultiResource(_scan_module(resource.__name__))
return Subroute(route, lambda request: resource)
_resource_re = re.compile('\s*([\S]+)\s*([-+]>)\s*(\S+)?\s*$').match
def _route_config(lines):
resources = []
lines.reverse()
while lines:
route = lines.pop()
m = _resource_re(route)
if m is None:
sep = resource = None
else:
route, sep, resource = m.groups()
if not resource:
if not sep:
# route is the resource.
if ':' in route:
resources.append(_get_global(route).bobo_response)
else:
resources.extend(_scan_module(route))
continue
else:
# line continuation
resource = lines.pop()
if sep == '->':
resource = reroute(route, resource)
else:
resource = preroute(route, resource)
resources.append(resource.bobo_response)
return resources
def _get_global(attr):
if ':' in attr:
mod, attr = attr.split(':', 1)
elif not mod:
raise ValueError("No ':' in global name", attr)
mod = _import(mod)
return eval(attr, mod.__dict__)
def _import(module_name):
return __import__(module_name, {}, {}, ['*'])
_order = 0
def order():
"""Return an integer that can be used to order a resource.
The function returns a larger integer each time it is called. A
resource can use this to set it's ``bobo_order`` attribute.
"""
global _order
_order += 1
return _order
_late_base = 1<<99
def late():
"""Return an order used for resources that should be searched late.
The function returns a larger integer each time it is called. The
value is larger than values returned by the order or early
functions.
"""
return order() + _late_base
_early_base = -_late_base
def early():
"""Return an order used for resources that should be searched early.
The function returns a larger integer each time it is called. The
value is smaller than values returned by the order or late
functions.
"""
return order() + _early_base
class _cached_property(object):
def __init__(self, func):
self.func = func
def __get__(self, inst, class_):
return self.func(inst)
_ext_re = re.compile('/(\w+)').search
class _Handler:
partial = False
def __init__(self, route, handler,
method=None, params=None, check=None, content_type=None,
order_=None):
if route is None:
route = '/'+handler.__name__
ext = _ext_re(content_type)
if ext:
route += '.'+ext.group(1)
self.bobo_route = route
if isinstance(method, basestring):
method = (method, )
self.bobo_methods = method
self.handler = handler
self.bobo_original = getattr(handler, 'bobo_original', handler)
bobo_sub_find = getattr(handler, 'bobo_response', None)
if bobo_sub_find is not None:
self.bobo_sub_find = bobo_sub_find
self.content_type = content_type
self.params = params
self.check = check
if order_ is None:
order_ = order()
self.bobo_order = order_
@_cached_property
def bobo_handle(self):
func = original = self.bobo_original
if self.params:
func = _make_caller(func, self.params)
func = _make_bobo_handle(func, original, self.check, self.content_type)
self.__dict__['bobo_handle'] = func
return func
@_cached_property
def match(self):
route_data = _compile_route(self.bobo_route, self.partial)
methods = self.bobo_methods
if methods is None:
return route_data
def match(request, path, method):
data = route_data(request, path)
if data is not None:
if method not in methods:
raise MethodNotAllowed(methods)
return data
self.__dict__['match'] = match
return match
def bobo_response(self, *args):
request, path, method = args[-3:]
route_data = self.match(request, path, method)
if route_data is None:
return self.bobo_sub_find(*args)
return self.bobo_handle(*args[:-2], **route_data)
def bobo_sub_find(self, *args):
pass
def __call__(self, *args, **kw):
return self.bobo_original(*args, **kw)
def __get__(self, inst, class_):
if inst is None:
return _UnboundHandler(self, class_)
return _BoundHandler(self, inst, class_)
@property
def func_code(self):
return self.bobo_original.func_code
@property
def func_defaults(self):
return self.bobo_original.func_defaults
@property
def __name__(self):
return self.bobo_original.__name__
def bobo_reroute(self, route):
return self.__class__(route, self.bobo_original, self.bobo_methods,
self.params, self.check, self.content_type)
class _UnboundHandler:
im_self = None
def __init__(self, handler, class_):
self.im_func = handler
self.im_class = class_
def __get__(self, inst, class_):
self._check_args(args)
if inst is None:
return self
return _BoundHandler(self.im_func, inst, self.im_class_)
def __repr__(self):
return "" % (
self.im_class.__name__,
self.im_func.__name__,
)
def _check_args(self, args):
if not args or not isinstance(args[0], self.im_class):
raise TypeError("Need %s initial argument"
% self.im_class.__name__)
def __call__(self, *args, **kw):
self._check_args(args)
return self.im_func(*args, **kw)
class _BoundHandler:
def __init__(self, handler, inst, class_):
if not isinstance(inst, class_):
raise TypeError("Can't bind", inst, class_)
self.im_func = handler
self.im_self = inst
self.im_class = class_
def __repr__(self):
return "" % (
self.im_class.__name__,
self.im_func.__name__,
self.im_self,
)
def bobo_response(self, *args):
return self.im_func.bobo_response(self.im_self, *args)
def __call__(self, *args, **kw):
return self.im_func(self.im_self, *args, **kw)
def _handler(route, func=None, **kw):
if func is None:
if route is None or isinstance(route, basestring):
return lambda f: _handler(route, f, **kw)
func = route
route = None
elif route is not None:
assert isinstance(route, basestring)
if route and not route.startswith('/'):
raise ValueError("Non-empty routes must start with '/'.", route)
return _Handler(route, func, **kw)
def resource(route=None, method=('GET', 'POST', 'HEAD'),
content_type=_default_content_type, check=None, order=None):
"""Create a resource
This function is used as a decorator to define a resource. It can be applied
to any kind of callable, not just a function.
Arguments:
route
The route to match against a request URL to determine
if the decorated callable should be used to satisfy a
request.
if omitted, a route will be computed using the decorated
callable's name with the content_type subtype used as an extension.
method
The HTTP request method or methods that can be used. This can be either
a string giving a single method name, or a sequence of strings.
content_type
The content_type for the response.
The content type is ignored if the callable returns a response object.
check
A check function.
If provided, the check function (or callable) will be called
before the decorated callable. The check function is passed
an instance, a request, and the decorated callable. If the
resource is a method, then first argument is the instance the
method was called on, otherwise it is None. If the check
function returns a response, the response will be used instead
of calling the decorated callable.
order
The order controls how resources are searched when matching
URLs. Normally, resources are searched in order of
evaluation. Passing the result of calling ``bobo.early`` or
``bobo.late`` can cause resources to be searched early or late.
The function may be used as a decorator directly without calling
it. For example::
@bobo.resource
def example(request):
...
is equivalent to::
@bobo.resource()
def example(request):
...
The callable must take a request object as the first argument. If the
route has placeholders, then the callable must accept named parameters
corresponding to the placeholders. The named parameters must have defaults
for any optional placeholders.
Unlike the post and query decorators, this decorator doesn't introspect the
callable it's applied to.
"""
return _handler(route, method=method, check=check,
content_type=content_type, order_=order)
def post(route=None, method=['POST', 'PUT'],
content_type=_default_content_type, check=None, order=None):
"""Create a resource that passes POST data as arguments
This function is used as a function decorator to define a resource.
Arguments:
route
The route to match against a request URL to determine
if the decorated callable should be used to satisfy a
request.
if omitted, a route will be computed using the decorated
callable's name with the content_type subtype used as an extension.
method
The HTTP request method or methods that can be used. This can
be either a string giving a single method name, or a sequence
of strings.
The method argument defaults to the string ``'POST'``.
content_type
The content_type for the response.
The content type is ignored if the callable returns a response object.
check
A check function.
If provided, the check function (or callable) will be called
before the decorated function. The check function is passed
an instance, a request, and the decorated function. If the
resource is a method, then first argument is the instance the
method was called on, otherwise it is None. If the check
function returns a response, the response will be used instead
of calling the decorated function.
order
The order controls how resources are searched when matching
URLs. Normally, resources are searched in order of
evaluation. Passing the result of calling ``bobo.early`` or
``bobo.late`` can cause resources to be searched early or late.
The function may be used as a decorator directly without calling
it. For example::
@bobo.post
def example():
...
is equivalent to::
@bobo.post()
def example():
...
The callable the decorator is applied to is analyzed to determine it's
signature. When the callable is called, the request, route data and
request form data are used to satisfy any named arguments in the callable's
signature. For example, in the case of::
@bobo.post('/:a')
def example(bobo_request, a, b, c=None):
...
when handling a request for: ``http://localhost/x``, with a post
body of ``b=1``, the request is passed to the ``bobo_request``
argument. the route data value ``'x'`` is passed to the argument
``a``, and the form data ``1`` is passed for ``b``.
Standard function metadata attributes ``func_code`` and ``func_defaults``
are used to determine the signature and required arguments. The method
attribute, ``im_func`` is used to determine if the callable is a method, in
which case the first argument found in the signature is ignored.
"""
return _handler(route, method=method, params='POST', check=check,
content_type=content_type, order_=order)
def query(route=None, method=('GET', 'POST', 'HEAD'),
content_type=_default_content_type, check=None, order=None):
"""Create a resource that passes form data as arguments
Create a decorator that, when applied to a callable, creates a
resource.
Arguments:
route
The route to match against a request URL to determine if the decorated
callable should be used to satisfy a request.
if omitted, a route will be computed using the decorated
callable's name with the content_type subtype used as an extension.
method
The HTTP request method or methods that can be used. This can
be either a string giving a single method name, or a sequence
of strings.
The method argument defaults to the tuple ``('GET', 'HEAD', 'POST')``.
content_type
The content_type for the response.
The content type is ignored if the callable returns a response object.
check
A check function.
If provided, the check function (or callable) will be called
before the decorated function. The check function is passed
an instance, a request, and the decorated function. If the
resource is a method, then first argument is the instance the
method was called on, otherwise it is None. If the check
function returns a response, the response will be used instead
of calling the decorated function.
order
The order controls how resources are searched when matching
URLs. Normally, resources are searched in order of
evaluation. Passing the result of calling ``bobo.early`` or
``bobo.late`` can cause resources to be searched early or late.
The function may be used as a decorator directly without calling
it. For example::
@bobo.query
def example():
...
is equivalent to::
@bobo.query()
def example():
...
The callable the decorator is applied to is analyzed to determine it's
signature. When the callable is called, the request, route data and
request form data are used to satisfy any named arguments in the callable's
signature. For example, in the case of::
@bobo.query('/:a')
def example(bobo_request, a, b, c=None):
...
when handling a request for: ``http://localhost/x?b=1``,
the request is passed to the ``bobo_request`` argument. the route
data value ``'x'`` is passed to the argument ``a``, and the form
data ``1`` is passed for ``b``.
Standard function metadata attributes ``func_code`` and
``func_defaults`` are used to determine the signature and required
arguments. The method attribute, ``im_func`` is used to determine
if the callable is a method, in which case the first argument found
in the signature is ignored.
"""
return _handler(route, method=method, params='params', check=check,
content_type=content_type, order_=order)
route_re = re.compile(r'(/:[a-zA-Z]\w*\??)(\.[^/]+)?')
def _compile_route(route, partial=False):
assert route.startswith('/') or not route
pat = route_re.split(route)
pat.reverse()
rpat = []
prefix = pat.pop()
if prefix:
rpat.append(re.escape(prefix))
while pat:
name = pat.pop()[2:]
optional = name.endswith('?')
if optional:
name = name[:-1]
name = '/(?P<%s>[^/]*)' % name
ext = pat.pop()
if ext:
name += re.escape(ext)
if optional:
name = '(%s)?' % name
rpat.append(name)
s = pat.pop()
if s:
rpat.append(re.escape(s))
if partial:
match = re.compile(''.join(rpat)).match
def route_data(request, path, method=None):
m = match(path)
if m is None:
return m
path = path[len(m.group(0)):]
return (dict(item for item in m.groupdict().iteritems()
if item[1] is not None),
path,
)
else:
match = re.compile(''.join(rpat)+'$').match
def route_data(request, path, method=None):
m = match(path)
if m is None:
return m
return dict(item for item in m.groupdict().iteritems()
if item[1] is not None)
return route_data
def _make_bobo_handle(func, original, check, content_type):
def handle(*args, **route):
if check is not None:
if len(args) == 1:
result = check(None, args[0], original)
else:
result = check(args[0], args[1], original)
if result is not None:
return result
result = func(*args, **route)
if hasattr(result, '__call__'):
return result
raise BoboException(200, result, content_type)
return handle
def _make_caller(obj, paramsattr):
wrapperCount = 0
unwrapped = obj
for i in range(10):
bases = getattr(unwrapped, '__bases__', None)
if bases is not None:
raise TypeError("mapply() can not call class constructors")
im_func = getattr(unwrapped, 'im_func', None)
if im_func is not None:
unwrapped = im_func
wrapperCount += 1
elif getattr(unwrapped, 'func_code', None) is not None:
break
else:
unwrapped = getattr(unwrapped, '__call__' , None)
if unwrapped is None:
raise TypeError("mapply() can not call %s" % repr(obj))
else:
raise TypeError("couldn't find callable metadata, mapply() error on %s"
% repr(obj))
code = unwrapped.func_code
defaults = unwrapped.func_defaults
names = code.co_varnames[wrapperCount:code.co_argcount]
nargs = len(names)
nrequired = len(names)
if defaults:
nrequired -= len(defaults)
# XXX maybe handle f(..., **kw)?
def bobo_apply(*pargs, **route):
request = pargs[-1]
pargs = pargs[:-1] # () or (self, )
params = getattr(request, paramsattr)
kw = {}
for index in range(len(pargs), nargs):
name = names[index]
if name == 'bobo_request':
kw[name] = request
continue
v = route.get(name)
if v is None:
v = params.getall(name)
if not v:
if index < nrequired:
raise MissingFormVariable(name)
continue
if len(v) == 1:
v = v[0]
kw[name] = v
return obj(*pargs, **kw)
return bobo_apply
class Subroute(_Handler):
partial = True
def __init__(self, route, handler):
_Handler.__init__(self, route, handler)
def bobo_response(self, *args):
request, path, method = args[-3:]
route_data = self.match(request, path)
if route_data is None:
return self.bobo_sub_find(*args)
route_data, path = route_data
resource = self.bobo_original(*args[:-2], **route_data)
if resource is not None:
return resource.bobo_response(request, path, method)
def bobo_reroute(self, route):
return self.__class__(route, self.bobo_original)
def _subroute(route, ob, scan):
if scan:
scan_class(ob)
return _subroute_class(route, ob)
import types
if isinstance(ob, (type, types.ClassType)):
return _subroute_class(route, ob)
return Subroute(route, ob)
def subroute(route=None, scan=False, order=None):
"""Create a resource that matches a URL in multiple steps
If called with a route or without any arguments, subroute returns
an object that should then be called with a resource factory. The
resource factory will be called with a request and route data and
should return a resource object. For example::
@subroute('/:employee_id', scan=True)
class EmployeeView:
def __init__(self, request, employee_id):
...
If no route is supplied, the ``__name__`` attribute of the callable
is used.
The resource factory may return None to indicate that a resource can't be
found on the subroute.
The scan argument, if given, should be given as a keyword
parameter. It defaults to False. If True, then the callable
should be a class and a ``bobo_response`` instance method will be
added to the class that calls resources found by scanning the
class and its base classes. Passing a True ``scan``
argument is equivalent to calling ``scan_class``::
@subroute('/:employee_id')
@scan_class
class EmployeeView:
def __init__(self, request, employee_id):
``subroute`` can be passed a callable directly, as in::
@subroute
class Employees:
def __init__(self, request):
...
def bobo_response(self, request, path, method):
...
Which is equivalent to calling ``subroute`` without the callable
and then passing the callable to the route::
@subroute()
class Employees:
def __init__(self, request):
...
def bobo_response(self, request, path, method):
...
Note that in the example above, the scan argument isn't passed and
defaults to False, so the class has to provide it's own
``bobo_response`` method (or otherwise arrange that instances have one).
The optional ``order`` parameter controls how resources are
searched when matching URLs. Normally, resources are searched in
order of evaluation. Passing the result of calling ``bobo.early``
or ``bobo.late`` can cause resources to be searched early or late.
It is usually a good idea to use ``bobo.late`` for subroutes that
match any URL.
"""
if route is None:
return lambda ob: _subroute('/'+ob.__name__, ob, scan)
if isinstance(route, basestring):
return lambda ob: _subroute(route, ob, scan)
return _subroute('/'+route.__name__, route, scan)
class _subroute_class_method(object):
def __init__(self, class_, class_func, inst_func):
self.class_ = class_
self.class_func = class_func
self.inst_func = inst_func
def __get__(self, inst, class_):
if inst is None:
return self.class_func.__get__(class_, type(class_))
inst_func = self.inst_func
if inst_func is None:
try:
return super(self.class_, inst).bobo_response
except TypeError:
raise AttributeError(
"%s instance has no attribute 'bobo_response'"
% inst.__class__.__name__)
return inst_func.__get__(inst, class_)
def _subroute_class(route, ob):
matchers = ob.__dict__.get('bobo_subroute_matchers', None)
if matchers is None:
matchers = ob.bobo_subroute_matchers = []
matchers.append(_compile_route(route, True))
br_orig = getattr(ob, 'bobo_response', None)
if br_orig is not None:
if br_orig.im_self is not None:
# we found another class method.
if len(matchers) > 1:
# stacked matchers, so we're done
return ob
if (('bobo_response' in ob.__dict__)
or not hasattr(ob, '__mro__')):
del ob.bobo_subroute_matchers
raise TypeError("bobo_response class method already defined")
# ok, it's inherited, we'll use super if necessary
br_orig = None
def bobo_response(self, request, path, method):
for matcher in matchers:
route_data = matcher(route, path)
if route_data:
route_data, path = route_data
resource = ob(request, **route_data)
if resource is not None:
return resource.bobo_response(request, path, method)
ob.bobo_response = _subroute_class_method(ob, bobo_response, br_orig)
return ob
def scan_class(class_):
"""Create an instance bobo_response method for a class
Scan a class (including its base classes) for resources and generate
a bobo_response method of the class that calls them.
"""
try:
mro = class_.__mro__
except AttributeError:
mro = type('C', (object, class_), {}).__mro__
resources = {}
for c in reversed(mro):
for name, resource in c.__dict__.iteritems():
br = getattr(resource, 'bobo_response', None)
if br is None:
continue
order = getattr(resource, 'bobo_order', 0) or _late_base
resources[name] = order, resource
by_route = {}
handlers = []
for (order, (name, resource)) in sorted(
(order, (name, resource))
for (name, (order, resource)) in resources.iteritems()
):
route = getattr(resource, 'bobo_route', None)
if route is not None:
methods = getattr(resource, 'bobo_methods', 0)
if methods != 0:
by_methods = by_route.get(route)
if not by_methods:
by_methods = by_route[route] = {}
handlers.append(
_make_br_method_by_methods(route, by_methods))
if methods is None:
methods = (methods, )
for method in methods:
if method not in by_methods:
by_methods[method] = name
continue
handlers.append(_make_br_method_for_name(name))
def bobo_response(self, request, path, method):
for handler in handlers:
found = handler(self, request, path, method)
if found is not None:
return found
old = class_.__dict__.get('bobo_response')
if isinstance(old, _subroute_class_method):
old.inst_func = bobo_response
else:
class_.bobo_response = bobo_response
return class_
def _make_br_method_for_name(name):
return (lambda self, request, path, method:
getattr(self, name).bobo_response(request, path, method)
)
def _make_br_method_by_methods(route, methods):
route_data = _compile_route(route)
def bobo_response(self, request, path, method):
name = methods.get(method)
if name is None:
name = methods.get(None)
if name is None:
data = route_data(request, path)
if data is not None:
raise MethodNotAllowed(methods)
return None
return getattr(self, name).bobo_response(request, path, method)
return bobo_response
class MissingFormVariable(Exception):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
class MethodNotAllowed(Exception):
def __init__(self, allowed):
self.allowed = sorted(allowed)
def __str__(self):
return "Allowed: %s" % repr(self.allowed)[1:-1]
class NotFound(Exception):
"""A resource cannot be found.
This exception can be conveniently raised by application
code. Bobo will catch it and generate a not-found response object.
"""
bobo-0.2.2/src/boboserver.py 0000644 0000765 0000765 00000017760 11325342322 014107 0 ustar jim jim ##############################################################################
#
# Copyright Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Create WSGI-based web applications.
"""
__all__ = (
'Debug',
'Reload',
'server',
'static',
)
__metaclass__ = type
import bobo
import optparse
import os
import mimetypes
import pdb
import re
import sys
import traceback
import types
import webob
def run_server(app, port):
wsgiref.simple_server.make_server('', port, app).serve_forever()
if sys.version_info >= (2, 5):
import wsgiref.simple_server
else:
# can't use wsgiref, use paste
import paste.httpserver
def run_server(app, port):
paste.httpserver.server_runner(app, {}, port=port)
class Directory:
def __init__(self, root, path=None):
self.root = os.path.abspath(root)+os.path.sep
self.path = path or root
@bobo.query('')
def base(self, bobo_request):
return bobo.redirect(bobo_request.url+'/')
@bobo.query('/')
def index(self):
links = []
for name in os.listdir(self.path):
if os.path.isdir(os.path.join(self.path, name)):
name += '/'
links.append('%s' % (name, name))
return """
%s
%s
""" % (self.path[len(self.root):], '
\n '.join(links))
@bobo.subroute('/:name')
def traverse(self, request, name):
path = os.path.abspath(os.path.join(self.path, name))
if not path.startswith(self.root):
raise bobo.NotFound
if os.path.isdir(path):
return Directory(self.root, path)
else:
return File(path)
bobo.scan_class(Directory)
class File:
def __init__(self, path):
self.path = path
@bobo.query('')
def base(self, bobo_request):
response = webob.Response()
content_type = mimetypes.guess_type(self.path)[0]
if content_type is not None:
response.content_type = content_type
try:
response.body = open(self.path, 'rb').read()
except IOError:
raise bobo.NotFound
return response
bobo.scan_class(File)
def static(route, directory):
"""Create a resource that serves static files from a directory
"""
return bobo.preroute(route, Directory(directory))
class Reload:
"""Module-reload middleware
This middleware can *only* be used with bobo applications. It
monitors a list of modules given by a ``modules`` keyword
parameter and configuration option. When a module changes, it
reloads the module and reinitializes the bobo application.
The Reload class implements the `Paste Deployment
filter_app_factory protocol
`_ and is
exported as a ``paste.filter_app_factory`` entry point named ``reload``.
"""
def __init__(self, app, default, modules):
if not isinstance(app, bobo.Application):
raise TypeError("Reload can only be used with bobo applications")
self.app = app
self.mtimes = mtimes = {}
for name in modules.split():
module = sys.modules[name]
mtimes[name] = (module.__file__, os.stat(module.__file__).st_mtime)
def __call__(self, environ, start_response):
for name, (path, mtime) in self.mtimes.iteritems():
if os.stat(path).st_mtime != mtime:
print 'Reloading', name
execfile(path, sys.modules[name].__dict__)
self.app.__init__(self.app.config)
self.mtimes[name] = path, os.stat(path).st_mtime
return self.app(environ, start_response)
class Debug:
"""Post-mortem debugging middleware
This middleware catches uncaught exceptions and runs the
``pdb.post_mortem`` debugging function, helping you to debug
exceptions raised by your application.
The Debug class implements the `Paste Deployment
filter_app_factory protocol
`_ and is
exported as a ``paste.filter_app_factory`` entry point named ``debug``.
"""
def __init__(self, app, default=None):
self.app = app
def __call__(self, environ, start_response):
try:
return self.app(environ, start_response)
except:
traceback.print_exception(*sys.exc_info())
pdb.post_mortem(sys.exc_info()[2])
raise
_mod_re = re.compile(
"(^|>) *(\w[a-zA-Z_.]*)(:|$)"
).search
def server(args=None, Application=bobo.Application):
"""Bobo development server
The server function implements the bobo development server.
It is exported as a ``console_script`` entry point named ``bobo``.
An alternate application can be passed in to run the server with a
different application implementation as long as application passed
in subclasses bobo.Application.
"""
if args is None:
import logging; logging.basicConfig()
args = sys.argv[1:]
usage = "%prog [options] name=value ..."
if sys.version_info >= (2, 5):
usage = 'Usage: ' + usage
parser = optparse.OptionParser(usage)
parser.add_option(
'--port', '-p', type='int', dest='port', default=8080,
help="Specify the port to listen on.")
parser.add_option(
'--file', '-f', dest='file', action='append',
help="Specify a source file to publish.")
parser.add_option(
'--resource', '-r', dest='resource', action='append',
help=("Specify a resource, such as a module or module global,"
" to publish."))
parser.add_option(
'--debug', '-D', action='store_true', dest='debug',
help="Run the post mortem debugger for uncaught exceptions.")
parser.add_option(
'-c', '--configure', dest='configure',
help="Specify the bobo_configure option.")
parser.add_option(
'-s', '--static', dest='static', action='append',
help=("Specify a route and directory (route=directory)"
" to serve statically"))
def error(message):
sys.stderr.write("Error:\n%s\n\n" % message)
parser.parse_args(['-h'])
options, pos = parser.parse_args(args)
resources = options.resource or []
mname = 'bobo__main__'
for path in options.file or ():
module = types.ModuleType(mname)
module.__file__ = path
execfile(module.__file__, module.__dict__)
sys.modules[module.__name__] = module
resources.append(module.__name__)
mname += '_'
for s in options.static or ():
route, path = s.split('=', 1)
resources.append("boboserver:static(%r,%r)" % (route, path))
if not resources:
error("No resources were specified.")
if [a for a in pos if '=' not in a]:
error("Positional arguments must be of the form name=value.")
app_options = dict(a.split('=', 1) for a in pos)
module_names = [m.group(2)
for m in map(_mod_re, resources)
if m is not None]
if options.configure:
if (':' not in options.configure) and module_names:
options.configure = module_names[0]+':'+options.configure
app_options['bobo_configure'] = options.configure
app = Application(app_options, bobo_resources='\n'.join(resources))
app = Reload(app, None, ' '.join(module_names))
if options.debug:
app = Debug(app)
print "Serving %s on port %s..." % (resources, options.port)
run_server(app, options.port)