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