Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/wtforms/utils.py: 63%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

27 statements  

1import os 

2import re 

3 

4_LEADING_SYMBOL = "#" if os.name == "nt" else "-" 

5 

6# https://docs.python.org/3/library/datetime.html#technical-detail (see NOTE #9) 

7_DATETIME_STRIP_ZERO_PADDING_FORMATS_RE = re.compile( 

8 f"%{_LEADING_SYMBOL}[" 

9 "d" # day of month 

10 "m" # month 

11 "H" # hour (24-hour) 

12 "I" # hour (12-hour) 

13 "M" # minutes 

14 "S" # seconds 

15 "U" # week of year (Sunday first day of week) 

16 "W" # week of year (Monday first day of week) 

17 "V" # week of year (ISO 8601) 

18 "]", 

19 re.MULTILINE, 

20) 

21 

22 

23def clean_datetime_format_for_strptime(formats): 

24 """ 

25 Remove dashes used to disable zero-padding with strftime formats (for 

26 compatibility with strptime). 

27 """ 

28 return [ 

29 re.sub( 

30 _DATETIME_STRIP_ZERO_PADDING_FORMATS_RE, 

31 lambda m: m[0].replace(_LEADING_SYMBOL, ""), 

32 format, 

33 ) 

34 for format in formats 

35 ] 

36 

37 

38class UnsetValue: 

39 """ 

40 An unset value. 

41 

42 This is used in situations where a blank value like `None` is acceptable 

43 usually as the default value of a class variable or function parameter 

44 (iow, usually when `None` is a valid value.) 

45 """ 

46 

47 def __str__(self): 

48 return "<unset value>" 

49 

50 def __repr__(self): 

51 return "<unset value>" 

52 

53 def __bool__(self): 

54 return False 

55 

56 def __nonzero__(self): 

57 return False 

58 

59 

60unset_value = UnsetValue() 

61 

62 

63class WebobInputWrapper: 

64 """ 

65 Wrap a webob MultiDict for use as passing as `formdata` to Field. 

66 

67 Since for consistency, we have decided in WTForms to support as input a 

68 small subset of the API provided in common between cgi.FieldStorage, 

69 Django's QueryDict, and Werkzeug's MultiDict, we need to wrap Webob, the 

70 only supported framework whose multidict does not fit this API, but is 

71 nevertheless used by a lot of frameworks. 

72 

73 While we could write a full wrapper to support all the methods, this will 

74 undoubtedly result in bugs due to some subtle differences between the 

75 various wrappers. So we will keep it simple. 

76 """ 

77 

78 def __init__(self, multidict): 

79 self._wrapped = multidict 

80 

81 def __iter__(self): 

82 return iter(self._wrapped) 

83 

84 def __len__(self): 

85 return len(self._wrapped) 

86 

87 def __contains__(self, name): 

88 return name in self._wrapped 

89 

90 def getlist(self, name): 

91 return self._wrapped.getall(name)