#!/usr/bin/python
# -*- coding: latin-1 -*-
# INTEL CONFIDENTIAL
# Copyright 2007 Intel Corporation All Rights Reserved.
#
# The source code contained or described herein and all documents related to the
# source code (Material) are owned by Intel Corporation or its suppliers or
# licensors. Title to the Material remains with Intel Corporation or its
# suppliers and licensors. The Material may contain trade secrets and
# proprietary and confidential information of Intel Corporation and its
# suppliers and licensors, and is protected by worldwide copyright and trade
# secret laws and treaty provisions. No part of the Material may be used,
# copied, reproduced, modified, published, uploaded, posted, transmitted,
# distributed, or disclosed in any way without Intels prior express written
# permission.
# No license under any patent, copyright, trade secret or other intellectual
# property right is granted to or conferred upon you by disclosure or delivery
# of the Materials, either expressly, by implication, inducement, estoppel or
# otherwise. Any license under such intellectual property rights must be express
# and approved by Intel in writing.
#
# Include any supplier copyright notices as supplier requires Intel to use.
#
# Include supplier trademarks or logos as supplier requires Intel to use, preceded
# by an asterisk. An asterisked footnote can be added as follows:
# *Third Party trademarks are the property of their respective owners.
#
# Unless otherwise agreed by Intel in writing, you may not remove or alter this
# notice or any other notice embedded in Materials by Intel or Intels suppliers
# or licensors in any way.

"""This module is the queryProperty utility

"""
__author__ = 'Todd C Davis'
__Product__ = 'Intel_PDK'

import systemMgr, sys, os.path

Usage = """Usage: %s source propertyName {-|propertyValue} [instance=\"INSTANCE\"] [optional attributes]
Usage:               ?
Usage:               source ? [optional attributes]

    '?'             lists sources, property names, and attributes
                    to help with refining queries.
    source          A valid source name, such as SMBIO, FRU, DISK, etc.
    propertyName    A valid property name that exists for a given source.
    -               Displays the current value of a given property.
    propertyValue   Returns 1, if property value matches propertyValue.
                    Returns 0, if values do not match.
    instance="INSTANCE"     Identifies a particular instance of a property.
    [optional attributes]   Qualitfies a particular occurrence of a property.

A single '?' will not list any property names, but lists only sources
and attributes that uniquely identify a set of property names.

Using a '?' with source and attribute options will list the property names for
the uniquely identified source.
Only enough attributes are needed to uniquely identify the source,

e.g.'s

    quertyProperty SMBIOS ? type="0"
    lists all the property names for the type 0 SMBIOS record:

    queryProperty SMBIOS Version - type="0"
    lists the current value for the 'Version' property in the SMIOS type 0 record.

    queryProperty SMBIOS Version -
    lists all the values that have 'Version' as a property name in the SMBIOS record.
"""


if __name__ == '__main__':
    #print "executing queryProperty ... "
    if len(sys.argv) == 1:
        print Usage % os.path.basename(sys.argv[0])
        sys.exit(-1)
    if sys.argv[1].find('?') >= 0:
        sources = systemMgr.getPropertySources(0)
        if sources:
            print "Property sources and attributes available for %s" % os.path.basename(sys.argv[0])
            for src in sources:
                name = src['source']
                del src['source']
                if src:
                    print name,
                    for attr in src.keys()[:-1]:
                        print ' %s="%s"'%(attr,src[attr]),
                    print ' %s="%s"'%(src.keys()[-1],src[src.keys()[-1]])
                else:
                    print name
            sys.exit(0)
        else:
            print "No properties found. Use 'prepareDeployment' or 'addProperty'"
            sys.exit(-1)

    if len(sys.argv) > 2 and sys.argv[2].find('?') >= 0:
        sources = systemMgr.getPropertySources(0)
        for src in sources:
            if src.has_key('source') and src['source'] == sys.argv[1]:
                break
        else:
            print "No properties found for source: %s" % sys.argv[1]
            sys.exit(-1)
        srcattrs = {}
        srcattrs['source'] = sys.argv[1]
        for arg in sys.argv[3:]:
            parts = arg.split('=')
            if not len(parts) == 2:
                print "Invalid attribute format for %s %s: %s" %(os.path.basename(sys.argv[0]),sys.argv[1], arg)
                print Usage % os.path.basename(sys.argv[0])
                sys.exit(-1)
            srcattrs[parts[0]] = parts[1]
        #print srcattrs
        props = systemMgr.getSourceProperties(0,srcattrs)
        if not props:
            print "No properties found for '%s' with optional attributes supplied."%sys.argv[1]
            sys.exit(-1)
        else:
            for prop in props:
                print prop
            sys.exit(0)

    if len(sys.argv) < 4:
        print Usage % os.path.basename(sys.argv[0])
        sys.exit(-1)
    src, name, value, attributes = systemMgr.args2attributes(Usage%os.path.basename(sys.argv[0]), sys.argv[1], sys.argv[2], sys.argv[3],sys.argv[4:])
    ret, value = systemMgr.queryProperty(0, src, name, value, attributes)
    sys.exit(ret)
