utils.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import re
  2. # https://docs.python.org/3/library/datetime.html#technical-detail (see NOTE #9)
  3. _DATETIME_STRIP_ZERO_PADDING_FORMATS_RE = re.compile(
  4. "%-["
  5. "d" # day of month
  6. "m" # month
  7. "H" # hour (24-hour)
  8. "I" # hour (12-hour)
  9. "M" # minutes
  10. "S" # seconds
  11. "U" # week of year (Sunday first day of week)
  12. "W" # week of year (Monday first day of week)
  13. "V" # week of year (ISO 8601)
  14. "]",
  15. re.MULTILINE,
  16. )
  17. def clean_datetime_format_for_strptime(formats):
  18. """
  19. Remove dashes used to disable zero-padding with strftime formats (for
  20. compatibility with strptime).
  21. """
  22. return [
  23. re.sub(
  24. _DATETIME_STRIP_ZERO_PADDING_FORMATS_RE,
  25. lambda m: m[0].replace("-", ""),
  26. format,
  27. )
  28. for format in formats
  29. ]
  30. class UnsetValue:
  31. """
  32. An unset value.
  33. This is used in situations where a blank value like `None` is acceptable
  34. usually as the default value of a class variable or function parameter
  35. (iow, usually when `None` is a valid value.)
  36. """
  37. def __str__(self):
  38. return "<unset value>"
  39. def __repr__(self):
  40. return "<unset value>"
  41. def __bool__(self):
  42. return False
  43. def __nonzero__(self):
  44. return False
  45. unset_value = UnsetValue()
  46. class WebobInputWrapper:
  47. """
  48. Wrap a webob MultiDict for use as passing as `formdata` to Field.
  49. Since for consistency, we have decided in WTForms to support as input a
  50. small subset of the API provided in common between cgi.FieldStorage,
  51. Django's QueryDict, and Werkzeug's MultiDict, we need to wrap Webob, the
  52. only supported framework whose multidict does not fit this API, but is
  53. nevertheless used by a lot of frameworks.
  54. While we could write a full wrapper to support all the methods, this will
  55. undoubtedly result in bugs due to some subtle differences between the
  56. various wrappers. So we will keep it simple.
  57. """
  58. def __init__(self, multidict):
  59. self._wrapped = multidict
  60. def __iter__(self):
  61. return iter(self._wrapped)
  62. def __len__(self):
  63. return len(self._wrapped)
  64. def __contains__(self, name):
  65. return name in self._wrapped
  66. def getlist(self, name):
  67. return self._wrapped.getall(name)