Create a MIME-compliant header that can contain many character sets. Optional s is the initial header value. If None, the initial header value is not set. You can later append to the header with .append() method calls. s may be a byte string or a Unicode string, but see t
(self, s=None, charset=None,
maxlinelen=None, header_name=None,
continuation_ws=' ', errors='strict')
| 183 | |
| 184 | class Header: |
| 185 | def __init__(self, s=None, charset=None, |
| 186 | maxlinelen=None, header_name=None, |
| 187 | continuation_ws=' ', errors='strict'): |
| 188 | """Create a MIME-compliant header that can contain many character sets. |
| 189 | |
| 190 | Optional s is the initial header value. If None, the initial header |
| 191 | value is not set. You can later append to the header with .append() |
| 192 | method calls. s may be a byte string or a Unicode string, but see the |
| 193 | .append() documentation for semantics. |
| 194 | |
| 195 | Optional charset serves two purposes: it has the same meaning as the |
| 196 | charset argument to the .append() method. It also sets the default |
| 197 | character set for all subsequent .append() calls that omit the charset |
| 198 | argument. If charset is not provided in the constructor, the us-ascii |
| 199 | charset is used both as s's initial charset and as the default for |
| 200 | subsequent .append() calls. |
| 201 | |
| 202 | The maximum line length can be specified explicitly via maxlinelen. For |
| 203 | splitting the first line to a shorter value (to account for the field |
| 204 | header which isn't included in s, e.g. 'Subject') pass in the name of |
| 205 | the field in header_name. The default maxlinelen is 78 as recommended |
| 206 | by RFC 2822. |
| 207 | |
| 208 | continuation_ws must be RFC 2822 compliant folding whitespace (usually |
| 209 | either a space or a hard tab) which will be prepended to continuation |
| 210 | lines. |
| 211 | |
| 212 | errors is passed through to the .append() call. |
| 213 | """ |
| 214 | if charset is None: |
| 215 | charset = USASCII |
| 216 | elif not isinstance(charset, Charset): |
| 217 | charset = Charset(charset) |
| 218 | self._charset = charset |
| 219 | self._continuation_ws = continuation_ws |
| 220 | self._chunks = [] |
| 221 | if s is not None: |
| 222 | self.append(s, charset, errors) |
| 223 | if maxlinelen is None: |
| 224 | maxlinelen = MAXLINELEN |
| 225 | self._maxlinelen = maxlinelen |
| 226 | if header_name is None: |
| 227 | self._headerlen = 0 |
| 228 | else: |
| 229 | # Take the separating colon and space into account. |
| 230 | self._headerlen = len(header_name) + 2 |
| 231 | |
| 232 | def __str__(self): |
| 233 | """Return the string value of the header.""" |