comparison mercurial/hgweb/wsgiheaders.py @ 43076:2372284d9457

formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:45:02 -0400
parents 765a608c2108
children 687b865b95ad
comparison
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
10 # Regular expression that matches `special' characters in parameters, the 10 # Regular expression that matches `special' characters in parameters, the
11 # existence of which force quoting of the parameter value. 11 # existence of which force quoting of the parameter value.
12 from __future__ import absolute_import, print_function 12 from __future__ import absolute_import, print_function
13 13
14 import re 14 import re
15
15 tspecials = re.compile(br'[ \(\)<>@,;:\\"/\[\]\?=]') 16 tspecials = re.compile(br'[ \(\)<>@,;:\\"/\[\]\?=]')
17
16 18
17 def _formatparam(param, value=None, quote=1): 19 def _formatparam(param, value=None, quote=1):
18 """Convenience function to format and return a key=value pair. 20 """Convenience function to format and return a key=value pair.
19 This will quote the value if needed or if quote is true. 21 This will quote the value if needed or if quote is true.
20 """ 22 """
43 45
44 def _convert_string_type(self, value): 46 def _convert_string_type(self, value):
45 """Convert/check value type.""" 47 """Convert/check value type."""
46 if type(value) is bytes: 48 if type(value) is bytes:
47 return value 49 return value
48 raise AssertionError(u"Header names/values must be" 50 raise AssertionError(
49 u" of type bytes (got %s)" % repr(value)) 51 u"Header names/values must be"
52 u" of type bytes (got %s)" % repr(value)
53 )
50 54
51 def __len__(self): 55 def __len__(self):
52 """Return the total number of headers, including duplicates.""" 56 """Return the total number of headers, including duplicates."""
53 return len(self._headers) 57 return len(self._headers)
54 58
55 def __setitem__(self, name, val): 59 def __setitem__(self, name, val):
56 """Set the value of a header.""" 60 """Set the value of a header."""
57 del self[name] 61 del self[name]
58 self._headers.append( 62 self._headers.append(
59 (self._convert_string_type(name), self._convert_string_type(val))) 63 (self._convert_string_type(name), self._convert_string_type(val))
64 )
60 65
61 def __delitem__(self, name): 66 def __delitem__(self, name):
62 """Delete all occurrences of a header, if present. 67 """Delete all occurrences of a header, if present.
63 Does *not* raise an exception if the header is missing. 68 Does *not* raise an exception if the header is missing.
64 """ 69 """
76 81
77 def __contains__(self, name): 82 def __contains__(self, name):
78 """Return true if the message contains the header.""" 83 """Return true if the message contains the header."""
79 return self.get(name) is not None 84 return self.get(name) is not None
80 85
81
82 def get_all(self, name): 86 def get_all(self, name):
83 """Return a list of all the values for the named field. 87 """Return a list of all the values for the named field.
84 These will be sorted in the order they appeared in the original header 88 These will be sorted in the order they appeared in the original header
85 list or were added to this instance, and may contain duplicates. Any 89 list or were added to this instance, and may contain duplicates. Any
86 fields deleted and re-inserted are always appended to the header list. 90 fields deleted and re-inserted are always appended to the header list.
87 If no fields exist with the given name, returns an empty list. 91 If no fields exist with the given name, returns an empty list.
88 """ 92 """
89 name = self._convert_string_type(name.lower()) 93 name = self._convert_string_type(name.lower())
90 return [kv[1] for kv in self._headers if kv[0].lower()==name] 94 return [kv[1] for kv in self._headers if kv[0].lower() == name]
91
92 95
93 def get(self, name, default=None): 96 def get(self, name, default=None):
94 """Get the first header value for 'name', or return 'default'""" 97 """Get the first header value for 'name', or return 'default'"""
95 name = self._convert_string_type(name.lower()) 98 name = self._convert_string_type(name.lower())
96 for k, v in self._headers: 99 for k, v in self._headers:
97 if k.lower()==name: 100 if k.lower() == name:
98 return v 101 return v
99 return default 102 return default
100
101 103
102 def keys(self): 104 def keys(self):
103 """Return a list of all the header field names. 105 """Return a list of all the header field names.
104 These will be sorted in the order they appeared in the original header 106 These will be sorted in the order they appeared in the original header
105 list, or were added to this instance, and may contain duplicates. 107 list, or were added to this instance, and may contain duplicates.
130 return r"%s(%r)" % (self.__class__.__name__, self._headers) 132 return r"%s(%r)" % (self.__class__.__name__, self._headers)
131 133
132 def __str__(self): 134 def __str__(self):
133 """str() returns the formatted headers, complete with end line, 135 """str() returns the formatted headers, complete with end line,
134 suitable for direct HTTP transmission.""" 136 suitable for direct HTTP transmission."""
135 return '\r\n'.join(["%s: %s" % kv for kv in self._headers]+['','']) 137 return '\r\n'.join(["%s: %s" % kv for kv in self._headers] + ['', ''])
136 138
137 def __bytes__(self): 139 def __bytes__(self):
138 return str(self).encode('iso-8859-1') 140 return str(self).encode('iso-8859-1')
139 141
140 def setdefault(self, name, value): 142 def setdefault(self, name, value):
141 """Return first matching header value for 'name', or 'value' 143 """Return first matching header value for 'name', or 'value'
142 If there is no header named 'name', add a new header with name 'name' 144 If there is no header named 'name', add a new header with name 'name'
143 and value 'value'.""" 145 and value 'value'."""
144 result = self.get(name) 146 result = self.get(name)
145 if result is None: 147 if result is None:
146 self._headers.append((self._convert_string_type(name), 148 self._headers.append(
147 self._convert_string_type(value))) 149 (
150 self._convert_string_type(name),
151 self._convert_string_type(value),
152 )
153 )
148 return value 154 return value
149 else: 155 else:
150 return result 156 return result
151 157
152 def add_header(self, _name, _value, **_params): 158 def add_header(self, _name, _value, **_params):
171 parts.append(k.replace('_', '-')) 177 parts.append(k.replace('_', '-'))
172 else: 178 else:
173 v = self._convert_string_type(v) 179 v = self._convert_string_type(v)
174 parts.append(_formatparam(k.replace('_', '-'), v)) 180 parts.append(_formatparam(k.replace('_', '-'), v))
175 self._headers.append( 181 self._headers.append(
176 (self._convert_string_type(_name), "; ".join(parts))) 182 (self._convert_string_type(_name), "; ".join(parts))
183 )