mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
Use 'except ... as' syntax
This syntax works on Python 2.6 through 3.x. lib/ansible/module_utils (and lib/ansible/modules) need to support Python 2.4, so I didn't touch those.
This commit is contained in:
12
lib/ansible/plugins/cache/jsonfile.py
vendored
12
lib/ansible/plugins/cache/jsonfile.py
vendored
@@ -45,7 +45,7 @@ class CacheModule(BaseCacheModule):
|
||||
if not os.path.exists(self._cache_dir):
|
||||
try:
|
||||
os.makedirs(self._cache_dir)
|
||||
except (OSError,IOError), e:
|
||||
except (OSError,IOError) as e:
|
||||
self._display.warning("error while trying to create cache dir %s : %s" % (self._cache_dir, str(e)))
|
||||
return None
|
||||
|
||||
@@ -60,7 +60,7 @@ class CacheModule(BaseCacheModule):
|
||||
cachefile = "%s/%s" % (self._cache_dir, key)
|
||||
try:
|
||||
f = codecs.open(cachefile, 'r', encoding='utf-8')
|
||||
except (OSError,IOError), e:
|
||||
except (OSError,IOError) as e:
|
||||
self._display.warning("error while trying to read %s : %s" % (cachefile, str(e)))
|
||||
pass
|
||||
else:
|
||||
@@ -81,7 +81,7 @@ class CacheModule(BaseCacheModule):
|
||||
cachefile = "%s/%s" % (self._cache_dir, key)
|
||||
try:
|
||||
f = codecs.open(cachefile, 'w', encoding='utf-8')
|
||||
except (OSError,IOError), e:
|
||||
except (OSError,IOError) as e:
|
||||
self._display.warning("error while trying to write to %s : %s" % (cachefile, str(e)))
|
||||
pass
|
||||
else:
|
||||
@@ -94,7 +94,7 @@ class CacheModule(BaseCacheModule):
|
||||
cachefile = "%s/%s" % (self._cache_dir, key)
|
||||
try:
|
||||
st = os.stat(cachefile)
|
||||
except (OSError,IOError), e:
|
||||
except (OSError,IOError) as e:
|
||||
if e.errno == errno.ENOENT:
|
||||
return False
|
||||
else:
|
||||
@@ -126,7 +126,7 @@ class CacheModule(BaseCacheModule):
|
||||
try:
|
||||
st = os.stat(cachefile)
|
||||
return True
|
||||
except (OSError,IOError), e:
|
||||
except (OSError,IOError) as e:
|
||||
if e.errno == errno.ENOENT:
|
||||
return False
|
||||
else:
|
||||
@@ -137,7 +137,7 @@ class CacheModule(BaseCacheModule):
|
||||
del self._cache[key]
|
||||
try:
|
||||
os.remove("%s/%s" % (self._cache_dir, key))
|
||||
except (OSError,IOError), e:
|
||||
except (OSError,IOError) as e:
|
||||
pass #TODO: only pass on non existing?
|
||||
|
||||
def flush(self):
|
||||
|
||||
@@ -152,7 +152,7 @@ def version_compare(value, version, operator='eq', strict=False):
|
||||
try:
|
||||
method = getattr(py_operator, operator)
|
||||
return method(Version(str(value)), Version(str(version)))
|
||||
except Exception, e:
|
||||
except Exception as e:
|
||||
raise errors.AnsibleFilterError('Version comparison: %s' % e)
|
||||
|
||||
def regex_escape(string):
|
||||
|
||||
@@ -80,14 +80,14 @@ def logarithm(x, base=math.e):
|
||||
return math.log10(x)
|
||||
else:
|
||||
return math.log(x, base)
|
||||
except TypeError, e:
|
||||
except TypeError as e:
|
||||
raise errors.AnsibleFilterError('log() can only be used on numbers: %s' % str(e))
|
||||
|
||||
|
||||
def power(x, y):
|
||||
try:
|
||||
return math.pow(x, y)
|
||||
except TypeError, e:
|
||||
except TypeError as e:
|
||||
raise errors.AnsibleFilterError('pow() can only be used on numbers: %s' % str(e))
|
||||
|
||||
|
||||
@@ -97,7 +97,7 @@ def inversepower(x, base=2):
|
||||
return math.sqrt(x)
|
||||
else:
|
||||
return math.pow(x, 1.0/float(base))
|
||||
except TypeError, e:
|
||||
except TypeError as e:
|
||||
raise errors.AnsibleFilterError('root() can only be used on numbers: %s' % str(e))
|
||||
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ except ImportError:
|
||||
try:
|
||||
import consul
|
||||
HAS_CONSUL = True
|
||||
except ImportError, e:
|
||||
except ImportError as e:
|
||||
HAS_CONSUL = False
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ class LookupModule(LookupBase):
|
||||
values.append(r['Value'])
|
||||
else:
|
||||
values.append(results[1]['Value'])
|
||||
except Exception, e:
|
||||
except Exception as e:
|
||||
raise AnsibleError(
|
||||
"Error locating '%s' in kv store. Error was %s" % (term, e))
|
||||
|
||||
@@ -127,7 +127,7 @@ class LookupModule(LookupBase):
|
||||
name, value = param.split('=')
|
||||
assert name in paramvals, "% not a valid consul lookup parameter" % name
|
||||
paramvals[name] = value
|
||||
except (ValueError, AssertionError), e:
|
||||
except (ValueError, AssertionError) as e:
|
||||
raise AnsibleError(e)
|
||||
|
||||
return paramvals
|
||||
|
||||
@@ -41,7 +41,7 @@ class LookupModule(LookupBase):
|
||||
val = credstash.getSecret(term, **kwargs)
|
||||
except credstash.ItemNotFound:
|
||||
raise AnsibleError('Key {0} not found'.format(term))
|
||||
except Exception, e:
|
||||
except Exception as e:
|
||||
raise AnsibleError('Encountered exception while fetching {0}: {1}'.format(term, e.message))
|
||||
ret.append(val)
|
||||
|
||||
|
||||
@@ -141,7 +141,7 @@ class LookupModule(LookupBase):
|
||||
try:
|
||||
nsaddr = dns.resolver.query(ns)[0].address
|
||||
nameservers.append(nsaddr)
|
||||
except Exception, e:
|
||||
except Exception as e:
|
||||
raise AnsibleError("dns lookup NS: ", str(e))
|
||||
myres.nameservers = nameservers
|
||||
continue
|
||||
@@ -176,7 +176,7 @@ class LookupModule(LookupBase):
|
||||
domain = n.to_text()
|
||||
except dns.exception.SyntaxError:
|
||||
pass
|
||||
except Exception, e:
|
||||
except Exception as e:
|
||||
raise AnsibleError("dns.reversename unhandled exception", str(e))
|
||||
|
||||
try:
|
||||
@@ -196,7 +196,7 @@ class LookupModule(LookupBase):
|
||||
rd['ttl'] = answers.rrset.ttl
|
||||
|
||||
ret.append(rd)
|
||||
except Exception, e:
|
||||
except Exception as e:
|
||||
ret.append(str(e))
|
||||
|
||||
except dns.resolver.NXDOMAIN:
|
||||
@@ -205,7 +205,7 @@ class LookupModule(LookupBase):
|
||||
ret.append("")
|
||||
except dns.resolver.Timeout:
|
||||
ret.append('')
|
||||
except dns.exception.DNSException, e:
|
||||
except dns.exception.DNSException as e:
|
||||
raise AnsibleError("dns.resolver unhandled exception", e)
|
||||
|
||||
return ret
|
||||
|
||||
@@ -47,7 +47,7 @@ class LookupModule(LookupBase):
|
||||
# Retrieve a single value
|
||||
try:
|
||||
value = self.cp.get(section, key)
|
||||
except ConfigParser.NoOptionError, e:
|
||||
except ConfigParser.NoOptionError as e:
|
||||
return dflt
|
||||
return value
|
||||
|
||||
@@ -76,7 +76,7 @@ class LookupModule(LookupBase):
|
||||
name, value = param.split('=')
|
||||
assert(name in paramvals)
|
||||
paramvals[name] = value
|
||||
except (ValueError, AssertionError), e:
|
||||
except (ValueError, AssertionError) as e:
|
||||
raise errors.AnsibleError(e)
|
||||
|
||||
path = self._loader.path_dwim_relative(basedir, 'files', paramvals['file'])
|
||||
|
||||
@@ -32,7 +32,7 @@ class LookupModule(LookupBase):
|
||||
for x in terms:
|
||||
try:
|
||||
intermediate = listify_lookup_plugin_terms(x, templar=self._templar, loader=self._loader, fail_on_undefined=True)
|
||||
except UndefinedError, e:
|
||||
except UndefinedError as e:
|
||||
raise AnsibleUndefinedVariable("One of the nested variables was undefined. The error was: %s" % e)
|
||||
results.append(intermediate)
|
||||
return results
|
||||
|
||||
@@ -186,7 +186,7 @@ class LookupModule(LookupBase):
|
||||
try:
|
||||
if not self.parse_simple_args(term):
|
||||
self.parse_kv_args(parse_kv(term))
|
||||
except Exception, e:
|
||||
except Exception as e:
|
||||
raise AnsibleError("unknown error parsing with_sequence arguments: %r. Error was: %s" % (term, e))
|
||||
|
||||
self.sanity_check()
|
||||
|
||||
@@ -55,7 +55,7 @@ class LookupModule(LookupBase):
|
||||
assert(name in paramvals)
|
||||
paramvals[name] = value
|
||||
|
||||
except (ValueError, AssertionError), e:
|
||||
except (ValueError, AssertionError) as e:
|
||||
# In case "file" or "key" are not present
|
||||
raise AnsibleError(e)
|
||||
|
||||
|
||||
@@ -382,7 +382,7 @@ class StrategyBase:
|
||||
data = self._loader.load_from_file(included_file._filename)
|
||||
if data is None:
|
||||
return []
|
||||
except AnsibleError, e:
|
||||
except AnsibleError as e:
|
||||
for host in included_file._hosts:
|
||||
tr = TaskResult(host=host, task=included_file._task, return_data=dict(failed=True, reason=str(e)))
|
||||
iterator.mark_host_failed(host)
|
||||
@@ -455,7 +455,7 @@ class StrategyBase:
|
||||
loader=self._loader,
|
||||
variable_manager=self._variable_manager
|
||||
)
|
||||
except AnsibleError, e:
|
||||
except AnsibleError as e:
|
||||
return False
|
||||
|
||||
if len(included_files) > 0:
|
||||
@@ -475,7 +475,7 @@ class StrategyBase:
|
||||
# and add the new blocks to the list of handler blocks
|
||||
handler_block.block.extend(block.block)
|
||||
#iterator._play.handlers.extend(new_blocks)
|
||||
except AnsibleError, e:
|
||||
except AnsibleError as e:
|
||||
for host in included_file._hosts:
|
||||
iterator.mark_host_failed(host)
|
||||
self._tqm._failed_hosts[host.name] = True
|
||||
|
||||
@@ -144,7 +144,7 @@ class StrategyModule(StrategyBase):
|
||||
|
||||
try:
|
||||
included_files = IncludedFile.process_include_results(host_results, self._tqm, iterator=iterator, loader=self._loader, variable_manager=self._variable_manager)
|
||||
except AnsibleError, e:
|
||||
except AnsibleError as e:
|
||||
return False
|
||||
|
||||
if len(included_files) > 0:
|
||||
@@ -153,7 +153,7 @@ class StrategyModule(StrategyBase):
|
||||
# list of noop tasks, to make sure that they continue running in lock-step
|
||||
try:
|
||||
new_blocks = self._load_included_file(included_file, iterator=iterator)
|
||||
except AnsibleError, e:
|
||||
except AnsibleError as e:
|
||||
for host in included_file._hosts:
|
||||
iterator.mark_host_failed(host)
|
||||
self._display.warning(str(e))
|
||||
|
||||
@@ -258,7 +258,7 @@ class StrategyModule(StrategyBase):
|
||||
|
||||
try:
|
||||
included_files = IncludedFile.process_include_results(host_results, self._tqm, iterator=iterator, loader=self._loader, variable_manager=self._variable_manager)
|
||||
except AnsibleError, e:
|
||||
except AnsibleError as e:
|
||||
return False
|
||||
|
||||
if len(included_files) > 0:
|
||||
@@ -273,7 +273,7 @@ class StrategyModule(StrategyBase):
|
||||
# list of noop tasks, to make sure that they continue running in lock-step
|
||||
try:
|
||||
new_blocks = self._load_included_file(included_file, iterator=iterator)
|
||||
except AnsibleError, e:
|
||||
except AnsibleError as e:
|
||||
for host in included_file._hosts:
|
||||
iterator.mark_host_failed(host)
|
||||
self._display.warning(str(e))
|
||||
@@ -296,7 +296,7 @@ class StrategyModule(StrategyBase):
|
||||
iterator.add_tasks(host, all_blocks[host])
|
||||
|
||||
self._display.debug("results queue empty")
|
||||
except (IOError, EOFError), e:
|
||||
except (IOError, EOFError) as e:
|
||||
self._display.debug("got IOError/EOFError in task loop: %s" % e)
|
||||
# most likely an abort, return failed
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user