Package ats3 :: Module atsconfigparser |
[hide private]
[frames] | no frames]

# Source Code for Module ats3.atsconfigparser

      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  import configuration
     21  import amara
     22
     23 -class TestXML:
     24 -    def __init__(self, testxml):
     25          self.testxml = testxml
     26          self.doc = amara.parse(testxml)
     27
     28 -    def containsproperty(self, name, value):
     29          for p in self.doc.xml_xpath("//property"):
     30              if str(p.name) == name:
     31                  return str(p.value) == value
     32          return False
     33
     34 -    def containssetting(self, name, value):
     35          for p in self.doc.xml_xpath("//setting"):
     36              if str(p.name) == name:
     37                  return str(p.value) == value
     38          return False
     39
     40 -    def addorreplacesetting(self, name, value):
     41          changed = False
     42          for p in self.doc.xml_xpath("//setting"):
     43              if str(p.name) == name:
     44                  p.value = value
     45                  changed = True
     46          if not changed:
     47              for device in self.doc.test.target.device:
     48                  device.xml_append(self.doc.xml_create_element(u"setting", attributes = {u'name': unicode(name), u'value': unicode(value)}))
     49
     50 -    def containsattribute(self, name, value):
     51          for p in self.doc.xml_xpath("//*[@" + name + "]"):
     52              if p[name] == value:
     53                  return True
     54          return False
     55
     56 -    def replaceattribute(self, name, value):
     57          for p in self.doc.xml_xpath("//*[@" + name + "]"):
     58              p[name] = value
     59
     60 -    def addorreplaceproperty(self, name, value):
     61          changed = False
     62          for p in self.doc.xml_xpath("//property"):
     63              if str(p.name) == name:
     64                  p.value = value
     65                  changed = True
     66          if not changed:
     67              for device in self.doc.test.target.device:
     68                  device.xml_append(self.doc.xml_create_element(u"property", attributes = {u'name': unicode(name), u'value': unicode(value)}))
     69
     70
     71 -class ATSConfigParser:
     72 -    def __init__(self, specfilename):
     73          specfile = open(specfilename)
     74          builder = configuration.NestedConfigurationBuilder(specfile)
     75          self.configs = builder.getConfigurations("common")
     76
     77 -    def properties(self):
     78          props = {}
     79          for config in self.configs:
     80              if (config.type == "properties"):
     81                  for subconfig in config:
     82                      props[subconfig] = config[subconfig]
     83          return props
     84
     85 -    def settings(self):
     86          settings = {}
     87          for config in self.configs:
     88              if (config.type == "settings"):
     89                  for subconfig in config:
     90                      settings[subconfig] = config[subconfig]
     91          return settings
     92
     93 -def converttestxml(specfilename, testxmldata):
     94      specfile = open(specfilename)
     95
     96      builder = configuration.NestedConfigurationBuilder(specfile)
     97      configs = builder.getConfigurations("common")# + builder.getConfigurations("ats3")
     98
     99      testxml = TestXML(testxmldata)
    100
    101      for config in configs:
    102          if (config.type == "properties"):
    103              for subconfig in config:
    104                  testxml.addorreplaceproperty(subconfig, config[subconfig])
    105          if (config.type == "conditional_properties"):
    106              check = config.name.split(',')
    107              if testxml.containsproperty(check[0], check[1]):
    108                  for subconfig in config:
    109                      testxml.addorreplaceproperty(subconfig, config[subconfig])
    110          if (config.type == "settings"):
    111              for subconfig in config:
    112                  testxml.addorreplacesetting(subconfig, config[subconfig])
    113          if (config.type == "conditional_settings"):
    114              check = config.name.split(',')
    115              if testxml.containssetting(check[0], check[1]):
    116                  for subconfig in config:
    117                      testxml.addorreplacesetting(subconfig, config[subconfig])
    118          if (config.type == "attributes"):
    119              for subconfig in config:
    120                  testxml.replaceattribute(subconfig, config[subconfig])
    121          if (config.type == "conditional_attributes"):
    122              check = config.name.split(',')
    123              if testxml.containsattribute(check[0], check[1]):
    124                  for subconfig in config:
    125                      testxml.replaceattribute(subconfig, config[subconfig])
    126
    127      return testxml.doc.xml(indent=u"yes")
    128