converters.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import re
  2. import typing as t
  3. import uuid
  4. from ..urls import _fast_url_quote
  5. if t.TYPE_CHECKING:
  6. from .map import Map
  7. class ValidationError(ValueError):
  8. """Validation error. If a rule converter raises this exception the rule
  9. does not match the current URL and the next URL is tried.
  10. """
  11. class BaseConverter:
  12. """Base class for all converters."""
  13. regex = "[^/]+"
  14. weight = 100
  15. part_isolating = True
  16. def __init__(self, map: "Map", *args: t.Any, **kwargs: t.Any) -> None:
  17. self.map = map
  18. def to_python(self, value: str) -> t.Any:
  19. return value
  20. def to_url(self, value: t.Any) -> str:
  21. if isinstance(value, (bytes, bytearray)):
  22. return _fast_url_quote(value)
  23. return _fast_url_quote(str(value).encode(self.map.charset))
  24. class UnicodeConverter(BaseConverter):
  25. """This converter is the default converter and accepts any string but
  26. only one path segment. Thus the string can not include a slash.
  27. This is the default validator.
  28. Example::
  29. Rule('/pages/<page>'),
  30. Rule('/<string(length=2):lang_code>')
  31. :param map: the :class:`Map`.
  32. :param minlength: the minimum length of the string. Must be greater
  33. or equal 1.
  34. :param maxlength: the maximum length of the string.
  35. :param length: the exact length of the string.
  36. """
  37. part_isolating = True
  38. def __init__(
  39. self,
  40. map: "Map",
  41. minlength: int = 1,
  42. maxlength: t.Optional[int] = None,
  43. length: t.Optional[int] = None,
  44. ) -> None:
  45. super().__init__(map)
  46. if length is not None:
  47. length_regex = f"{{{int(length)}}}"
  48. else:
  49. if maxlength is None:
  50. maxlength_value = ""
  51. else:
  52. maxlength_value = str(int(maxlength))
  53. length_regex = f"{{{int(minlength)},{maxlength_value}}}"
  54. self.regex = f"[^/]{length_regex}"
  55. class AnyConverter(BaseConverter):
  56. """Matches one of the items provided. Items can either be Python
  57. identifiers or strings::
  58. Rule('/<any(about, help, imprint, class, "foo,bar"):page_name>')
  59. :param map: the :class:`Map`.
  60. :param items: this function accepts the possible items as positional
  61. arguments.
  62. .. versionchanged:: 2.2
  63. Value is validated when building a URL.
  64. """
  65. part_isolating = True
  66. def __init__(self, map: "Map", *items: str) -> None:
  67. super().__init__(map)
  68. self.items = set(items)
  69. self.regex = f"(?:{'|'.join([re.escape(x) for x in items])})"
  70. def to_url(self, value: t.Any) -> str:
  71. if value in self.items:
  72. return str(value)
  73. valid_values = ", ".join(f"'{item}'" for item in sorted(self.items))
  74. raise ValueError(f"'{value}' is not one of {valid_values}")
  75. class PathConverter(BaseConverter):
  76. """Like the default :class:`UnicodeConverter`, but it also matches
  77. slashes. This is useful for wikis and similar applications::
  78. Rule('/<path:wikipage>')
  79. Rule('/<path:wikipage>/edit')
  80. :param map: the :class:`Map`.
  81. """
  82. regex = "[^/].*?"
  83. weight = 200
  84. part_isolating = False
  85. class NumberConverter(BaseConverter):
  86. """Baseclass for `IntegerConverter` and `FloatConverter`.
  87. :internal:
  88. """
  89. weight = 50
  90. num_convert: t.Callable = int
  91. part_isolating = True
  92. def __init__(
  93. self,
  94. map: "Map",
  95. fixed_digits: int = 0,
  96. min: t.Optional[int] = None,
  97. max: t.Optional[int] = None,
  98. signed: bool = False,
  99. ) -> None:
  100. if signed:
  101. self.regex = self.signed_regex
  102. super().__init__(map)
  103. self.fixed_digits = fixed_digits
  104. self.min = min
  105. self.max = max
  106. self.signed = signed
  107. def to_python(self, value: str) -> t.Any:
  108. if self.fixed_digits and len(value) != self.fixed_digits:
  109. raise ValidationError()
  110. value = self.num_convert(value)
  111. if (self.min is not None and value < self.min) or (
  112. self.max is not None and value > self.max
  113. ):
  114. raise ValidationError()
  115. return value
  116. def to_url(self, value: t.Any) -> str:
  117. value = str(self.num_convert(value))
  118. if self.fixed_digits:
  119. value = value.zfill(self.fixed_digits)
  120. return value
  121. @property
  122. def signed_regex(self) -> str:
  123. return f"-?{self.regex}"
  124. class IntegerConverter(NumberConverter):
  125. """This converter only accepts integer values::
  126. Rule("/page/<int:page>")
  127. By default it only accepts unsigned, positive values. The ``signed``
  128. parameter will enable signed, negative values. ::
  129. Rule("/page/<int(signed=True):page>")
  130. :param map: The :class:`Map`.
  131. :param fixed_digits: The number of fixed digits in the URL. If you
  132. set this to ``4`` for example, the rule will only match if the
  133. URL looks like ``/0001/``. The default is variable length.
  134. :param min: The minimal value.
  135. :param max: The maximal value.
  136. :param signed: Allow signed (negative) values.
  137. .. versionadded:: 0.15
  138. The ``signed`` parameter.
  139. """
  140. regex = r"\d+"
  141. part_isolating = True
  142. class FloatConverter(NumberConverter):
  143. """This converter only accepts floating point values::
  144. Rule("/probability/<float:probability>")
  145. By default it only accepts unsigned, positive values. The ``signed``
  146. parameter will enable signed, negative values. ::
  147. Rule("/offset/<float(signed=True):offset>")
  148. :param map: The :class:`Map`.
  149. :param min: The minimal value.
  150. :param max: The maximal value.
  151. :param signed: Allow signed (negative) values.
  152. .. versionadded:: 0.15
  153. The ``signed`` parameter.
  154. """
  155. regex = r"\d+\.\d+"
  156. num_convert = float
  157. part_isolating = True
  158. def __init__(
  159. self,
  160. map: "Map",
  161. min: t.Optional[float] = None,
  162. max: t.Optional[float] = None,
  163. signed: bool = False,
  164. ) -> None:
  165. super().__init__(map, min=min, max=max, signed=signed) # type: ignore
  166. class UUIDConverter(BaseConverter):
  167. """This converter only accepts UUID strings::
  168. Rule('/object/<uuid:identifier>')
  169. .. versionadded:: 0.10
  170. :param map: the :class:`Map`.
  171. """
  172. regex = (
  173. r"[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-"
  174. r"[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}"
  175. )
  176. part_isolating = True
  177. def to_python(self, value: str) -> uuid.UUID:
  178. return uuid.UUID(value)
  179. def to_url(self, value: uuid.UUID) -> str:
  180. return str(value)
  181. #: the default converter mapping for the map.
  182. DEFAULT_CONVERTERS: t.Mapping[str, t.Type[BaseConverter]] = {
  183. "default": UnicodeConverter,
  184. "string": UnicodeConverter,
  185. "any": AnyConverter,
  186. "path": PathConverter,
  187. "int": IntegerConverter,
  188. "float": FloatConverter,
  189. "uuid": UUIDConverter,
  190. }