You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
938 B
Python
42 lines
938 B
Python
"""Module for URI Template expansion."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from .expansions import ExpansionFailedError
|
|
from .uritemplate import ExpansionInvalidError, ExpansionReservedError, URITemplate
|
|
from .variable import Variable, VariableInvalidError
|
|
|
|
|
|
__all__ = (
|
|
'URITemplate',
|
|
'Variable',
|
|
'ExpansionInvalidError',
|
|
'ExpansionReservedError',
|
|
'VariableInvalidError',
|
|
'ExpansionFailedError',
|
|
)
|
|
|
|
|
|
def expand(template: str, **kwargs) -> (str | None):
|
|
try:
|
|
templ = URITemplate(template)
|
|
return templ.expand(**kwargs)
|
|
except Exception:
|
|
return None
|
|
|
|
|
|
def partial(template: str, **kwargs) -> (str | None):
|
|
try:
|
|
templ = URITemplate(template)
|
|
return str(templ.partial(**kwargs))
|
|
except Exception:
|
|
return None
|
|
|
|
|
|
def validate(template: str) -> bool:
|
|
try:
|
|
URITemplate(template)
|
|
return True
|
|
except Exception:
|
|
return False
|