Files
os-traits/os_traits/__init__.py
Jay Pipes 23d81d4451 organize os_traits for the future
Sean Mooney had a good idea that for future-proofing the os-traits library and
ensuring that we don't have to deal with one giant const.py file, that we break
the library into various modules corresponding to the higher-level namespaces.

This patch adds some symbol-registration foo into a utils module and allows the
os_traits module and "leaf modules" to be called in the following way:

 import os_traits
 from os_traits.hw.cpu import x86

 assert os_traits.HW_CPU_X86_SSE42 == x86.SSE42
 assert x86.SSE42 == 'HW_CPU_X86_SSE42'

Change-Id: I0e8f50822ab67cb3be85ed3b935dd6cdb4436dbf
2017-04-05 16:01:33 +00:00

80 lines
2.4 KiB
Python

# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import sys
import pbr.version
import six
__version__ = pbr.version.VersionInfo(
'os_traits').version_string()
# Any user-specified feature/trait is prefixed with the custom namespace
CUSTOM_NAMESPACE = 'CUSTOM_'
# Each submodule registers its symbols with the os_traits module namespace
from os_traits.hw.cpu import x86 # noqa
from os_traits.storage import disk # noqa
def get_symbol_names(prefix=None):
"""Returns the names of symbols of trait strings in the os_traits module,
optionally filtered by a supplied prefix.
:param prefix: Optional string prefix to filter by. e.g. 'HW_'
"""
return [
k for k, v in sys.modules[__name__].__dict__.items()
if isinstance(v, six.string_types) and
not k.startswith('_') and
(prefix is None or v.startswith(prefix))
]
def get_traits(prefix=None):
"""Returns the trait strings in the os_traits module, optionally filtered
by a supplied prefix.
:param prefix: Optional string prefix to filter by. e.g. 'HW_'
"""
return [
v for k, v in sys.modules[__name__].__dict__.items()
if isinstance(v, six.string_types) and
not k.startswith('_') and
(prefix is None or v.startswith(prefix))
]
def check_traits(traits):
"""Returns a tuple of two trait string sets, the first set contains valid
traits, and the second contains others.
:param traits: An iterable contains trait strings.
"""
trait_set = set(traits)
valid_trait_set = set(get_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(CUSTOM_NAMESPACE)