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