diff options
author | Alois Mahdal <amahdal@redhat.com> | 2014-05-13 13:03:13 +0200 |
---|---|---|
committer | Alois Mahdal <amahdal@redhat.com> | 2014-05-19 12:45:05 +0200 |
commit | 2517d041f10ce7907181b75fb20b333a79a352c8 (patch) | |
tree | 7a68e58dffb3faeccfe33c4afc96bbc1314bf6f9 | |
parent | bcab5803efc7444b3c1fbff28d0ac9691f152222 (diff) | |
download | openlmi-providers-2517d041f10ce7907181b75fb20b333a79a352c8.tar.gz openlmi-providers-2517d041f10ce7907181b75fb20b333a79a352c8.tar.xz openlmi-providers-2517d041f10ce7907181b75fb20b333a79a352c8.zip |
PEP8, Flake8, readability improvements
-rw-r--r-- | src/python/lmi/test/util.py | 83 |
1 files changed, 52 insertions, 31 deletions
diff --git a/src/python/lmi/test/util.py b/src/python/lmi/test/util.py index 428ac80..a28369e 100644 --- a/src/python/lmi/test/util.py +++ b/src/python/lmi/test/util.py @@ -12,7 +12,7 @@ # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # # Authors: Michal Minar <miminar@redhat.com> # @@ -31,14 +31,16 @@ import subprocess from collections import OrderedDict from lmi.test import unittest + def is_this_system(system_name): """ :returns: Whether the given *system_name* matches the hostname of currently running system. :rtype: boolean """ - return ( socket.gethostbyaddr(system_name)[0] - == socket.gethostbyaddr(socket.gethostname())[0]) + return (socket.gethostbyaddr(system_name)[0] + == socket.gethostbyaddr(socket.gethostname())[0]) + def get_environvar(variable, default='', convert=str): """ @@ -57,6 +59,7 @@ def get_environvar(variable, default='', convert=str): return None return convert(val) + def mark_dangerous(method): """ Decorator for methods of :py:class:`unittest.TestCase` subclasses that @@ -70,6 +73,7 @@ def mark_dangerous(method): else: return unittest.skip("This test is marked as dangerous.")(method) + def mark_tedious(method): """ Decorator for methods of :py:class:`unittest.TestCase` subclasses that @@ -82,6 +86,7 @@ def mark_tedious(method): else: return unittest.skip("This test is marked as tedious.")(method) + def check_inames_equal(fst, snd): """ Compare two objects of :py:class:`pywbem.CIMInstanceName`. Their ``host`` @@ -91,12 +96,31 @@ def check_inames_equal(fst, snd): :returns: ``True`` if both instance names are equal. :rtype: boolean """ - if not isinstance(fst, pywbem.CIMInstanceName): - raise TypeError("fst argument must be a pywbem.CIMInstanceName, not %s" - % repr(fst)) - if not isinstance(snd, pywbem.CIMInstanceName): - raise TypeError("snd argument must be a pywbem.CIMInstanceName, not %s" - % repr(snd)) + + def isa_CIMInstanceName(x): + return isinstance(x, pywbem.CIMInstanceName) + + def isa_cs(x): + return x.classname.endswith('_ComputerSystem') + + def isa_basestring(x): + return isinstance(x, basestring) + + def isa_str(x): + return isinstance(x, str) + + def chkparam(x, n): + if not isa_CIMInstanceName(x): + msg = ("%s argument must be a pywbem.CIMInstanceName, not %r" + % (x, n)) + raise TypeError(msg) + + def decode_utf8_if_str(x): + return x.decode('utf-8') if isa_str(x) else x + + chkparam(fst, "fst") + chkparam(snd, "snd") + if fst.classname != snd.classname or fst.namespace != snd.namespace: return False @@ -105,33 +129,29 @@ def check_inames_equal(fst, snd): if key not in snd_keys: return False snd_value = snd_keys.pop(key) - if ( isinstance(value, pywbem.CIMInstanceName) - and isinstance(snd_value, pywbem.CIMInstanceName)): + if isa_CIMInstanceName(value) and isa_CIMInstanceName(snd_value): if not check_inames_equal(value, snd_value): return False # accept also aliases in the Name attribute of ComputerSystem - elif ( ( fst.classname.endswith('_ComputerSystem') - and key.lower() == 'name') - or ( key.lower() == 'systemname' - and 'SystemCreationClassName' in fst)): - if ( value != snd_value - and ( not is_this_system(value) - or not is_this_system(snd_value))): + elif ((isa_cs(fst) and key.lower() == 'name') + or (key.lower() == 'systemname' + and 'SystemCreationClassName' in fst) + ): + if (value != snd_value + and (not is_this_system(value) + or not is_this_system(snd_value))): return False - elif ( fst.classname.endswith('_ComputerSystem') - and key.lower() == 'creationclassname'): - if ( value != snd_value - and 'CIM_ComputerSystem' not in [ - p['CreationClassName'] for p in (fst, snd)]): + elif (isa_cs(fst) and key.lower() == 'creationclassname'): + if (value != snd_value + and 'CIM_ComputerSystem' not in [ + p['CreationClassName'] for p in (fst, snd)]): return False - elif isinstance(value, basestring) \ - and isinstance(snd_value, basestring): - value = value.decode('utf-8') if isinstance(value, str) else value - snd_value = ( snd_value.decode('utf-8') - if isinstance(snd_value, str) else snd_value) + elif isa_basestring(value) and isa_basestring(snd_value): + value = decode_utf8_if_str(value) + snd_value = decode_utf8_if_str(snd_value) if value != snd_value: return False @@ -143,6 +163,7 @@ def check_inames_equal(fst, snd): return True + def random_string(strength=6, chars=None, prefix=""): """ Generate a random string, e.g. usable as UID/GID @@ -249,7 +270,7 @@ class BaseCrippler: LINE_COUNT = 50 BINARY_LENGTH = 10 * 1024 * 1024 - ## virtual + # # virtual # def _define_cases(self): @@ -281,7 +302,7 @@ class BaseCrippler: """ pass - ## internal + # # internal # def __init__(self): @@ -323,7 +344,7 @@ class BaseCrippler: raise ValueError("unknown case: %s for: %s" % (case, path)) return content - ## public + # # public # def all_cases_for(self, path): |