summaryrefslogtreecommitdiff
path: root/lib/ansible/plugins/filter
diff options
context:
space:
mode:
authorDag Wieers <dag@wieers.com>2017-06-02 13:14:11 +0200
committerJohn R Barker <john@johnrbarker.com>2017-06-02 12:14:11 +0100
commit5553b208281d723ead1e61be5c37227ec2b2736e (patch)
tree2f81e684e9f4451a3b1b58b95e1d00125b34e439 /lib/ansible/plugins/filter
parent2f33c1a1a1d0c74d31445984fd85d877f0d8ab59 (diff)
downloadansible-5553b208281d723ead1e61be5c37227ec2b2736e.tar.gz
Collated PEP8 fixes (#25293)
- Make PEP8 compliant
Diffstat (limited to 'lib/ansible/plugins/filter')
-rw-r--r--lib/ansible/plugins/filter/__init__.py2
-rw-r--r--lib/ansible/plugins/filter/core.py71
-rw-r--r--lib/ansible/plugins/filter/ipaddr.py92
-rw-r--r--lib/ansible/plugins/filter/json_query.py1
-rw-r--r--lib/ansible/plugins/filter/mathstuff.py34
5 files changed, 137 insertions, 63 deletions
diff --git a/lib/ansible/plugins/filter/__init__.py b/lib/ansible/plugins/filter/__init__.py
index cbbbd64118..980f84a225 100644
--- a/lib/ansible/plugins/filter/__init__.py
+++ b/lib/ansible/plugins/filter/__init__.py
@@ -1,5 +1,3 @@
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
-
-
diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py
index e82061a51d..462a6612f2 100644
--- a/lib/ansible/plugins/filter/core.py
+++ b/lib/ansible/plugins/filter/core.py
@@ -72,20 +72,24 @@ class AnsibleJSONEncoder(json.JSONEncoder):
else:
return super(AnsibleJSONEncoder, self).default(o)
+
def to_yaml(a, *args, **kw):
'''Make verbose, human readable yaml'''
transformed = yaml.dump(a, Dumper=AnsibleDumper, allow_unicode=True, **kw)
return to_text(transformed)
+
def to_nice_yaml(a, indent=4, *args, **kw):
'''Make verbose, human readable yaml'''
transformed = yaml.dump(a, Dumper=AnsibleDumper, indent=indent, allow_unicode=True, default_flow_style=False, **kw)
return to_text(transformed)
+
def to_json(a, *args, **kw):
''' Convert the value to JSON '''
return json.dumps(a, cls=AnsibleJSONEncoder, *args, **kw)
+
def to_nice_json(a, indent=4, *args, **kw):
'''Make verbose, human readable JSON'''
# python-2.6's json encoder is buggy (can't encode hostvars)
@@ -109,6 +113,7 @@ def to_nice_json(a, indent=4, *args, **kw):
# Fallback to the to_json filter
return to_json(a, *args, **kw)
+
def to_bool(a):
''' return a bool for the arg '''
if a is None or isinstance(a, bool):
@@ -119,11 +124,12 @@ def to_bool(a):
return True
return False
+
def to_datetime(string, format="%Y-%d-%m %H:%M:%S"):
return datetime.strptime(string, format)
-def strftime(string_format, second = None):
+def strftime(string_format, second=None):
''' return a date string using string. See https://docs.python.org/2/library/time.html#time.strftime for format '''
if second is not None:
try:
@@ -132,13 +138,16 @@ def strftime(string_format, second = None):
raise errors.AnsibleFilterError('Invalid value for epoch value (%s)' % second)
return time.strftime(string_format, time.localtime(second))
+
def quote(a):
''' return its argument quoted for shell usage '''
return shlex_quote(a)
+
def fileglob(pathname):
''' return list of matched regular files for glob '''
- return [ g for g in glob.glob(pathname) if os.path.isfile(g) ]
+ return [g for g in glob.glob(pathname) if os.path.isfile(g)]
+
def regex_replace(value='', pattern='', replacement='', ignorecase=False):
''' Perform a `re.sub` returning a string '''
@@ -152,6 +161,7 @@ def regex_replace(value='', pattern='', replacement='', ignorecase=False):
_re = re.compile(pattern, flags=flags)
return _re.sub(replacement, value)
+
def regex_findall(value, regex, multiline=False, ignorecase=False):
''' Perform re.findall and return the list of matches '''
flags = 0
@@ -161,6 +171,7 @@ def regex_findall(value, regex, multiline=False, ignorecase=False):
flags |= re.M
return re.findall(regex, value, flags)
+
def regex_search(value, regex, *args, **kwargs):
''' Perform re.search and return the list of matches or a backref '''
@@ -191,6 +202,7 @@ def regex_search(value, regex, *args, **kwargs):
items.append(match.group(item))
return items
+
def ternary(value, true_val, false_val):
''' value ? true_val : false_val '''
if value:
@@ -199,16 +211,17 @@ def ternary(value, true_val, false_val):
return false_val
-
def regex_escape(string):
'''Escape all regular expressions special characters from STRING.'''
return re.escape(string)
+
def from_yaml(data):
if isinstance(data, string_types):
return yaml.safe_load(data)
return data
+
@environmentfilter
def rand(environment, end, start=None, step=None, seed=None):
if seed is None:
@@ -228,6 +241,7 @@ def rand(environment, end, start=None, step=None, seed=None):
else:
raise errors.AnsibleFilterError('random can only be used on sequences and integers')
+
def randomize_list(mylist, seed=None):
try:
mylist = list(mylist)
@@ -240,9 +254,10 @@ def randomize_list(mylist, seed=None):
pass
return mylist
+
def get_hash(data, hashtype='sha1'):
- try: # see if hash is supported
+ try: # see if hash is supported
h = hashlib.new(hashtype)
except:
return None
@@ -250,14 +265,15 @@ def get_hash(data, hashtype='sha1'):
h.update(to_bytes(data, errors='surrogate_then_strict'))
return h.hexdigest()
+
def get_encrypted_password(password, hashtype='sha512', salt=None):
# TODO: find a way to construct dynamically from system
- cryptmethod= {
- 'md5': '1',
+ cryptmethod = {
+ 'md5': '1',
'blowfish': '2a',
- 'sha256': '5',
- 'sha512': '6',
+ 'sha256': '5',
+ 'sha512': '6',
}
if hashtype in cryptmethod:
@@ -273,7 +289,7 @@ def get_encrypted_password(password, hashtype='sha512', salt=None):
if not HAS_PASSLIB:
if sys.platform.startswith('darwin'):
raise errors.AnsibleFilterError('|password_hash requires the passlib python module to generate password hashes on Mac OS X/Darwin')
- saltstring = "$%s$%s" % (cryptmethod[hashtype],salt)
+ saltstring = "$%s$%s" % (cryptmethod[hashtype], salt)
encrypted = crypt.crypt(password, saltstring)
else:
if hashtype == 'blowfish':
@@ -287,9 +303,11 @@ def get_encrypted_password(password, hashtype='sha512', salt=None):
return None
+
def to_uuid(string):
return str(uuid.uuid5(UUID_NAMESPACE_ANSIBLE, str(string)))
+
def mandatory(a):
from jinja2.runtime import Undefined
@@ -298,6 +316,7 @@ def mandatory(a):
raise errors.AnsibleFilterError('Mandatory variable not defined.')
return a
+
def combine(*terms, **kwargs):
recursive = kwargs.get('recursive', False)
if len(kwargs) > 1 or (len(kwargs) == 1 and 'recursive' not in kwargs):
@@ -312,6 +331,7 @@ def combine(*terms, **kwargs):
else:
return dict(itertools.chain(*map(iteritems, terms)))
+
def comment(text, style='plain', **kw):
# Predefined comment types
comment_styles = {
@@ -394,6 +414,7 @@ def comment(text, style='plain', **kw):
str_postfix,
str_end)
+
def extract(item, container, morekeys=None):
from jinja2.runtime import Undefined
@@ -410,6 +431,7 @@ def extract(item, container, morekeys=None):
return value
+
def failed(*a, **kw):
''' Test if task result yields failed '''
item = a[0]
@@ -422,26 +444,31 @@ def failed(*a, **kw):
else:
return False
+
def success(*a, **kw):
''' Test if task result yields success '''
return not failed(*a, **kw)
+
def changed(*a, **kw):
''' Test if task result yields changed '''
item = a[0]
if not isinstance(item, MutableMapping):
raise errors.AnsibleFilterError("|changed expects a dictionary")
- if not 'changed' in item:
+ if 'changed' not in item:
changed = False
- if ('results' in item # some modules return a 'results' key
- and isinstance(item['results'], MutableSequence)
- and isinstance(item['results'][0], MutableMapping)):
+ if (
+ 'results' in item and # some modules return a 'results' key
+ isinstance(item['results'], MutableSequence) and
+ isinstance(item['results'][0], MutableMapping)
+ ):
for result in item['results']:
changed = changed or result.get('changed', False)
else:
changed = item.get('changed', False)
return changed
+
def skipped(*a, **kw):
''' Test if task result yields skipped '''
item = a[0]
@@ -504,7 +531,7 @@ class FilterModule(object):
'to_nice_yaml': to_nice_yaml,
'from_yaml': from_yaml,
- #date
+ # date
'to_datetime': to_datetime,
# path
@@ -567,18 +594,18 @@ class FilterModule(object):
'extract': extract,
# failure testing
- 'failed' : failed,
- 'failure' : failed,
- 'success' : success,
- 'succeeded' : success,
+ 'failed': failed,
+ 'failure': failed,
+ 'success': success,
+ 'succeeded': success,
# changed testing
- 'changed' : changed,
- 'change' : changed,
+ 'changed': changed,
+ 'change': changed,
# skip testing
- 'skipped' : skipped,
- 'skip' : skipped,
+ 'skipped': skipped,
+ 'skip': skipped,
# debug
'type_debug': lambda o: o.__class__.__name__,
diff --git a/lib/ansible/plugins/filter/ipaddr.py b/lib/ansible/plugins/filter/ipaddr.py
index beef17e53f..cd56537eda 100644
--- a/lib/ansible/plugins/filter/ipaddr.py
+++ b/lib/ansible/plugins/filter/ipaddr.py
@@ -36,7 +36,6 @@ from ansible import errors
# ---- IP address and network query helpers ----
-
def _empty_ipaddr_query(v, vtype):
# We don't have any query to process, so just check what type the user
# expects, and return the IP address in a correct format
@@ -46,6 +45,7 @@ def _empty_ipaddr_query(v, vtype):
elif vtype == 'network':
return str(v)
+
def _6to4_query(v, vtype, value):
if v.version == 4:
@@ -76,6 +76,7 @@ def _6to4_query(v, vtype, value):
else:
return False
+
def _ip_query(v):
if v.size == 1:
return str(v.ip)
@@ -84,22 +85,27 @@ def _ip_query(v):
if v.ip != v.network or not v.broadcast:
return str(v.ip)
+
def _gateway_query(v):
if v.size > 1:
if v.ip != v.network:
return str(v.ip) + '/' + str(v.prefixlen)
+
def _bool_ipaddr_query(v):
if v:
return True
+
def _broadcast_query(v):
if v.size > 1:
return str(v.broadcast)
+
def _cidr_query(v):
return str(v)
+
def _cidr_lookup_query(v, iplist, value):
try:
if v in iplist:
@@ -107,6 +113,7 @@ def _cidr_lookup_query(v, iplist, value):
except:
return False
+
def _host_query(v):
if v.size == 1:
return str(v)
@@ -114,15 +121,18 @@ def _host_query(v):
if v.ip != v.network:
return str(v.ip) + '/' + str(v.prefixlen)
+
def _hostmask_query(v):
return str(v.hostmask)
+
def _int_query(v, vtype):
if vtype == 'address':
return int(v.ip)
elif vtype == 'network':
return str(int(v.ip)) + '/' + str(int(v.prefixlen))
+
def _ipv4_query(v, value):
if v.version == 6:
try:
@@ -132,12 +142,14 @@ def _ipv4_query(v, value):
else:
return value
+
def _ipv6_query(v, value):
if v.version == 4:
return str(v.ipv6())
else:
return value
+
def _link_local_query(v, value):
v_ip = netaddr.IPAddress(str(v.ip))
if v.version == 4:
@@ -148,34 +160,42 @@ def _link_local_query(v, value):
if ipaddr(str(v_ip), 'fe80::/10'):
return value
+
def _loopback_query(v, value):
v_ip = netaddr.IPAddress(str(v.ip))
if v_ip.is_loopback():
return value
+
def _multicast_query(v, value):
if v.is_multicast():
return value
+
def _net_query(v):
if v.size > 1:
if v.ip == v.network:
return str(v.network) + '/' + str(v.prefixlen)
+
def _netmask_query(v):
return str(v.netmask)
+
def _network_query(v):
if v.size > 1:
return str(v.network)
+
def _prefix_query(v):
return int(v.prefixlen)
+
def _private_query(v, value):
if v.is_private():
return value
+
def _public_query(v, value):
v_ip = netaddr.IPAddress(str(v.ip))
if (v_ip.is_unicast() and not v_ip.is_private() and
@@ -183,16 +203,20 @@ def _public_query(v, value):
not v_ip.is_hostmask()):
return value
+
def _revdns_query(v):
v_ip = netaddr.IPAddress(str(v.ip))
return v_ip.reverse_dns
+
def _size_query(v):
return v.size
+
def _subnet_query(v):
return str(v.cidr)
+
def _type_query(v):
if v.size == 1:
return 'address'
@@ -202,13 +226,16 @@ def _type_query(v):
else:
return 'network'
+
def _unicast_query(v, value):
if v.is_unicast():
return value
+
def _version_query(v):
return v.version
+
def _wrap_query(v, vtype, value):
if v.version == 6:
if vtype == 'address':
@@ -224,41 +251,48 @@ def _bare_query(v):
v.dialect = netaddr.mac_bare
return str(v)
+
def _bool_hwaddr_query(v):
if v:
return True
+
def _int_hwaddr_query(v):
return int(v)
+
def _cisco_query(v):
v.dialect = netaddr.mac_cisco
return str(v)
+
def _empty_hwaddr_query(v, value):
if v:
return value
+
def _linux_query(v):
v.dialect = mac_linux
return str(v)
+
def _postgresql_query(v):
v.dialect = netaddr.mac_pgsql
return str(v)
+
def _unix_query(v):
v.dialect = netaddr.mac_unix
return str(v)
+
def _win_query(v):
v.dialect = netaddr.mac_eui48
return str(v)
# ---- IP address and network filters ----
-
-def ipaddr(value, query = '', version = False, alias = 'ipaddr'):
+def ipaddr(value, query='', version=False, alias='ipaddr'):
''' Check if string is an IP address or network and filter it '''
query_func_extra_args = {
@@ -276,7 +310,8 @@ def ipaddr(value, query = '', version = False, alias = 'ipaddr'):
'public': ('value',),
'unicast': ('value',),
'wrap': ('vtype', 'value'),
- }
+ }
+
query_func_map = {
'': _empty_ipaddr_query,
'6to4': _6to4_query,
@@ -316,7 +351,7 @@ def ipaddr(value, query = '', version = False, alias = 'ipaddr'):
'v6': _ipv6_query,
'version': _version_query,
'wrap': _wrap_query,
- }
+ }
vtype = None
@@ -421,7 +456,7 @@ def ipaddr(value, query = '', version = False, alias = 'ipaddr'):
# that string is a valid subnet, if so, we can check later if given IP
# address/network is inside that specific subnet
try:
- ### ?? 6to4 and link-local were True here before. Should they still?
+ # ?? 6to4 and link-local were True here before. Should they still?
if query and (query not in query_func_map or query == 'cidr_lookup') and ipaddr(query, 'network'):
iplist = netaddr.IPSet([netaddr.IPNetwork(query)])
query = 'cidr_lookup'
@@ -463,19 +498,19 @@ def ipaddr(value, query = '', version = False, alias = 'ipaddr'):
return False
-def ipwrap(value, query = ''):
+def ipwrap(value, query=''):
try:
if isinstance(value, (list, tuple, types.GeneratorType)):
_ret = []
for element in value:
- if ipaddr(element, query, version = False, alias = 'ipwrap'):
+ if ipaddr(element, query, version=False, alias='ipwrap'):
_ret.append(ipaddr(element, 'wrap'))
else:
_ret.append(element)
return _ret
else:
- _ret = ipaddr(value, query, version = False, alias = 'ipwrap')
+ _ret = ipaddr(value, query, version=False, alias='ipwrap')
if _ret:
return ipaddr(_ret, 'wrap')
else:
@@ -485,12 +520,12 @@ def ipwrap(value, query = ''):
return value
-def ipv4(value, query = ''):
- return ipaddr(value, query, version = 4, alias = 'ipv4')
+def ipv4(value, query=''):
+ return ipaddr(value, query, version=4, alias='ipv4')
-def ipv6(value, query = ''):
- return ipaddr(value, query, version = 6, alias = 'ipv6')
+def ipv6(value, query=''):
+ return ipaddr(value, query, version=6, alias='ipv6')
# Split given subnet into smaller subnets or find out the biggest subnet of
@@ -511,7 +546,7 @@ def ipv6(value, query = ''):
#
# - address | ipsubnet(cidr, index)
# returns next indexed subnet which contains given address
-def ipsubnet(value, query = '', index = 'x'):
+def ipsubnet(value, query='', index='x'):
''' Manipulate IPv4/IPv6 subnets '''
try:
@@ -563,6 +598,7 @@ def ipsubnet(value, query = '', index = 'x'):
return False
+
# Returns the nth host within a network described by value.
# Usage:
#
@@ -594,11 +630,12 @@ def nthhost(value, query=''):
return False
+
# Returns the SLAAC address within a network for a given HW/MAC address.
# Usage:
#
# - prefix | slaac(mac)
-def slaac(value, query = ''):
+def slaac(value, query=''):
''' Get the SLAAC address within given network '''
try:
vtype = ipaddr(value, 'type')
@@ -618,7 +655,7 @@ def slaac(value, query = ''):
return False
try:
- mac = hwaddr(query, alias = 'slaac')
+ mac = hwaddr(query, alias='slaac')
eui = netaddr.EUI(mac)
except:
@@ -628,13 +665,13 @@ def slaac(value, query = ''):
# ---- HWaddr / MAC address filters ----
-
-def hwaddr(value, query = '', alias = 'hwaddr'):
+def hwaddr(value, query='', alias='hwaddr'):
''' Check if string is a HW/MAC address and filter it '''
query_func_extra_args = {
'': ('value',),
- }
+ }
+
query_func_map = {
'': _empty_hwaddr_query,
'bare': _bare_query,
@@ -648,7 +685,7 @@ def hwaddr(value, query = '', alias = 'hwaddr'):
'psql': _postgresql_query,
'unix': _unix_query,
'win': _win_query,
- }
+ }
try:
v = netaddr.EUI(value)
@@ -666,23 +703,26 @@ def hwaddr(value, query = '', alias = 'hwaddr'):
return False
-def macaddr(value, query = ''):
- return hwaddr(value, query, alias = 'macaddr')
+
+def macaddr(value, query=''):
+ return hwaddr(value, query, alias='macaddr')
+
def _need_netaddr(f_name, *args, **kwargs):
- raise errors.AnsibleFilterError('The {0} filter requires python-netaddr be'
- ' installed on the ansible controller'.format(f_name))
+ raise errors.AnsibleFilterError('The %s filter requires python-netaddr be '
+ 'installed on the ansible controller' % f_name)
+
def ip4_hex(arg):
''' Convert an IPv4 address to Hexadecimal notation '''
numbers = list(map(int, arg.split('.')))
return '{:02x}{:02x}{:02x}{:02x}'.format(*numbers)
-# ---- Ansible filters ----
+# ---- Ansible filters ----
class FilterModule(object):
''' IP address and network manipulation filters '''
- filter_map = {
+ filter_map = {
# IP addresses and networks
'ipaddr': ipaddr,
'ipwrap': ipwrap,
diff --git a/lib/ansible/plugins/filter/json_query.py b/lib/ansible/plugins/filter/json_query.py
index 479721e9b7..bfb97fc51b 100644
--- a/lib/ansible/plugins/filter/json_query.py
+++ b/lib/ansible/plugins/filter/json_query.py
@@ -39,6 +39,7 @@ def json_query(data, expr):
return jmespath.search(expr, data)
+
class FilterModule(object):
''' Query filter '''
diff --git a/lib/ansible/plugins/filter/mathstuff.py b/lib/ansible/plugins/filter/mathstuff.py
index 242549f348..6e3cc94fe2 100644
--- a/lib/ansible/plugins/filter/mathstuff.py
+++ b/lib/ansible/plugins/filter/mathstuff.py
@@ -20,16 +20,16 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
-import math
import collections
import itertools
+import math
from ansible import errors
from ansible.module_utils import basic
def unique(a):
- if isinstance(a,collections.Hashable):
+ if isinstance(a, collections.Hashable):
c = set(a)
else:
c = []
@@ -38,38 +38,44 @@ def unique(a):
c.append(x)
return c
+
def intersect(a, b):
- if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable):
+ if isinstance(a, collections.Hashable) and isinstance(b, collections.Hashable):
c = set(a) & set(b)
else:
c = unique(filter(lambda x: x in b, a))
return c
+
def difference(a, b):
- if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable):
+ if isinstance(a, collections.Hashable) and isinstance(b, collections.Hashable):
c = set(a) - set(b)
else:
c = unique(filter(lambda x: x not in b, a))
return c
+
def symmetric_difference(a, b):
- if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable):
+ if isinstance(a, collections.Hashable) and isinstance(b, collections.Hashable):
c = set(a) ^ set(b)
else:
- c = unique(filter(lambda x: x not in intersect(a,b), union(a,b)))
+ c = unique(filter(lambda x: x not in intersect(a, b), union(a, b)))
return c
+
def union(a, b):
- if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable):
+ if isinstance(a, collections.Hashable) and isinstance(b, collections.Hashable):
c = set(a) | set(b)
else:
c = unique(a + b)
return c
+
def min(a):
_min = __builtins__.get('min')
return _min(a)
+
def max(a):
_max = __builtins__.get('max')
return _max(a)
@@ -97,7 +103,7 @@ def inversepower(x, base=2):
if base == 2:
return math.sqrt(x)
else:
- return math.pow(x, 1.0/float(base))
+ return math.pow(x, 1.0 / float(base))
except TypeError as e:
raise errors.AnsibleFilterError('root() can only be used on numbers: %s' % str(e))
@@ -109,6 +115,7 @@ def human_readable(size, isbits=False, unit=None):
except:
raise errors.AnsibleFilterError("human_readable() can't interpret following string: %s" % size)
+
def human_to_bytes(size, default_unit=None, isbits=False):
''' Return bytes count from a human readable string '''
try:
@@ -116,14 +123,15 @@ def human_to_bytes(size, default_unit=None, isbits=False):
except:
raise errors.AnsibleFilterError("human_to_bytes() can't interpret following string: %s" % size)
+
class FilterModule(object):
''' Ansible math jinja2 filters '''
def filters(self):
filters = {
# general math
- 'min' : min,
- 'max' : max,
+ 'min': min,
+ 'max': max,
# exponents and logarithms
'log': logarithm,
@@ -131,7 +139,7 @@ class FilterModule(object):
'root': inversepower,
# set theory
- 'unique' : unique,
+ 'unique': unique,
'intersect': intersect,
'difference': difference,
'symmetric_difference': symmetric_difference,
@@ -142,8 +150,8 @@ class FilterModule(object):
'combinations': itertools.combinations,
# computer theory
- 'human_readable' : human_readable,
- 'human_to_bytes' : human_to_bytes,
+ 'human_readable': human_readable,
+ 'human_to_bytes': human_to_bytes,
}