ENH: remove some code duplication, enhance docstrings, uniformize naming

pull/1346/head
Yaroslav Halchenko 9 years ago
parent 7eafb0c206
commit c84b6370c5

@ -998,7 +998,7 @@ class DNSUtils:
IP_CRE = re.compile("^(?:\d{1,3}\.){3}\d{1,3}$")
# todo: make configurable the expired time and max count of cache entries:
CACHE_dnsToIp = Utils.Cache(maxCount=1000, maxTime=5*60)
CACHE_nameToIp = Utils.Cache(maxCount=1000, maxTime=5*60)
CACHE_ipToName = Utils.Cache(maxCount=1000, maxTime=5*60)
@staticmethod
@ -1007,7 +1007,7 @@ class DNSUtils:
Thanks to Kevin Drapel.
"""
# cache, also prevent long wait during retrieving of ip for wrong dns or lazy dns-system:
v = DNSUtils.CACHE_dnsToIp.get(dns)
v = DNSUtils.CACHE_nameToIp.get(dns)
if v is not None:
return v
# retrieve ip (todo: use AF_INET6 for IPv6)
@ -1017,7 +1017,7 @@ class DNSUtils:
# todo: make configurable the expired time of cache entry:
logSys.warning("Unable to find a corresponding IP address for %s: %s", dns, e)
v = list()
DNSUtils.CACHE_dnsToIp.set(dns, v)
DNSUtils.CACHE_nameToIp.set(dns, v)
return v
@staticmethod

@ -111,12 +111,13 @@ class Utils():
If shell is True (default), the specified command (may be a string) will be
executed through the shell.
output : bool
If output is True, the function returns tuple (success, stdoutdata, stderrdata, returncode)
If output is True, the function returns tuple (success, stdoutdata, stderrdata, returncode).
If False, just indication of success is returned
Returns
-------
bool
True if the command succeeded.
bool or (bool, str, str, int)
True if the command succeeded and with stdout, stderr, returncode if output was set to True
Raises
------
@ -185,9 +186,10 @@ class Utils():
logSys.log(std_level, "%s -- stderr: %r", realCmd, stderr)
popen.stderr.close()
success = False
if retcode == 0:
logSys.debug("%s -- returned successfully", realCmd)
return True if not output else (True, stdout, stderr, retcode)
success = True
elif retcode is None:
logSys.error("%s -- unable to kill PID %i" % (realCmd, popen.pid))
elif retcode < 0 or retcode > 128:
@ -200,13 +202,13 @@ class Utils():
logSys.error("%s -- returned %i" % (realCmd, retcode))
if msg:
logSys.info("HINT on %i: %s", retcode, msg % locals())
return False if not output else (False, stdout, stderr, retcode)
return success if not output else (success, stdout, stderr, retcode)
@staticmethod
def wait_for(cond, timeout, interval=None):
"""Wait until condition expression `cond` is True, up to `timeout` sec
"""
ini = 1
ini = 1 # to delay initializations until/when necessary
while True:
ret = cond()
if ret:

Loading…
Cancel
Save