buildframework/helium/sf/python/pythoncore/lib/ats3/atsconfigparser.py
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 587 85df38eb4012
child 645 b8d81fa19e7d
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     1
#============================================================================ 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     2
#Name        : atsconfigparser.py
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     3
#Part of     : Helium 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     4
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     5
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     6
#All rights reserved.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     7
#This component and the accompanying materials are made available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     8
#under the terms of the License "Eclipse Public License v1.0"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     9
#which accompanies this distribution, and is available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    10
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    11
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    12
#Initial Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    13
#Nokia Corporation - initial contribution.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    14
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    15
#Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    16
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    17
#Description:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
#===============================================================================
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    20
""" handles the parameters  used in configuring the ATS """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    21
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    22
import configuration
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    23
import amara
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    25
class TestXML:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    26
    """ class used to create XML file"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
    def __init__(self, testxml):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
        """ init file"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
        self.testxml = testxml
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
        self.doc = amara.parse(testxml)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    31
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    32
    def containsproperty(self, name, value):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    33
        """ returns the value of property if it exists or false"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
        for p_temp in self.doc.xml_xpath("//property"):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
            if str(p_temp.name) == name:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
                return str(p_temp.value) == value
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    37
        return False
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
    def containssetting(self, name, value):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
        """ returns the value of setting if it exists or false"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
        for p_temp in self.doc.xml_xpath("//setting"):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
            if str(p_temp.name) == name:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
                return str(p_temp.value) == value
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
        return False
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
    def addorreplacesetting(self, name, value):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
        """ Add or replace 'setting' value """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
        changed = False
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
        for p_temp in self.doc.xml_xpath("//setting"):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
            if str(p_temp.name) == name:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
                p_temp.value = value
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
                changed = True
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
        if not changed:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
            for device in self.doc.test.target.device:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
                device.xml_append(self.doc.xml_create_element(u"setting", attributes = {u'name': unicode(name), u'value': unicode(value)}))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
    def containsattribute(self, name, value):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
        """ returns true or false """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    59
        for p_temp in self.doc.xml_xpath("//*[@" + name + "]"):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
            if p_temp[name] == value:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
                return True
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
        return False
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    63
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
    def replaceattribute(self, name, value):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
        """sets the xpath to the passed in value"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    66
        for p_temp in self.doc.xml_xpath("//*[@" + name + "]"):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    67
            p_temp[name] = value
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
            
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
    def addorreplaceproperty(self, name, value):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
        """ add or replace property value"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    71
        changed = False
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
        for p_temp in self.doc.xml_xpath("//property"):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    73
            if str(p_temp.name) == name:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    74
                p_temp.value = value
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    75
                changed = True
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
        if not changed:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
            for device in self.doc.test.target.device:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    78
                device.xml_append(self.doc.xml_create_element(u"property", attributes = {u'name': unicode(name), u'value': unicode(value)}))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    79
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    80
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    81
class ATSConfigParser:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
    """ ATS configuration parser"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    83
    def __init__(self, specfilename):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    84
        specfile = open(specfilename)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    85
        builder = configuration.NestedConfigurationBuilder(specfile)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    86
        self.configs = builder.getConfigurations("common")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    87
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    88
    def properties(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    89
        """ retrieve the property values"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    90
        props = {}
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    91
        for config in self.configs:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    92
            if (config.type == "properties"):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    93
                for subconfig in config:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    94
                    props[subconfig] = config[subconfig]
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
        return props
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    96
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    97
    def settings(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    98
        """ retrieve the settings values"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    99
        settings = {}
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   100
        for config in self.configs:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   101
            if (config.type == "settings"):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   102
                for subconfig in config:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   103
                    settings[subconfig] = config[subconfig]
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   104
        return settings
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   105
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   106
def converttestxml(specfilename, testxmldata): # pylint: disable=R0912
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   107
    """ convert the specfilename to xml"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   108
    specfile = open(specfilename)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   109
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   110
    builder = configuration.NestedConfigurationBuilder(specfile)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   111
    configs = builder.getConfigurations("common")# + builder.getConfigurations("ats3")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   112
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   113
    testxml = TestXML(testxmldata)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   114
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   115
    for config in configs:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   116
        if (config.type == "properties"):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   117
            for subconfig in config:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   118
                testxml.addorreplaceproperty(subconfig, config[subconfig])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   119
        if (config.type == "conditional_properties"):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   120
            check = config.name.split(',')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   121
            if testxml.containsproperty(check[0], check[1]):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   122
                for subconfig in config:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   123
                    testxml.addorreplaceproperty(subconfig, config[subconfig])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   124
        if (config.type == "settings"):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   125
            for subconfig in config:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   126
                testxml.addorreplacesetting(subconfig, config[subconfig])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   127
        if (config.type == "conditional_settings"):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   128
            check = config.name.split(',')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   129
            if testxml.containssetting(check[0], check[1]):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   130
                for subconfig in config:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   131
                    testxml.addorreplacesetting(subconfig, config[subconfig])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   132
        if (config.type == "attributes"):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   133
            for subconfig in config:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   134
                testxml.replaceattribute(subconfig, config[subconfig])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   135
        if (config.type == "conditional_attributes"):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   136
            check = config.name.split(',')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   137
            if testxml.containsattribute(check[0], check[1]):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   138
                for subconfig in config:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   139
                    testxml.replaceattribute(subconfig, config[subconfig])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   140
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   141
    return testxml.doc.xml(indent=u"yes")