mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 13:22:48 +00:00
Migrate most uses of if type() to if isinstance()
Also convert those checks to use abcs instead of dict and list. Make a sentinel class for strategies to report when they've reache the end
This commit is contained in:
@@ -19,8 +19,7 @@
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.plugins.callback.default import CallbackModule as CallbackModule_default
|
||||
from ansible.utils.color import colorize, hostcolor
|
||||
from collections import MutableMapping, MutableSequence
|
||||
|
||||
HAS_OD = False
|
||||
try:
|
||||
@@ -29,6 +28,10 @@ try:
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
from ansible.module_utils.six import binary_type, text_type
|
||||
from ansible.plugins.callback.default import CallbackModule as CallbackModule_default
|
||||
from ansible.utils.color import colorize, hostcolor
|
||||
|
||||
try:
|
||||
from __main__ import display
|
||||
except ImportError:
|
||||
@@ -235,7 +238,7 @@ class CallbackModule_dense(CallbackModule_default):
|
||||
|
||||
# Remove empty attributes (list, dict, str)
|
||||
for attr in result.copy():
|
||||
if type(result[attr]) in (list, dict, basestring, unicode):
|
||||
if isinstance(result[attr], (MutableSequence, MutableMapping, binary_type, text_type)):
|
||||
if not result[attr]:
|
||||
del(result[attr])
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ __metaclass__ = type
|
||||
import os
|
||||
import time
|
||||
import json
|
||||
from collections import MutableMapping
|
||||
|
||||
from ansible.module_utils._text import to_bytes
|
||||
from ansible.plugins.callback import CallbackBase
|
||||
@@ -54,7 +55,7 @@ class CallbackModule(CallbackBase):
|
||||
os.makedirs("/var/log/ansible/hosts")
|
||||
|
||||
def log(self, host, category, data):
|
||||
if type(data) == dict:
|
||||
if isinstance(data, MutableMapping):
|
||||
if '_ansible_verbose_override' in data:
|
||||
# avoid logging extraneous data
|
||||
data = 'omitted'
|
||||
|
||||
@@ -31,6 +31,7 @@ import re
|
||||
import string
|
||||
import sys
|
||||
import uuid
|
||||
from collections import MutableMapping, MutableSequence
|
||||
from datetime import datetime
|
||||
from functools import partial
|
||||
from random import Random, SystemRandom, shuffle
|
||||
@@ -108,14 +109,13 @@ def to_nice_json(a, indent=4, *args, **kw):
|
||||
|
||||
def to_bool(a):
|
||||
''' return a bool for the arg '''
|
||||
if a is None or type(a) == bool:
|
||||
if a is None or isinstance(a, bool):
|
||||
return a
|
||||
if isinstance(a, string_types):
|
||||
a = a.lower()
|
||||
if a in ['yes', 'on', '1', 'true', 1]:
|
||||
if a in ('yes', 'on', '1', 'true', 1):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
return False
|
||||
|
||||
def to_datetime(string, format="%Y-%d-%m %H:%M:%S"):
|
||||
return datetime.strptime(string, format)
|
||||
@@ -402,10 +402,10 @@ def extract(item, container, morekeys=None):
|
||||
def failed(*a, **kw):
|
||||
''' Test if task result yields failed '''
|
||||
item = a[0]
|
||||
if type(item) != dict:
|
||||
if not isinstance(item, MutableMapping):
|
||||
raise errors.AnsibleFilterError("|failed expects a dictionary")
|
||||
rc = item.get('rc',0)
|
||||
failed = item.get('failed',False)
|
||||
rc = item.get('rc', 0)
|
||||
failed = item.get('failed', False)
|
||||
if rc != 0 or failed:
|
||||
return True
|
||||
else:
|
||||
@@ -418,13 +418,13 @@ def success(*a, **kw):
|
||||
def changed(*a, **kw):
|
||||
''' Test if task result yields changed '''
|
||||
item = a[0]
|
||||
if type(item) != dict:
|
||||
if not isinstance(item, MutableMapping):
|
||||
raise errors.AnsibleFilterError("|changed expects a dictionary")
|
||||
if not 'changed' in item:
|
||||
changed = False
|
||||
if ('results' in item # some modules return a 'results' key
|
||||
and type(item['results']) == list
|
||||
and type(item['results'][0]) == dict):
|
||||
and isinstance(item['results'], MutableSequence)
|
||||
and isinstance(item['results'][0], MutableMapping)):
|
||||
for result in item['results']:
|
||||
changed = changed or result.get('changed', False)
|
||||
else:
|
||||
@@ -434,7 +434,7 @@ def changed(*a, **kw):
|
||||
def skipped(*a, **kw):
|
||||
''' Test if task result yields skipped '''
|
||||
item = a[0]
|
||||
if type(item) != dict:
|
||||
if not isinstance(item, MutableMapping):
|
||||
raise errors.AnsibleFilterError("|skipped expects a dictionary")
|
||||
skipped = item.get('skipped', False)
|
||||
return skipped
|
||||
|
||||
@@ -19,6 +19,7 @@ __metaclass__ = type
|
||||
|
||||
import codecs
|
||||
import csv
|
||||
from collections import MutableSequence
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.plugins.lookup import LookupBase
|
||||
@@ -102,7 +103,7 @@ class LookupModule(LookupBase):
|
||||
lookupfile = self.find_file_in_search_path(variables, 'files', paramvals['file'])
|
||||
var = self.read_csv(lookupfile, key, paramvals['delimiter'], paramvals['encoding'], paramvals['default'], paramvals['col'])
|
||||
if var is not None:
|
||||
if type(var) is list:
|
||||
if isinstance(var, MutableSequence):
|
||||
for v in var:
|
||||
ret.append(v)
|
||||
else:
|
||||
|
||||
@@ -22,11 +22,12 @@ from ansible.plugins.lookup import LookupBase
|
||||
import socket
|
||||
|
||||
try:
|
||||
import dns.exception
|
||||
import dns.name
|
||||
import dns.resolver
|
||||
import dns.reversename
|
||||
from dns.rdatatype import (A, AAAA, CNAME, DLV, DNAME, DNSKEY, DS, HINFO, LOC,
|
||||
MX, NAPTR, NS, NSEC3PARAM, PTR, RP, SOA, SPF, SRV, SSHFP, TLSA, TXT)
|
||||
import dns.exception
|
||||
HAVE_DNS = True
|
||||
except ImportError:
|
||||
HAVE_DNS = False
|
||||
@@ -70,7 +71,7 @@ def make_rdata_dict(rdata):
|
||||
for f in fields:
|
||||
val = rdata.__getattribute__(f)
|
||||
|
||||
if type(val) == dns.name.Name:
|
||||
if isinstance(val, dns.name.Name):
|
||||
val = dns.name.Name.to_text(val)
|
||||
|
||||
if rdata.rdtype == DLV and f == 'digest':
|
||||
|
||||
@@ -17,9 +17,10 @@
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from io import StringIO
|
||||
import os
|
||||
import re
|
||||
from collections import MutableSequence
|
||||
from io import StringIO
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.module_utils.six.moves import configparser
|
||||
@@ -110,7 +111,7 @@ class LookupModule(LookupBase):
|
||||
else:
|
||||
var = self.read_ini(path, key, paramvals['section'], paramvals['default'], paramvals['re'])
|
||||
if var is not None:
|
||||
if type(var) is list:
|
||||
if isinstance(var, MutableSequence):
|
||||
for v in var:
|
||||
ret.append(v)
|
||||
else:
|
||||
|
||||
@@ -53,6 +53,9 @@ except ImportError:
|
||||
|
||||
__all__ = ['StrategyBase']
|
||||
|
||||
class StrategySentinel:
|
||||
pass
|
||||
|
||||
# TODO: this should probably be in the plugins/__init__.py, with
|
||||
# a smarter mechanism to set all of the attributes based on
|
||||
# the loaders created there
|
||||
@@ -70,12 +73,12 @@ class SharedPluginLoaderObj:
|
||||
self.module_loader = module_loader
|
||||
|
||||
|
||||
_sentinel = object()
|
||||
_sentinel = StrategySentinel()
|
||||
def results_thread_main(strategy):
|
||||
while True:
|
||||
try:
|
||||
result = strategy._final_q.get()
|
||||
if type(result) == object:
|
||||
if isinstance(result, StrategySentinel):
|
||||
break
|
||||
else:
|
||||
strategy._results_lock.acquire()
|
||||
|
||||
@@ -21,6 +21,7 @@ __metaclass__ = type
|
||||
|
||||
import re
|
||||
import operator as py_operator
|
||||
from collections import MutableMapping, MutableSequence
|
||||
from distutils.version import LooseVersion, StrictVersion
|
||||
|
||||
from ansible import errors
|
||||
@@ -28,7 +29,7 @@ from ansible import errors
|
||||
def failed(*a, **kw):
|
||||
''' Test if task result yields failed '''
|
||||
item = a[0]
|
||||
if type(item) != dict:
|
||||
if not isinstance(item, MutableMapping):
|
||||
raise errors.AnsibleFilterError("|failed expects a dictionary")
|
||||
rc = item.get('rc',0)
|
||||
failed = item.get('failed',False)
|
||||
@@ -44,13 +45,13 @@ def success(*a, **kw):
|
||||
def changed(*a, **kw):
|
||||
''' Test if task result yields changed '''
|
||||
item = a[0]
|
||||
if type(item) != dict:
|
||||
if not isinstance(item, MutableMapping):
|
||||
raise errors.AnsibleFilterError("|changed expects a dictionary")
|
||||
if not 'changed' in item:
|
||||
changed = False
|
||||
if ('results' in item # some modules return a 'results' key
|
||||
and type(item['results']) == list
|
||||
and type(item['results'][0]) == dict):
|
||||
and isinstance(item['results'], MutableSequence)
|
||||
and isinstance(item['results'][0], MutableMapping)):
|
||||
for result in item['results']:
|
||||
changed = changed or result.get('changed', False)
|
||||
else:
|
||||
@@ -60,7 +61,7 @@ def changed(*a, **kw):
|
||||
def skipped(*a, **kw):
|
||||
''' Test if task result yields skipped '''
|
||||
item = a[0]
|
||||
if type(item) != dict:
|
||||
if not isinstance(item, MutableMapping):
|
||||
raise errors.AnsibleFilterError("|skipped expects a dictionary")
|
||||
skipped = item.get('skipped', False)
|
||||
return skipped
|
||||
|
||||
Reference in New Issue
Block a user