Module escapeddict |
[hide private]
[frames] | no frames]

# Source Code for Module escapeddict

     1  #============================================================================
     2  #Name        : escapeddict.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  """ This class enables developer to use ${xxx} pattern in their dict and
    21      get them replaced recursively.
    22  """
    23  import re
    24  import types
    25  import UserDict
    26
    27
    28 -class _CustomArray(list):
    29      """ Internal class
    30      """
    31 -    def __str__(self):
    32          string = ""
    33          for elem in self:
    34              string += " "+elem
    35          return string
    36
    37
    38 -class EscapedDict(UserDict.UserDict):
    39      """ Implements a dictionary that escapes the key values recursively. """
    40
    41 -    def __init__(self, dict={}, failonerror=False):
    42          UserDict.UserDict.__init__(self, dict)
    43          self.__failonerror = failonerror
    44
    45 -    def __getitem__(self, key):
    46          """ Overrides the usual __getitem__ to insert values of other keys referenced in this key's
    47          value. """
    48          if key in self.data:
    49              value = self.data[key]
    50              result = value
    51              if isinstance(value, types.ListType):
    52                  result = _CustomArray()
    53                  for elem in value:
    54                      (string, changes) = re.subn(r'\${(?P<name>[._a-zA-Z0-9]+)}', r'%(\g<name>)s', elem)
    55                      if changes > 0:
    56                          result.append(string % self)
    57                      else:
    58                          result.append(elem)
    59              else:
    60                  (string, changes) = re.subn(r'\${(?P<name>[._a-zA-Z0-9]+)}', r'%(\g<name>)s', value)
    61                  if changes > 0:
    62                      result = string % self
    63              return result
    64          elif not self.__failonerror:
    65              return "${%s}" % key
    66          raise KeyError("Could not find key '%s'" % key)
    67
    68
    69
    70 -def escapeString(input_string, config):
    71      """ Escape a string recursively.
    72
    73      :param input_string: the string to be escaped.
    74      :param config: a dictionnary containing the values to escape.
    75      :return: the escaped string.
    76      """
    77      data = EscapedDict(config)
    78      match = re.search(r'\${(?P<name>[._a-zA-Z0-9]+)}', input_string)
    79      if match != None:
    80          for property_name in match.groups():
    81              property_value = data[property_name]
    82              property_value = re.sub(r'\\', r'\\\\', property_value)
    83              input_string = re.sub('\${' + property_name + '}', property_value, input_string)
    84      return input_string
    85