/usr/lib/python2.7/dist-packages/cyclone/bottle.py is in python-cyclone 1.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | # coding: utf-8
#
# Copyright 2010 Alexandre Fiori
# based on the original Tornado by Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Support for Bootle application style.
http://bottlepy.com
For more information see the bottle demo:
https://github.com/fiorix/cyclone/tree/master/demos/bottle
"""
import cyclone.web
import functools
import sys
from twisted.python import log
from twisted.internet import reactor
_handlers = []
_BaseHandler = None
class Router:
def __init__(self):
self.items = []
def add(self, method, callback):
self.items.append((method, callback))
def __call__(self, *args, **kwargs):
obj = _BaseHandler(*args, **kwargs)
for (method, callback) in self.items:
callback = functools.partial(callback, obj)
setattr(obj, method.lower(), callback)
return obj
def route(path=None, method="GET", callback=None, **kwargs):
"""Use this decorator to route requests to the handler.
Example::
@route("/")
def index(cli):
cli.write("Hello, world")
@route("/foobar", method="post")
def whatever(cli):
...
"""
if callable(path):
path, callback = None, path
def decorator(callback):
_handlers.append((path, method.lower(), callback, kwargs))
return callback
return decorator
def create_app(**settings):
"""Return an application which will serve the bottle-defined routes.
Parameters:
base_handler: The class or factory for request handlers. The default
is cyclone.web.RequestHandler.
more_handlers: A regular list of tuples containing regex -> handler
All other parameters are passed directly to the `cyclone.web.Application`
constructor.
"""
global _handlers, _BaseHandler
_BaseHandler = settings.pop("base_handler", cyclone.web.RequestHandler)
handlers = {}
for (path, method, callback, kwargs) in _handlers:
if path not in handlers:
handlers[path] = Router()
handlers[path].add(method, callback)
_handlers = None
handlers = handlers.items() + settings.pop("more_handlers", [])
return cyclone.web.Application(handlers, **settings)
def run(**settings):
"""Start the application.
Parameters:
host: Interface to listen on. [default: 0.0.0.0]
port: TCP port to listen on. [default: 8888]
log: The log file to use, the default is sys.stdout.
base_handler: The class or factory for request handlers. The default
is cyclone.web.RequestHandler.
more_handlers: A regular list of tuples containing regex -> handler
All other parameters are passed directly to the `cyclone.web.Application`
constructor.
"""
port = settings.get("port", 8888)
interface = settings.get("host", "0.0.0.0")
log.startLogging(settings.pop("log", sys.stdout))
reactor.listenTCP(port, create_app(**settings), interface=interface)
reactor.run()
|