betty.url package

Submodules

Module contents

Provide a URL generation API.

exception betty.url.GenerationError[source]

Bases: RuntimeError

A URL generation error.

exception betty.url.InvalidMediaType[source]

Bases: GenerationError

Raised when a URL generator cannot generate a URL for a resource with the given media type.

classmethod new(resource: Any, media_type: MediaType | None) Self[source]

Create a new instance.

class betty.url.LocalizedUrlGenerator[source]

Bases: _UrlGenerator

Generate URLs for localizable resources.

static __new__(*args, **kwargs)
abstractmethod generate(resource: Any, media_type: MediaType, *, absolute: bool = False, locale: Localey | None = None) str[source]

Generate a URL for a resource.

Raises:
class betty.url.PassthroughUrlGenerator[source]

Bases: UrlGenerator

Returns resources verbatim if they are absolute URLs already.

generate(resource: Any, *, media_type: MediaType | None = None, absolute: bool = False, locale: Localey | None = None) str[source]

Generate a URL for a resource.

Raises:
supports(resource: Any) bool[source]

Whether the given resource is supported by this URL generator.

class betty.url.StaticUrlGenerator[source]

Bases: _UrlGenerator

Generate URLs for static (non-localizable) resources.

static __new__(*args, **kwargs)
abstractmethod generate(resource: Any, *, absolute: bool = False) str[source]

Generate a static URL for a static resource.

Raises:
exception betty.url.UnsupportedResource[source]

Bases: GenerationError

Raised when a URL generator cannot generate a URL for a resource.

These are preventable by checking betty.url.LocalizedUrlGenerator.supports() or betty.url.StaticUrlGenerator.supports() first.

classmethod new(resource: Any) Self[source]

Create a new instance.

class betty.url.UrlGenerator[source]

Bases: _UrlGenerator

Generate URLs for resources.

abstractmethod generate(resource: Any, *, media_type: MediaType | None = None, absolute: bool = False, locale: Localey | None = None) str[source]

Generate a URL for a resource.

Raises:
betty.url.generate_from_path(path: str, *, base_url: str, root_path: str, locales: Mapping[str, str], clean_urls: bool, absolute: bool = False, locale: Localey | None = None) str[source]

Generate a full URL from a public path.