--- a/usr/src/lib/install_doc/data_object/__init__.py Fri Nov 05 09:59:41 2010 -0700
+++ b/usr/src/lib/install_doc/data_object/__init__.py Tue Nov 09 17:39:01 2010 +0000
@@ -751,8 +751,9 @@
else:
return matched
- def str_replace_paths_refs(self, orig_string, value_separator=","):
- """ Replace the %{...} references to DOC values with quoted strings
+ def str_replace_paths_refs(self, orig_string, value_separator=",",
+ quote=False):
+ """ Replace the %{...} references to DOC values with strings.
Returns a new string with the values replaced.
@@ -761,13 +762,23 @@
val1,val2,val3
- The value for each matched object is generated calling 'repr(obj)'
- since that generates a more accurate representation of values
- than calling str() - including the automatic quoting of strings.
+ By, default, with a 'quote' value of False, the value of each matched
+ object is created by calling 'str()' on the object or attribute.
+
+ If the value of 'quote' is True, then the value for each matched object
+ will be surrounded by single-quotes, unless the value itself is a
+ string, then the quoting is done using repr() which handles escaping of
+ quotes within strings too.
If the references are not valid, the exceptions from the
DataObjectBase.find_path() will be passed on.
"""
+
+ if quote:
+ quote_str="'"
+ else:
+ quote_str=""
+
new_string = orig_string
for matches in re.finditer(
DataObjectBase.__STRING_REPLACEMENT_RE, orig_string):
@@ -779,10 +790,17 @@
# Combine with SEPARATOR, using repr to get usable text values
# since it automatically quotes if it is a string.
for value in found_list:
+ if quote and isinstance(value, basestring):
+ # Use repr() for strings since it handles quoting and
+ # escaping of quotes in strings well.
+ new_val_str = repr(value)
+ else:
+ new_val_str = "%s%s%s" % \
+ (quote_str, str(value), quote_str)
if value_str == "":
- value_str = repr(value)
+ value_str = new_val_str
else:
- value_str += "%s%s" % (value_separator, repr(value))
+ value_str += "%s%s" % (value_separator, new_val_str)
self.logger.debug("Replacing reference to '%s' with '%s'" %
(matches.group(0), value_str))
--- a/usr/src/lib/install_doc/test/test_data_object_paths.py Fri Nov 05 09:59:41 2010 -0700
+++ b/usr/src/lib/install_doc/test/test_data_object_paths.py Tue Nov 09 17:39:01 2010 +0000
@@ -569,21 +569,41 @@
"""
self.assertEquals(self.data_objs["data_obj"].str_replace_paths_refs(
"value=%{//child_5_2_3_1.name}"),
+ "value=child_5_2_3_1")
+
+ self.assertEquals(self.data_objs["data_obj"].str_replace_paths_refs(
+ "value=%{//child_5_2_3_1.name}", quote=True),
"value='child_5_2_3_1'")
self.assertEquals(self.data_objs["data_obj"].str_replace_paths_refs(
"allvalues=%{//[@simple_data_object.SimpleDataObject2].name}"),
+ "allvalues=child_1,child_2_1,child_2_1_1,child_2_1_1_1,"
+ "child_2_1_1_2,child_4")
+
+ self.assertEquals(self.data_objs["data_obj"].str_replace_paths_refs(
+ "allvalues=%{//[@simple_data_object.SimpleDataObject2].name}",
+ quote=True),
"allvalues='child_1','child_2_1','child_2_1_1','child_2_1_1_1',"
"'child_2_1_1_2','child_4'")
self.assertEquals(self.data_objs["data_obj"].str_replace_paths_refs(
- "allvalues=%{/child_2//.name}"),
+ "allvalues=%{/child_2//.name}", quote=True),
"allvalues="
"'child_2_1','child_2_1_1','child_2_1_1_1','child_2_1_1_2'")
self.assertEquals(self.data_objs["data_obj"].str_replace_paths_refs(
+ "allvalues=%{/child_2//.name}"),
+ "allvalues="
+ "child_2_1,child_2_1_1,child_2_1_1_1,child_2_1_1_2")
+
+ self.assertEquals(self.data_objs["data_obj"].str_replace_paths_refs(
"allvalues=%{/child_2//.name}", value_separator=" "),
"allvalues="
+ "child_2_1 child_2_1_1 child_2_1_1_1 child_2_1_1_2")
+
+ self.assertEquals(self.data_objs["data_obj"].str_replace_paths_refs(
+ "allvalues=%{/child_2//.name}", value_separator=" ", quote=True),
+ "allvalues="
"'child_2_1' 'child_2_1_1' 'child_2_1_1_1' 'child_2_1_1_2'")
def test_dobj_path_multiple_str_subst(self):
@@ -592,7 +612,7 @@
self.assertEquals(self.data_objs["data_obj"].str_replace_paths_refs(
"value1=%{//child_3_1_1.name}"
" value2=%{//child_5_2_1.name}"),
- "value1='child_3_1_1' value2='child_5_2_1'")
+ "value1=child_3_1_1 value2=child_5_2_1")
if __name__ == '__main__':