Each Bottle object represents a single, distinct web application and consists of routes, callbacks, plugins, resources and configuration. Instances are callable WSGI applications. :param catchall: If true (default), handle all exceptions. Turn off to
| 578 | |
| 579 | |
| 580 | class Bottle(object): |
| 581 | """ Each Bottle object represents a single, distinct web application and |
| 582 | consists of routes, callbacks, plugins, resources and configuration. |
| 583 | Instances are callable WSGI applications. |
| 584 | |
| 585 | :param catchall: If true (default), handle all exceptions. Turn off to |
| 586 | let debugging middleware handle exceptions. |
| 587 | """ |
| 588 | |
| 589 | def __init__(self, catchall=True, autojson=True): |
| 590 | |
| 591 | #: A :class:`ConfigDict` for app specific configuration. |
| 592 | self.config = ConfigDict() |
| 593 | self.config._on_change = functools.partial(self.trigger_hook, 'config') |
| 594 | self.config.meta_set('autojson', 'validate', bool) |
| 595 | self.config.meta_set('catchall', 'validate', bool) |
| 596 | self.config['catchall'] = catchall |
| 597 | self.config['autojson'] = autojson |
| 598 | |
| 599 | #: A :class:`ResourceManager` for application files |
| 600 | self.resources = ResourceManager() |
| 601 | |
| 602 | self.routes = [] # List of installed :class:`Route` instances. |
| 603 | self.router = Router() # Maps requests to :class:`Route` instances. |
| 604 | self.error_handler = {} |
| 605 | |
| 606 | # Core plugins |
| 607 | self.plugins = [] # List of installed plugins. |
| 608 | if self.config['autojson']: |
| 609 | self.install(JSONPlugin()) |
| 610 | self.install(TemplatePlugin()) |
| 611 | |
| 612 | #: If true, most exceptions are caught and returned as :exc:`HTTPError` |
| 613 | catchall = DictProperty('config', 'catchall') |
| 614 | |
| 615 | __hook_names = 'before_request', 'after_request', 'app_reset', 'config' |
| 616 | __hook_reversed = 'after_request' |
| 617 | |
| 618 | @cached_property |
| 619 | def _hooks(self): |
| 620 | return dict((name, []) for name in self.__hook_names) |
| 621 | |
| 622 | def add_hook(self, name, func): |
| 623 | ''' Attach a callback to a hook. Three hooks are currently implemented: |
| 624 | |
| 625 | before_request |
| 626 | Executed once before each request. The request context is |
| 627 | available, but no routing has happened yet. |
| 628 | after_request |
| 629 | Executed once after each request regardless of its outcome. |
| 630 | app_reset |
| 631 | Called whenever :meth:`Bottle.reset` is called. |
| 632 | ''' |
| 633 | if name in self.__hook_reversed: |
| 634 | self._hooks[name].insert(0, func) |
| 635 | else: |
| 636 | self._hooks[name].append(func) |
| 637 |