Ejemplo n.º 1
0
def get_class_version(conn, name, namespace=None, cache=None):
    """
    Query broker for version of particular CIM class. Version is stored in
    ``Version`` qualifier of particular CIM class.

    :param conn: Connection object.
    :param string name: Name of class to query.
    :param string namespace: Optional CIM namespace. Defaults to configured namespace.
    :param dictionary cache: Optional cache used to speed up expression prrocessing.
    :returns: Version of CIM matching class. Empty string if class is registered but
        is missing ``Version`` qualifier and ``None`` if it is not registered.
    :rtype: string
    """
    if namespace is None:
        namespace = Configuration.get_instance().namespace
    if cache and (namespace, name) in cache:
        return cache[(conn.uri, namespace, name)]
    ns = conn.get_namespace(namespace)
    cls = getattr(ns, name, None)
    if not cls:
        ret = None
    else:
        quals = cls.wrapped_object.qualifiers
        if 'Version' not in quals:
            ret = ''
        else:
            ret = quals['Version'].value
    if cache is not None:
        cache[(conn.uri, namespace, name)] = ret
    return ret
Ejemplo n.º 2
0
    def _process_property(prop, inst):
        """
        Takes a single property and instance. Returns computed value.

        :rtype: ``(str, any)`` A pair of property name and value.
        """
        if isinstance(prop, basestring):
            prop_name = prop
            if not prop in inst.properties():
                LOG().warn('property "%s" not present in instance of "%s"',
                           prop, inst.path)
                value = "UNKNOWN"
            else:
                value = getattr(inst, prop)
        else:
            if not isinstance(prop, (tuple, list)):
                raise TypeError("prop must be a string or tuple, not %s" %
                                repr(prop))
            prop_name = prop[0]
            try:
                if callable(prop[1]):
                    value = prop[1](inst)
                else:
                    value = getattr(inst, prop[1])
            except Exception as exc:
                if Configuration.get_instance().trace:
                    LOG().exception('failed to render property "%s"', prop[0])
                else:
                    LOG().error('failed to render property "%s": %s', prop[0],
                                exc)
                value = "ERROR"
        return prop_name, value
Ejemplo n.º 3
0
    def _process_property(prop, inst):
        """
        Takes a single property and instance. Returns computed value.

        :rtype: ``(str, any)`` A pair of property name and value.
        """
        if isinstance(prop, basestring):
            prop_name = prop
            if not prop in inst.properties():
                LOG().warn('property "%s" not present in instance of "%s"', prop, inst.path)
                value = "UNKNOWN"
            else:
                value = getattr(inst, prop)
        else:
            if not isinstance(prop, (tuple, list)):
                raise TypeError("prop must be a string or tuple, not %s" % repr(prop))
            prop_name = prop[0]
            try:
                if callable(prop[1]):
                    value = prop[1](inst)
                else:
                    value = getattr(inst, prop[1])
            except Exception as exc:
                if Configuration.get_instance().trace:
                    LOG().exception('failed to render property "%s"', prop[0])
                else:
                    LOG().error('failed to render property "%s": %s', prop[0], exc)
                value = "ERROR"
        return prop_name, value
Ejemplo n.º 4
0
 def __init__(self, namespace=None):
     if namespace is not None and not isinstance(namespace, basestring):
         raise TypeError("namespace must be a string")
     if namespace is None:
         namespace = Configuration.get_instance().get_safe(
                 "Main", "CommandNamespace")
     self._namespace = namespace
     self._commands = {}
     self._load_commands()
Ejemplo n.º 5
0
 def __init__(self, namespace=None):
     if namespace is not None and not isinstance(namespace, basestring):
         raise TypeError("namespace must be a string")
     if namespace is None:
         namespace = Configuration.get_instance().get_safe(
             "Main", "CommandNamespace")
     self._namespace = namespace
     self._commands = {}
     self._load_commands()
