Pyjoyment.Routes.Route - Route

from Pyjoyment.Routes.Route import Route

r = Route()

# Or import the module, as in Google Python Style Guide

import Pyjoyment.Routes.Route

r = Pyjoyment.Routes.Route.new()

Pyjoyment.Routes.Route is the route container used by Pyjoyment.Routes.

Shortcuts

In addition to the attributes and methods below you can also call shortcuts provided by root on Pyjoyment.Routes.Route objects.

# Add a "firefox" shortcut
r.root.add_shortcut('firefox', lambda r, path: r.get(path).requires(agent=re.compile(r'Firefox')))

# Use "firefox" shortcut to generate routes
r.firefox('/welcome').to('firefox#welcome')
r.firefox('/bye').to('firefox#bye')

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojolicious::Routes::Route of Mojolicious 9.52.

class Pyjoyment.Routes.Route.Pyjoyment_Routes_Route[source]

Pyjoyment.Routes.Route inherits all methods from Pyjo.Base and implements the following new ones.

add_child(route: Pyjoyment_Routes_Route) → Self[source]
r = r.add_child(Pyjoyment.Routes.Route.new())

Add a child to this route, it will be automatically removed from its current parent if necessary.

# Reattach route
r.add_child(r.find('foo'))
any(*args: Any, **constraints: Constraint) → Pyjoyment_Routes_Route[source]
route = r.any()
route = r.any('/:foo')
route = r.any('/:foo', lambda c: ...)
route = r.any('/:foo', lambda c: ..., 'name')
route = r.any('/:foo', {'foo': 'bar'}, lambda c: ...)
route = r.any('/:foo', lambda c: ..., foo=re.compile(r'\w+'))
route = r.any(['GET', 'POST'], '/:foo', lambda c: ...)
route = r.any(['GET', 'POST'], '/:foo', foo=re.compile(r'\w+'))

Generate Pyjoyment.Routes.Route object matching any of the listed HTTP request methods or all.

# Route with pattern and destination
r.any('/user').to('user#whatever')

All arguments are optional, but some have to appear in a certain order, like the list of the HTTP methods to match, which comes first, and the restrictive placeholders, which are keyword arguments.

# Route with HTTP methods, pattern, restrictive placeholders and destination
r.any(['DELETE', 'PUT'], '/:foo', foo=re.compile(r'\w+')).to('foo#bar')

There are also two supported string values, containing the route pattern and the route name, defaulting to the pattern / and a name based on the pattern.

# Route with pattern, name and destination
r.any('/:foo', 'foo_route').to('foo#bar')

Route conditions are activated with requires().

# Route with pattern, condition and destination
r.any('/').requires(agent=re.compile(r'Firefox')).to('foo#bar')

A dict is used to specify optional placeholders and default values for the stash.

# Route with pattern, optional placeholder and destination
r.any('/:foo', {'foo': 'bar'}).to('foo#bar')

And a callable can be used to specify a cb value to be merged into the default values for the stash.

# Route with pattern and a closure as destination
r.any('/:foo', lambda c: c.render(text='Hello World!'))
children: list[Pyjoyment_Routes_Route]
children = r.children
r.children = [Pyjoyment.Routes.Route.new()]

The children of this route, used for nesting routes.

delete(*args: Any, **constraints: Constraint) → Pyjoyment_Routes_Route[source]
route = r.delete()
route = r.delete('/:foo')
route = r.delete('/:foo', lambda c: ...)
route = r.delete('/:foo', lambda c: ..., 'name')
route = r.delete('/:foo', {'foo': 'bar'}, lambda c: ...)
route = r.delete('/:foo', lambda c: ..., foo=re.compile(r'\w+'))

Generate Pyjoyment.Routes.Route object matching only DELETE requests, takes the same arguments as any() (except for the HTTP methods to match, which are implied).

# Route with destination
r.delete('/user').to('user#remove')
find(name: str) → Pyjoyment_Routes_Route | None[source]
route = r.find('foo')

Find child route by name, custom names have precedence over automatically generated ones.

# Change default parameters of a named route
r.find('show_user').to(foo='bar')
get(*args: Any, **constraints: Constraint) → Pyjoyment_Routes_Route[source]
route = r.get()
route = r.get('/:foo')
route = r.get('/:foo', lambda c: ...)
route = r.get('/:foo', lambda c: ..., 'name')
route = r.get('/:foo', {'foo': 'bar'}, lambda c: ...)
route = r.get('/:foo', lambda c: ..., foo=re.compile(r'\w+'))

