This class wraps a route callback along with route specific metadata and configuration and applies Plugins on demand. It is also responsible for turing an URL path rule into a regular expression usable by the Router.
| 459 | |
| 460 | |
| 461 | class Route(object): |
| 462 | ''' This class wraps a route callback along with route specific metadata and |
| 463 | configuration and applies Plugins on demand. It is also responsible for |
| 464 | turing an URL path rule into a regular expression usable by the Router. |
| 465 | ''' |
| 466 | |
| 467 | def __init__(self, app, rule, method, callback, name=None, |
| 468 | plugins=None, skiplist=None, **config): |
| 469 | #: The application this route is installed to. |
| 470 | self.app = app |
| 471 | #: The path-rule string (e.g. ``/wiki/:page``). |
| 472 | self.rule = rule |
| 473 | #: The HTTP method as a string (e.g. ``GET``). |
| 474 | self.method = method |
| 475 | #: The original callback with no plugins applied. Useful for introspection. |
| 476 | self.callback = callback |
| 477 | #: The name of the route (if specified) or ``None``. |
| 478 | self.name = name or None |
| 479 | #: A list of route-specific plugins (see :meth:`Bottle.route`). |
| 480 | self.plugins = plugins or [] |
| 481 | #: A list of plugins to not apply to this route (see :meth:`Bottle.route`). |
| 482 | self.skiplist = skiplist or [] |
| 483 | #: Additional keyword arguments passed to the :meth:`Bottle.route` |
| 484 | #: decorator are stored in this dictionary. Used for route-specific |
| 485 | #: plugin configuration and meta-data. |
| 486 | self.config = ConfigDict().load_dict(config, make_namespaces=True) |
| 487 | |
| 488 | def __call__(self, *a, **ka): |
| 489 | depr("Some APIs changed to return Route() instances instead of"\ |
| 490 | " callables. Make sure to use the Route.call method and not to"\ |
| 491 | " call Route instances directly.") #0.12 |
| 492 | return self.call(*a, **ka) |
| 493 | |
| 494 | @cached_property |
| 495 | def call(self): |
| 496 | ''' The route callback with all plugins applied. This property is |
| 497 | created on demand and then cached to speed up subsequent requests.''' |
| 498 | return self._make_callback() |
| 499 | |
| 500 | def reset(self): |
| 501 | ''' Forget any cached values. The next time :attr:`call` is accessed, |
| 502 | all plugins are re-applied. ''' |
| 503 | self.__dict__.pop('call', None) |
| 504 | |
| 505 | def prepare(self): |
| 506 | ''' Do all on-demand work immediately (useful for debugging).''' |
| 507 | self.call |
| 508 | |
| 509 | @property |
| 510 | def _context(self): |
| 511 | depr('Switch to Plugin API v2 and access the Route object directly.') #0.12 |
| 512 | return dict(rule=self.rule, method=self.method, callback=self.callback, |
| 513 | name=self.name, app=self.app, config=self.config, |
| 514 | apply=self.plugins, skip=self.skiplist) |
| 515 | |
| 516 | def all_plugins(self): |
| 517 | ''' Yield all Plugins affecting this route. ''' |
| 518 | unique = set() |