Ejemplo n.º 6
0
    def cim_namespace(cls):
        """
        This returns default cim namespace, the connection object will be
        nested into before being passed to associated function.
        It can be overriden in few ways:

            1. by setting ``[CIM] Namespace`` option in configuration
            2. by giving ``--namespace`` argument on command line to the
               ``lmi`` meta-command
            3. by setting ``NAMESPACE`` property in declaration of command

        Higher number means higher priority.
        """
        return Configuration.get_instance().namespace
Ejemplo n.º 7
0
    def cim_namespace(cls):
        """
        This returns default cim namespace, the connection object will be
        nested into before being passed to associated function.
        It can be overriden in few ways:

            1. by setting ``[CIM] Namespace`` option in configuration
            2. by giving ``--namespace`` argument on command line to the
               ``lmi`` meta-command
            3. by setting ``NAMESPACE`` property in declaration of command

        Higher number means higher priority.
        """
        return Configuration.get_instance().namespace
Ejemplo n.º 8
0
def eval_respl(expr, conn, namespace=None, cache=None):
    """
    Evaluate LMIReSpL expression on particular broker.

    :param string expr: Expression to evaluate.
    :param conn: Connection object.
    :param string namespace: Optional CIM namespace where CIM classes will be
        searched.
    :param dictionary cache: Optional cache speeding up evaluation.
    :returns: ``True`` if requirements in expression are satisfied.
    :rtype: boolean
    """
    if namespace is None:
        namespace = Configuration.get_instance().namespace
    stack = []
    pvget = functools.partial(get_profile_version, conn, cache=cache)
    cvget = functools.partial(get_class_version, conn,
            namespace=namespace, cache=cache)
    pr = parser.bnf_parser(stack, pvget, cvget)
    pr.parseString(expr, parseAll=True)
    # Now evaluate starting non-terminal created on stack.
    return stack[0]()
Ejemplo n.º 9
0
import urlparse

from lmi.scripts.common import Configuration
from lmi.scripts.common import get_logger

PYTHON_EGG_NAME = "openlmi-scripts"

RE_NETLOC = re.compile(r'^((?P<username>[^:@]+)(:(?P<password>[^@]+))?@)?'
                       r'(?P<hostname>[^:]+)(:(?P<port>\d+))?$')

DEFAULT_LOGGING_CONFIG = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'console': {
            'format': Configuration.default_options()['ConsoleFormat'],
            'datefmt': '%Y-%m-%d %H:%M:%S'
        },
        'file': {
            'format': Configuration.default_options()['FileFormat'],
            'datefmt': '%Y-%m-%d %H:%M:%S'
        }
    },
    'handlers': {
        'console': {
            'class': "logging.StreamHandler",
            'level': logging.ERROR,
            'formatter': 'console',
        },
        'file': {
            'class': "logging.FileHandler",
Ejemplo n.º 10
0
import urlparse

from lmi.scripts.common import Configuration
from lmi.scripts.common import get_logger

PYTHON_EGG_NAME = "openlmi-scripts"

RE_NETLOC = re.compile(r'^((?P<username>[^:@]+)(:(?P<password>[^@]+))?@)?'
        r'(?P<hostname>[^:]+)(:(?P<port>\d+))?$')

DEFAULT_LOGGING_CONFIG = {
    'version' : 1,
    'disable_existing_loggers': True,
    'formatters' : {
        'console' : {
            'format': Configuration.default_options()['ConsoleFormat'],
            'datefmt' : '%Y-%m-%d %H:%M:%S'
        },
        'file' : {
            'format' : Configuration.default_options()['FileFormat'],
            'datefmt' : '%Y-%m-%d %H:%M:%S'
        }
    },
    'handlers' : {
        'console': {
            'class' : "logging.StreamHandler",
            'level' : logging.ERROR,
            'formatter': 'console',
        },
        'file' : {
            'class' : "logging.FileHandler",