From eb209e92c90d7d8e838356152f763f076148b737 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Tue, 22 May 2018 20:46:36 +0200 Subject: [PATCH] Move collections abc shim to _collections_compat --- lib/ansible/module_utils/basic.py | 21 +++++++------- .../common/_collections_compat.py | 29 +++++++++++++++++++ 2 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 lib/ansible/module_utils/common/_collections_compat.py diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 221f08c4fac..889a2a7fb34 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -81,8 +81,6 @@ import pwd import platform import errno import datetime -from collections import deque -from collections import Mapping, MutableMapping, Sequence, MutableSequence, Set, MutableSet from itertools import chain, repeat try: @@ -107,14 +105,6 @@ except ImportError: # Python2 & 3 way to get NoneType NoneType = type(None) -# Note: When getting Sequence from collections, it matches with strings. If -# this matters, make sure to check for strings before checking for sequencetype -try: - from collections.abc import KeysView - SEQUENCETYPE = (Sequence, frozenset, KeysView) -except ImportError: - SEQUENCETYPE = (Sequence, frozenset) - try: import json # Detect the python-json library which is incompatible @@ -162,6 +152,13 @@ except ImportError: except ImportError: pass +from ansible.module_utils.common._collections_compat import ( + deque, + KeysView, + Mapping, MutableMapping, + Sequence, MutableSequence, + Set, MutableSet, +) from ansible.module_utils.pycompat24 import get_exception, literal_eval from ansible.module_utils.six import ( PY2, @@ -178,6 +175,10 @@ from ansible.module_utils._text import to_native, to_bytes, to_text from ansible.module_utils.parsing.convert_bool import BOOLEANS_FALSE, BOOLEANS_TRUE, boolean +# Note: When getting Sequence from collections, it matches with strings. If +# this matters, make sure to check for strings before checking for sequencetype +SEQUENCETYPE = frozenset, KeysView, Sequence + PASSWORD_MATCH = re.compile(r'^(?:.+[-_\s])?pass(?:[-_\s]?(?:word|phrase|wrd|wd)?)(?:[-_\s].+)?$', re.I) _NUMBERTYPES = tuple(list(integer_types) + [float]) diff --git a/lib/ansible/module_utils/common/_collections_compat.py b/lib/ansible/module_utils/common/_collections_compat.py new file mode 100644 index 00000000000..3d1bbfc337c --- /dev/null +++ b/lib/ansible/module_utils/common/_collections_compat.py @@ -0,0 +1,29 @@ +# Copyright (c), Sviatoslav Sydorenko 2018 +# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause) +"""Collections ABC import shim. + +This module is intended only for internal use. +It will go away once the bundled copy of six includes equivalent functionality. +Third parties should not use this. +""" + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +try: + """Python 3.3+ branch.""" + from collections.abc import ( + deque, KeysView, + Mapping, MutableMapping, + Sequence, MutableSequence, + Set, MutableSet, + ) +except ImportError: + """Use old lib location under 2.6-3.2.""" + from collections import ( + deque, KeysView, + Mapping, MutableMapping, + Sequence, MutableSequence, + Set, MutableSet, + )