|
6 | 6 |
|
7 | 7 | from __future__ import absolute_import, division, print_function |
8 | 8 |
|
| 9 | +# This module requires Python 3.8+ (secrets, f-strings with =, os.replace, json.JSONDecodeError). This should be fine since it will always run on localhost and the Ansible Controller has to be Python 3.9+ anyway |
| 10 | + |
9 | 11 | import email.encoders |
10 | 12 | import email.mime.application |
11 | 13 | import email.mime.multipart |
12 | 14 | import email.mime.nonmultipart |
13 | 15 | import email.parser |
14 | | -import email.utils |
| 16 | +import email.policy |
15 | 17 | import json |
16 | 18 | import mimetypes |
17 | 19 | import os |
|
20 | 22 | from urllib.error import HTTPError, URLError |
21 | 23 |
|
22 | 24 | from ansible.module_utils.common.collections import Mapping |
23 | | -from ansible.module_utils.six import PY2, PY3, string_types |
24 | | -from ansible.module_utils.six.moves import cStringIO |
25 | | - |
26 | | -try: |
27 | | - import email.policy |
28 | | -except ImportError: |
29 | | - # Py2 |
30 | | - import email.generator |
| 25 | +from ansible.module_utils.six import string_types |
31 | 26 |
|
32 | 27 | from ansible.module_utils._text import to_bytes, to_native, to_text |
33 | 28 | from ansible.module_utils.urls import (ConnectionError, SSLValidationError, |
@@ -120,30 +115,15 @@ def prepare_multipart_no_base64(fields): |
120 | 115 |
|
121 | 116 | m.attach(part) |
122 | 117 |
|
123 | | - if PY3: |
124 | | - # Ensure headers are not split over multiple lines |
125 | | - # The HTTP policy also uses CRLF by default |
126 | | - b_data = m.as_bytes(policy=email.policy.HTTP) |
127 | | - else: |
128 | | - # Py2 |
129 | | - # We cannot just call ``as_string`` since it provides no way |
130 | | - # to specify ``maxheaderlen`` |
131 | | - fp = cStringIO() # cStringIO seems to be required here |
132 | | - # Ensure headers are not split over multiple lines |
133 | | - g = email.generator.Generator(fp, maxheaderlen=0) |
134 | | - g.flatten(m) |
135 | | - # ``fix_eols`` switches from ``\n`` to ``\r\n`` |
136 | | - b_data = email.utils.fix_eols(fp.getvalue()) |
| 118 | + # Ensure headers are not split over multiple lines |
| 119 | + # The HTTP policy also uses CRLF by default |
| 120 | + b_data = m.as_bytes(policy=email.policy.HTTP) |
137 | 121 | del m |
138 | 122 |
|
139 | 123 | headers, sep, b_content = b_data.partition(b'\r\n\r\n') |
140 | 124 | del b_data |
141 | 125 |
|
142 | | - if PY3: |
143 | | - parser = email.parser.BytesHeaderParser().parsebytes |
144 | | - else: |
145 | | - # Py2 |
146 | | - parser = email.parser.HeaderParser().parsestr |
| 126 | + parser = email.parser.BytesHeaderParser().parsebytes |
147 | 127 |
|
148 | 128 | return ( |
149 | 129 | parser(headers)['content-type'], # Message converts to native strings |
|
0 commit comments