Generate Pyjoyment.Routes.Route object matching only GET requests, takes the same arguments as any() (except for the HTTP methods to match, which are implied).

# Route with destination
r.get('/user').to('user#show')
property has_custom_name: bool[source]
boolean = r.has_custom_name

Check if this route has a custom name.

property has_websocket: bool[source]
boolean = r.has_websocket

Check if this route has a WebSocket ancestor and cache the result for future checks.

inline: bool | None
boolean = r.inline
r.inline = boolean

Allow under() semantics for this route.

property is_endpoint: bool[source]
boolean = r.is_endpoint

Check if this route qualifies as an endpoint.

is_reserved(name: str) → bool[source]
boolean = r.is_reserved('controller')

Check if string is a reserved stash value.

property is_websocket: bool[source]
boolean = r.is_websocket

Check if this route is a WebSocket.

methods() → list[str] | None[source]
methods(method: str | list[str], /, *methods: str) → Self
methods = r.methods()
r = r.methods('GET')
r = r.methods('GET', 'POST')
r = r.methods(['GET', 'POST'])

Restrict HTTP methods this route is allowed to handle, defaults to no restrictions.

# Route with two methods and destination
r.any('/foo').methods('GET', 'POST').to('foo#bar')
property name: str | None[source]
name = r.name
r.name = 'foo'

The name of this route, defaults to an automatically generated name based on the route pattern. Note that the name current is reserved for referring to the current route.

# Route with destination and custom name
r.get('/user').to('user#show').set(name='show_user')
options(*args: Any, **constraints: Constraint) → Pyjoyment_Routes_Route[source]
route = r.options()
route = r.options('/:foo')
route = r.options('/:foo', lambda c: ...)
route = r.options('/:foo', lambda c: ..., 'name')
route = r.options('/:foo', {'foo': 'bar'}, lambda c: ...)
route = r.options('/:foo', lambda c: ..., foo=re.compile(r'\w+'))

Generate Pyjoyment.Routes.Route object matching only OPTIONS requests, takes the same arguments as any() (except for the HTTP methods to match, which are implied).

# Route with destination
r.options('/user').to('user#overview')
property parent: Pyjoyment_Routes_Route | None[source]
parent = r.parent
r.parent = Pyjoyment.Routes.Route.new()

The parent of this route, usually a Pyjoyment.Routes.Route object. Note that this attribute is weakened.

parse(pattern: str | None = None, /, **constraints: Constraint) → Self[source]
r = r.parse('/user/:id')
r = r.parse('/user/:id', id=re.compile(r'\d+'))
r = r.parse(format=['json', 'yaml'])

Parse pattern.

partial: bool | None
boolean = r.partial
r.partial = boolean

Route has no specific end, remaining characters will be captured in path.

patch(*args: Any, **constraints: Constraint) → Pyjoyment_Routes_Route[source]
route = r.patch()
route = r.patch('/:foo')
route = r.patch('/:foo', lambda c: ...)
route = r.patch('/:foo', lambda c: ..., 'name')
route = r.patch('/:foo', {'foo': 'bar'}, lambda c: ...)
route = r.patch('/:foo', lambda c: ..., foo=re.compile(r'\w+'))

Generate Pyjoyment.Routes.Route object matching only PATCH requests, takes the same arguments as any() (except for the HTTP methods to match, which are implied).

# Route with destination
r.patch('/user').to('user#update')
pattern: Pyjoyment_Routes_Pattern
pattern = r.pattern
r.pattern = Pyjoyment.Routes.Pattern.new()

Pattern for this route, defaults to a Pyjoyment.Routes.Pattern object.

post(*args: Any, **constraints: Constraint) → Pyjoyment_Routes_Route[source]
route = r.post()
route = r.post('/:foo')
route = r.post('/:foo', lambda c: ...)
route = r.post('/:foo', lambda c: ..., 'name')
route = r.post('/:foo', {'foo': 'bar'}, lambda c: ...)
route = r.post('/:foo', lambda c: ..., foo=re.compile(r'\w+'))

Generate Pyjoyment.Routes.Route object matching only POST requests, takes the same arguments as any() (except for the HTTP methods to match, which are implied).

