Skip to content

Commit 910567b

Browse files
authored
Avoid six. (#177)
1 parent 2f8326d commit 910567b

19 files changed

+104
-26
lines changed

changelogs/fragments/177-six.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- "Avoid using ``ansible.module_utils.six`` to avoid deprecation warnings with ansible-core 2.20 (https://github.com/ansible-collections/community.hrobot/pull/177)."

plugins/module_utils/api.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
from __future__ import absolute_import, division, print_function
88
__metaclass__ = type
99

10+
import sys
1011

1112
from ansible.module_utils.common.text.converters import to_native
12-
from ansible.module_utils.six import PY3
13-
from ansible.module_utils.six.moves.urllib.parse import urlencode
1413
from ansible.module_utils.urls import fetch_url
1514

1615
import time
@@ -19,6 +18,12 @@
1918
CheckDoneTimeoutException,
2019
)
2120

21+
try:
22+
from urllib.parse import urlencode
23+
except ImportError:
24+
# Python 2.x fallback:
25+
from urllib import urlencode
26+
2227

2328
API_DEFAULT_ARGUMENT_SPEC = dict(
2429
hetzner_token=dict(type='str', required=True, no_log=True),
@@ -75,7 +80,7 @@ def raw_api_fetch_url_json(
7580
try:
7681
# In Python 2, reading from a closed response yields a TypeError.
7782
# In Python 3, read() simply returns ''
78-
if PY3 and resp.closed:
83+
if sys.version_info[0] > 2 and resp.closed:
7984
raise TypeError
8085
content = resp.read()
8186
except (AttributeError, TypeError):

plugins/module_utils/failover.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
from __future__ import absolute_import, division, print_function
88
__metaclass__ = type
99

10-
1110
import json
1211

13-
from ansible.module_utils.six.moves.urllib.parse import urlencode
14-
1512
from ansible_collections.community.hrobot.plugins.module_utils.robot import (
1613
BASE_URL,
1714
fetch_url_json,
1815
)
1916

17+
try:
18+
from urllib.parse import urlencode
19+
except ImportError:
20+
# Python 2.x fallback:
21+
from urllib import urlencode
22+
2023

2124
def get_failover_record(module, ip):
2225
'''

plugins/module_utils/robot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
from __future__ import absolute_import, division, print_function
88
__metaclass__ = type
99

10+
import sys
1011

1112
from ansible.module_utils.common.text.converters import to_native
12-
from ansible.module_utils.six import PY3
1313
from ansible.module_utils.six.moves.urllib.error import HTTPError
1414
from ansible.module_utils.urls import fetch_url, open_url
1515

@@ -151,7 +151,7 @@ def raw_fetch_url_json(module, url, method='GET', timeout=10, data=None, headers
151151
try:
152152
# In Python 2, reading from a closed response yields a TypeError.
153153
# In Python 3, read() simply returns ''
154-
if PY3 and resp.closed:
154+
if sys.version_info[0] > 2 and resp.closed:
155155
raise TypeError
156156
content = resp.read()
157157
except (AttributeError, TypeError):

plugins/modules/boot.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@
276276
"""
277277

278278
from ansible.module_utils.basic import AnsibleModule
279-
from ansible.module_utils.six.moves.urllib.parse import urlencode
280279

281280
from ansible_collections.community.hrobot.plugins.module_utils.robot import (
282281
BASE_URL,
@@ -289,6 +288,12 @@
289288
extract_fingerprint,
290289
)
291290

291+
try:
292+
from urllib.parse import urlencode
293+
except ImportError:
294+
# Python 2.x fallback:
295+
from urllib import urlencode
296+
292297

293298
BOOT_CONFIGURATION_DATA = [
294299
('rescue', 'rescue', {

plugins/modules/firewall.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,6 @@
442442
fetch_url_json,
443443
fetch_url_json_with_retries,
444444
)
445-
from ansible.module_utils.six.moves.urllib.parse import urlencode
446445
from ansible.module_utils.common.text.converters import to_native, to_text
447446

448447
try:
@@ -453,6 +452,12 @@
453452
IPADDRESS_IMP_ERR = traceback.format_exc()
454453
HAS_IPADDRESS = False
455454

455+
try:
456+
from urllib.parse import urlencode
457+
except ImportError:
458+
# Python 2.x fallback:
459+
from urllib import urlencode
460+
456461

457462
RULE_OPTION_NAMES = [
458463
'name', 'ip_version', 'dst_ip', 'dst_port', 'src_ip', 'src_port',

plugins/modules/reset.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,19 @@
9191
RETURN = r"""#"""
9292

9393
from ansible.module_utils.basic import AnsibleModule
94-
from ansible.module_utils.six.moves.urllib.parse import urlencode
9594

9695
from ansible_collections.community.hrobot.plugins.module_utils.robot import (
9796
BASE_URL,
9897
ROBOT_DEFAULT_ARGUMENT_SPEC,
9998
fetch_url_json,
10099
)
101100

101+
try:
102+
from urllib.parse import urlencode
103+
except ImportError:
104+
# Python 2.x fallback:
105+
from urllib import urlencode
106+
102107

103108
def main():
104109
argument_spec = dict(

plugins/modules/reverse_dns.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,19 @@
7676
RETURN = r"""#"""
7777

7878
from ansible.module_utils.basic import AnsibleModule
79-
from ansible.module_utils.six.moves.urllib.parse import urlencode
8079

8180
from ansible_collections.community.hrobot.plugins.module_utils.robot import (
8281
BASE_URL,
8382
ROBOT_DEFAULT_ARGUMENT_SPEC,
8483
fetch_url_json,
8584
)
8685

86+
try:
87+
from urllib.parse import urlencode
88+
except ImportError:
89+
# Python 2.x fallback:
90+
from urllib import urlencode
91+
8792

8893
def main():
8994
argument_spec = dict(

plugins/modules/server.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,19 @@
212212
"""
213213

214214
from ansible.module_utils.basic import AnsibleModule
215-
from ansible.module_utils.six.moves.urllib.parse import urlencode
216215

217216
from ansible_collections.community.hrobot.plugins.module_utils.robot import (
218217
BASE_URL,
219218
ROBOT_DEFAULT_ARGUMENT_SPEC,
220219
fetch_url_json,
221220
)
222221

222+
try:
223+
from urllib.parse import urlencode
224+
except ImportError:
225+
# Python 2.x fallback:
226+
from urllib import urlencode
227+
223228

224229
def main():
225230
argument_spec = dict(

plugins/modules/ssh_key.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@
9696

9797
from ansible.module_utils.basic import AnsibleModule
9898
from ansible.module_utils.common.text.converters import to_native
99-
from ansible.module_utils.six.moves.urllib.parse import urlencode
10099

101100
from ansible_collections.community.hrobot.plugins.module_utils.robot import (
102101
BASE_URL,
@@ -111,6 +110,12 @@
111110
remove_comment,
112111
)
113112

113+
try:
114+
from urllib.parse import urlencode
115+
except ImportError:
116+
# Python 2.x fallback:
117+
from urllib import urlencode
118+
114119

115120
def main():
116121
argument_spec = dict(

0 commit comments

Comments
 (0)