__init__.py 938 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """Module for URI Template expansion."""
  2. from __future__ import annotations
  3. from .expansions import ExpansionFailedError
  4. from .uritemplate import ExpansionInvalidError, ExpansionReservedError, URITemplate
  5. from .variable import Variable, VariableInvalidError
  6. __all__ = (
  7. 'URITemplate',
  8. 'Variable',
  9. 'ExpansionInvalidError',
  10. 'ExpansionReservedError',
  11. 'VariableInvalidError',
  12. 'ExpansionFailedError',
  13. )
  14. def expand(template: str, **kwargs) -> (str | None):
  15. try:
  16. templ = URITemplate(template)
  17. return templ.expand(**kwargs)
  18. except Exception:
  19. return None
  20. def partial(template: str, **kwargs) -> (str | None):
  21. try:
  22. templ = URITemplate(template)
  23. return str(templ.partial(**kwargs))
  24. except Exception:
  25. return None
  26. def validate(template: str) -> bool:
  27. try:
  28. URITemplate(template)
  29. return True
  30. except Exception:
  31. return False