# Route with destination
r.post('/user').to('user#create')
put(*args: Any, **constraints: Constraint) → Pyjoyment_Routes_Route[source]
route = r.put()
route = r.put('/:foo')
route = r.put('/:foo', lambda c: ...)
route = r.put('/:foo', lambda c: ..., 'name')
route = r.put('/:foo', {'foo': 'bar'}, lambda c: ...)
route = r.put('/:foo', lambda c: ..., foo=re.compile(r'\w+'))

Generate Pyjoyment.Routes.Route object matching only PUT requests, takes the same arguments as any() (except for the HTTP methods to match, which are implied).

# Route with destination
r.put('/user').to('user#replace')
query(*args: Any, **constraints: Constraint) → Pyjoyment_Routes_Route[source]
route = r.query()
route = r.query('/:foo')
route = r.query('/:foo', lambda c: ...)
route = r.query('/:foo', lambda c: ..., 'name')
route = r.query('/:foo', {'foo': 'bar'}, lambda c: ...)
route = r.query('/:foo', lambda c: ..., foo=re.compile(r'\w+'))

Generate Pyjoyment.Routes.Route object matching only QUERY requests, takes the same arguments as any() (except for the HTTP methods to match, which are implied).

# Route with destination
r.query('/user').to('user#search')
remove() → Self[source]
r = r.remove()

Remove route from parent.

# Remove route completely
r.find('foo').remove()

# Reattach route to new parent
r.any('/foo').add_child(r.find('bar').remove())
render(values: dict[str, Any]) → str[source]
path = r.render({'foo': 'bar'})

Render route with parameters into a path.

requires() → list[tuple[str, Any]] | None[source]
requires(conditions: list[tuple[str, Any]], /) → Self
requires(**conditions: Any) → Self
requires = r.requires()
r = r.requires(foo=1)
r = r.requires(foo=1, bar={'baz': 'yada'})
r = r.requires([('foo', 1), ('bar', {'baz': 'yada'})])

Activate conditions for this route. Note that this automatically disables the routing cache, since conditions are too complex for caching.

# Route with condition and destination
r.get('/foo').requires(host=re.compile(r'mojolicious\.org')).to('foo#bar')
property root: Any[source]
root = r.root

The Pyjoyment.Routes object this route is a descendant of.

property suggested_method: str[source]
method = r.suggested_method

Suggested HTTP method for reaching this route, GET and POST are preferred.

to() → dict[str, Any][source]
to(shortcut: Any, /, *args: Any, **defaults: Any) → Self
to(**defaults: Any) → Self
defaults = r.to()
r = r.to(action='foo')
r = r.to({'action': 'foo'})
r = r.to('controller#action')
r = r.to('controller#action', foo='bar')
r = r.to('controller#action', {'foo': 'bar'})
r = r.to(Pyjoyment.new())
r = r.to(Pyjoyment.new(), foo='bar')
r = r.to(Pyjoyment.new(), {'foo': 'bar'})
r = r.to('MyApp')
r = r.to('MyApp', foo='bar')
r = r.to('MyApp', {'foo': 'bar'})

Set default parameters for this route.

to_str() → str[source]
string = r.to_str()

Stringify the whole route.

under(*args: Any, **constraints: Constraint) → Pyjoyment_Routes_Route[source]
route = r.under(lambda c: ...)
route = r.under('/:foo', lambda c: ...)
route = r.under('/:foo', {'foo': 'bar'})
route = r.under('/:foo', foo=re.compile(r'\w+'))
route = r.under(format=['json', 'yaml'])

Generate Pyjoyment.Routes.Route object for a nested route with its own intermediate destination, takes the same arguments as any() (except for the HTTP methods to match, which are not available).

# Longer version
r.any('/:foo', lambda c: ...).set(inline=True)

# Intermediate destination and prefix shared between two routes
auth = r.under('/user').to('user#auth')
auth.get('/show').to('#show')
auth.post('/create').to('#create')
websocket(*args: Any, **constraints: Constraint) → Pyjoyment_Routes_Route[source]
route = r.websocket()
route = r.websocket('/:foo')
route = r.websocket('/:foo', lambda c: ...)
route = r.websocket('/:foo', lambda c: ..., 'name')
route = r.websocket('/:foo', {'foo': 'bar'}, lambda c: ...)
route = r.websocket('/:foo', lambda c: ..., foo=re.compile(r'\w+'))

Generate Pyjoyment.Routes.Route object matching only WebSocket handshakes, takes the same arguments as any() (except for the HTTP methods to match, which are implied).

# Route with destination
r.websocket('/echo').to('example#echo')