1515# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1616# See the License for the specific language governing permissions and
1717# limitations under the License.
18+ import string
19+
1820from .Nodes import RootNode
1921from .Nodes import TemplateNode
2022from .Nodes import EndOfStreamNode
@@ -61,9 +63,6 @@ def _make_template_xml_view(root_node, cache=None):
6163 if cache is None :
6264 cache = {}
6365
64- def escape_format_chars (s ):
65- return s .replace ("{" , "{{" ).replace ("}" , "}}" )
66-
6766 def rec (node , acc ):
6867 if isinstance (node , EndOfStreamNode ):
6968 pass # intended
@@ -90,7 +89,7 @@ def rec(node, acc):
9089 elif isinstance (node , CloseElementNode ):
9190 pass # intended
9291 elif isinstance (node , ValueNode ):
93- acc .append (escape_format_chars (node .children ()[0 ].string ()))
92+ acc .append (to_xml_string (node .children ()[0 ].string ()))
9493 elif isinstance (node , AttributeNode ):
9594 pass # intended
9695 elif isinstance (node , CDataSectionNode ):
@@ -134,6 +133,11 @@ def rec(node, acc):
134133 return "" .join (acc )
135134
136135
136+ class SafeDict (dict ):
137+ def __missing__ (self , key ):
138+ return '{' + key + '}'
139+
140+
137141def _build_record_xml (record , cache = None ):
138142 """
139143 Note, the cache should be local to the Evtx.Chunk.
@@ -145,21 +149,22 @@ def _build_record_xml(record, cache=None):
145149 """
146150 if cache is None :
147151 cache = {}
148-
149152 def rec (root_node ):
150153 f = _make_template_xml_view (root_node , cache = cache )
151154 subs_strs = []
152155 for sub in root_node .fast_substitutions ():
153156 if isinstance (sub , str ):
154- #subs_strs.append(to_xml_string(sub))
155157 subs_strs .append (sub )
156158 elif isinstance (sub , RootNode ):
157159 subs_strs .append (rec (sub ))
158160 elif sub is None :
159161 subs_strs .append ("" )
160162 else :
161163 subs_strs .append (str (sub ))
162- return f .format (* subs_strs )
164+
165+ # maintain substrings like {foo} if foo= isn't a kwarg to format
166+ # via: http://stackoverflow.com/a/17215533/87207
167+ return string .Formatter ().vformat (f , subs_strs , SafeDict ())
163168 xml = rec (record .root ())
164169 return xml
165170
0 commit comments