diff --git a/os_traits/__init__.py b/os_traits/__init__.py index 8b6a3d2..109423c 100644 --- a/os_traits/__init__.py +++ b/os_traits/__init__.py @@ -74,3 +74,12 @@ def check_traits(traits): valid_traits = trait_set & valid_trait_set return (valid_traits, trait_set - valid_traits) + + +def is_custom(trait): + """Returns True if the trait string represents a custom trait, or False + otherwise. + + :param trait: String name of the trait + """ + return trait.startswith(NAMESPACES['CUSTOM']) diff --git a/os_traits/const.py b/os_traits/const.py index 81d068a..e576971 100644 --- a/os_traits/const.py +++ b/os_traits/const.py @@ -12,6 +12,9 @@ # License for the specific language governing permissions and limitations # under the License. +# Any user-specified feature/trait is prefixed with the custom namespace +_CUSTOM_NS = 'CUSTOM_' + # All hardware-specific features are prefixed with this namespace _HW_NS = 'HW_' @@ -68,6 +71,7 @@ HW_CPU_X86_VMX = _CPU_X86_NS + 'VMX' HW_CPU_X86_SVM = _CPU_X86_NS + 'SVM' NAMESPACES = { + 'CUSTOM': _CUSTOM_NS, 'HARDWARE': _HW_NS, 'HW': _HW_NS, 'CPU': _CPU_NS, diff --git a/os_traits/tests/test_os_traits.py b/os_traits/tests/test_os_traits.py index 0d5e011..fa490d5 100644 --- a/os_traits/tests/test_os_traits.py +++ b/os_traits/tests/test_os_traits.py @@ -37,7 +37,7 @@ class TestOs_traits(base.TestCase): def test_namespaces(self): namespaces = ot.NAMESPACES self.assertIn(("HARDWARE", "HW_"), namespaces.items()) - self.assertEqual(4, len(namespaces)) + self.assertEqual(5, len(namespaces)) def test_get_traits(self): traits = ot.get_traits(ot.NAMESPACES['X86']) @@ -53,3 +53,7 @@ class TestOs_traits(base.TestCase): check_traits.extend(not_traits) self.assertEqual((traits, not_traits), ot.check_traits(check_traits)) + + def test_is_custom(self): + self.assertTrue(ot.is_custom('CUSTOM_FOO')) + self.assertFalse(ot.is_custom('HW_CPU_X86_SSE42'))