Convenience function to format and return a key=value pair. This will quote the value if needed or if quote is true. If value is a three tuple (charset, language, value), it will be encoded according to RFC2231 rules. If it contains non-ascii characters it will likewise be encoded
(param, value=None, quote=True)
| 37 | return a.strip(), b.strip() |
| 38 | |
| 39 | def _formatparam(param, value=None, quote=True): |
| 40 | """Convenience function to format and return a key=value pair. |
| 41 | |
| 42 | This will quote the value if needed or if quote is true. If value is a |
| 43 | three tuple (charset, language, value), it will be encoded according |
| 44 | to RFC2231 rules. If it contains non-ascii characters it will likewise |
| 45 | be encoded according to RFC2231 rules, using the utf-8 charset and |
| 46 | a null language. |
| 47 | """ |
| 48 | if value is not None and len(value) > 0: |
| 49 | # A tuple is used for RFC 2231 encoded parameter values where items |
| 50 | # are (charset, language, value). charset is a string, not a Charset |
| 51 | # instance. RFC 2231 encoded values are never quoted, per RFC. |
| 52 | if isinstance(value, tuple): |
| 53 | # Encode as per RFC 2231 |
| 54 | param += '*' |
| 55 | value = utils.encode_rfc2231(value[2], value[0], value[1]) |
| 56 | return '%s=%s' % (param, value) |
| 57 | else: |
| 58 | try: |
| 59 | value.encode('ascii') |
| 60 | except UnicodeEncodeError: |
| 61 | param += '*' |
| 62 | value = utils.encode_rfc2231(value, 'utf-8', '') |
| 63 | return '%s=%s' % (param, value) |
| 64 | # BAW: Please check this. I think that if quote is set it should |
| 65 | # force quoting even if not necessary. |
| 66 | if quote or tspecials.search(value): |
| 67 | return '%s="%s"' % (param, utils.quote(value)) |
| 68 | else: |
| 69 | return '%s=%s' % (param, value) |
| 70 | else: |
| 71 | return param |
| 72 | |
| 73 | def _parseparam(s): |
| 74 | # RDM This might be a Header, so for now stringify it. |