Merge "Add a new parameter ``suffix`` to function ``get_traits``"

This commit is contained in:
Jenkins 2017-09-04 14:25:28 +00:00 committed by Gerrit Code Review
commit 4e047e1674
3 changed files with 28 additions and 3 deletions

View File

@ -65,18 +65,21 @@ def import_submodules(package, recursive=True):
import_submodules(sys.modules.get(__name__))
def get_traits(prefix=None):
def get_traits(prefix=None, suffix=None):
"""Returns the trait strings in the os_traits module, optionally filtered
by a supplied prefix.
by a supplied prefix and suffix.
:param prefix: Optional string prefix to filter by. e.g. 'HW_'
:param suffix: Optional string suffix to filter by, e.g. 'SSE'
"""
prefix = prefix or ""
suffix = suffix or ""
return [
v for k, v in sys.modules[__name__].__dict__.items()
if isinstance(v, six.string_types) and
not k.startswith('_') and
v.startswith(prefix) and
v.endswith(suffix) and
# skip module constants
k not in ('CUSTOM_NAMESPACE', 'THIS_NAME', 'THIS_LIB', 'TEST_DIR')
]

View File

@ -38,7 +38,7 @@ class TestSymbols(base.TestCase):
ot.HW_GPU_RESOLUTION_W1920H1080)
self.assertEqual(offload.TSO, ot.HW_NIC_OFFLOAD_TSO)
def test_get_traits(self):
def test_get_traits_filter_by_prefix(self):
traits = ot.get_traits('HW_CPU')
self.assertIn("HW_CPU_X86_SSE42", traits)
self.assertIn(ot.HW_CPU_X86_AVX2, traits)
@ -47,6 +47,22 @@ class TestSymbols(base.TestCase):
self.assertNotIn('CUSTOM_NAMESPACE', traits)
self.assertNotIn('os_traits', traits)
def test_get_traits_filter_by_suffix(self):
traits = ot.get_traits(suffix='SSE42')
self.assertIn("HW_CPU_X86_SSE42", traits)
self.assertEqual(1, len(traits))
def test_get_traits_filter_by_prefix_and_suffix(self):
traits = ot.get_traits(prefix='HW_NIC', suffix='RSA')
self.assertIn("HW_NIC_ACCEL_RSA", traits)
self.assertNotIn(ot.HW_NIC_ACCEL_TLS, traits)
self.assertEqual(1, len(traits))
traits = ot.get_traits(prefix='HW_NIC', suffix='TX')
self.assertIn("HW_NIC_SRIOV_QOS_TX", traits)
self.assertIn("HW_NIC_OFFLOAD_TX", traits)
self.assertEqual(2, len(traits))
def test_check_traits(self):
traits = set(["HW_CPU_X86_SSE42", "HW_CPU_X86_XOP"])
not_traits = set(["not_trait1", "not_trait2"])

View File

@ -0,0 +1,6 @@
---
features:
- |
Added a new optional parameter ``suffix`` to function ``get_traits``. This
new parameter allows filtering the list of traits returned by the ending
of the name.