utils.py 2.3 KB

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