587
|
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 |
|
628
|
41 |
# pylint: disable=W0622
|
|
42 |
def __init__(self, dict=None, failonerror=False):
|
|
43 |
if dict == None:
|
|
44 |
dict = {}
|
587
|
45 |
UserDict.UserDict.__init__(self, dict)
|
|
46 |
self.__failonerror = failonerror
|
|
47 |
|
|
48 |
def __getitem__(self, key):
|
|
49 |
""" Overrides the usual __getitem__ to insert values of other keys referenced in this key's
|
|
50 |
value. """
|
|
51 |
if key in self.data:
|
|
52 |
value = self.data[key]
|
|
53 |
result = value
|
|
54 |
if isinstance(value, types.ListType):
|
|
55 |
result = _CustomArray()
|
|
56 |
for elem in value:
|
|
57 |
(string, changes) = re.subn(r'\${(?P<name>[._a-zA-Z0-9]+)}', r'%(\g<name>)s', elem)
|
|
58 |
if changes > 0:
|
|
59 |
result.append(string % self)
|
|
60 |
else:
|
|
61 |
result.append(elem)
|
|
62 |
else:
|
|
63 |
(string, changes) = re.subn(r'\${(?P<name>[._a-zA-Z0-9]+)}', r'%(\g<name>)s', value)
|
|
64 |
if changes > 0:
|
|
65 |
result = string % self
|
|
66 |
return result
|
|
67 |
elif not self.__failonerror:
|
|
68 |
return "${%s}" % key
|
|
69 |
raise KeyError("Could not find key '%s'" % key)
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
def escapeString(input_string, config):
|
|
74 |
""" Escape a string recursively.
|
|
75 |
|
|
76 |
:param input_string: the string to be escaped.
|
|
77 |
:param config: a dictionnary containing the values to escape.
|
|
78 |
:return: the escaped string.
|
|
79 |
"""
|
|
80 |
data = EscapedDict(config)
|
|
81 |
match = re.search(r'\${(?P<name>[._a-zA-Z0-9]+)}', input_string)
|
|
82 |
if match != None:
|
|
83 |
for property_name in match.groups():
|
|
84 |
property_value = data[property_name]
|
|
85 |
property_value = re.sub(r'\\', r'\\\\', property_value)
|
|
86 |
input_string = re.sub('\${' + property_name + '}', property_value, input_string)
|
|
87 |
return input_string
|
|
88 |
|
|
89 |
|