def encoded_header_len(self, s): """Return the length of the encoded header string.""" cset = self.get_output_charset() # The len(s) of a 7bit encoding is len(s) if self.header_encoding == BASE64: return base64MIME.base64_len(s) + len(cset) + MISC_LEN elif self.header_encoding == QP: return quopriMIME.header_quopri_len(s) + len(cset) + MISC_LEN elif self.header_encoding == SHORTEST: lenb64 = base64MIME.base64_len(s) lenqp = quopriMIME.header_quopri_len(s) return min(lenb64, lenqp) + len(cset) + MISC_LEN else: return len(s)
def header_encode(self, s, convert=False): """Header-encode a string, optionally converting it to output_charset. If convert is True, the string will be converted from the input charset to the output charset automatically. This is not useful for multibyte character sets, which have line length issues (multibyte characters must be split on a character, not a byte boundary); use the high-level Header class to deal with these issues. convert defaults to False. The type of encoding (base64 or quoted-printable) will be based on self.header_encoding. """ cset = self.get_output_charset() if convert: s = self.convert(s) # 7bit/8bit encodings return the string unchanged (modulo conversions) if self.header_encoding == BASE64: return base64MIME.header_encode(s, cset) elif self.header_encoding == QP: return quopriMIME.header_encode(s, cset, maxlinelen=None) elif self.header_encoding == SHORTEST: lenb64 = base64MIME.base64_len(s) lenqp = quopriMIME.header_quopri_len(s) if lenb64 < lenqp: return base64MIME.header_encode(s, cset) else: return quopriMIME.header_encode(s, cset, maxlinelen=None) else: return s