| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 """Types for building models of metric description xml files. | 4 """Types for building models of metric description xml files. |
| 5 | 5 |
| 6 UMA uses several XML files to allow clients to describe the metrics that they | 6 UMA uses several XML files to allow clients to describe the metrics that they |
| 7 collect, e.g. | 7 collect, e.g. |
| 8 https://chromium.googlesource.com/chromium/src/+/master/tools/metrics/rappor/rap
por.xml | 8 https://chromium.googlesource.com/chromium/src/+/master/tools/metrics/rappor/rap
por.xml |
| 9 | 9 |
| 10 These types can be used to build models that describe the canonical formatted | 10 These types can be used to build models that describe the canonical formatted |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 | 69 |
| 70 class NodeType(object): | 70 class NodeType(object): |
| 71 """Base type for a type of XML node. | 71 """Base type for a type of XML node. |
| 72 | 72 |
| 73 Args: | 73 Args: |
| 74 indent: True iff this node should have its children indented when pretty | 74 indent: True iff this node should have its children indented when pretty |
| 75 printing. | 75 printing. |
| 76 extra_newlines: None or a triple of integers describing the number of | 76 extra_newlines: None or a triple of integers describing the number of |
| 77 newlines that should be printed (after_open, before_close, after_close) | 77 newlines that should be printed (after_open, before_close, after_close) |
| 78 single_line: True iff this node may be squashed into a single line. | 78 single_line: True iff this node may be squashed into a single line. |
| 79 alphabetization: A (tag, keyfn) pair, which specifies the tag of the |
| 80 children that should be sorted, and a function to get the sort key from |
| 81 the xml node. |
| 79 """ | 82 """ |
| 80 __metaclass__ = abc.ABCMeta | 83 __metaclass__ = abc.ABCMeta |
| 81 | 84 |
| 82 def __init__(self, tag, | 85 def __init__(self, tag, |
| 83 indent=True, | 86 indent=True, |
| 84 extra_newlines=None, | 87 extra_newlines=None, |
| 85 single_line=False): | 88 single_line=False, |
| 89 alphabetization=None): |
| 86 self.tag = tag | 90 self.tag = tag |
| 87 self.indent = indent | 91 self.indent = indent |
| 88 self.extra_newlines = extra_newlines | 92 self.extra_newlines = extra_newlines |
| 89 self.single_line = single_line | 93 self.single_line = single_line |
| 94 self.alphabetization = alphabetization |
| 90 | 95 |
| 91 @abc.abstractmethod | 96 @abc.abstractmethod |
| 92 def Unmarshall(self, node): | 97 def Unmarshall(self, node): |
| 93 """Extracts the content of the node to an object. | 98 """Extracts the content of the node to an object. |
| 94 | 99 |
| 95 Args: | 100 Args: |
| 96 node: The XML node to extract data from. | 101 node: The XML node to extract data from. |
| 97 | 102 |
| 98 Returns: | 103 Returns: |
| 99 An object extracted from the node. | 104 An object extracted from the node. |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 Returns: | 367 Returns: |
| 363 An XML style object. | 368 An XML style object. |
| 364 """ | 369 """ |
| 365 types = self.root_type.GetNodeTypes() | 370 types = self.root_type.GetNodeTypes() |
| 366 return pretty_print_xml.XmlStyle( | 371 return pretty_print_xml.XmlStyle( |
| 367 attribute_order={t: types[t].GetAttributes() for t in types}, | 372 attribute_order={t: types[t].GetAttributes() for t in types}, |
| 368 tags_that_have_extra_newline={t: types[t].extra_newlines for t in types | 373 tags_that_have_extra_newline={t: types[t].extra_newlines for t in types |
| 369 if types[t].extra_newlines}, | 374 if types[t].extra_newlines}, |
| 370 tags_that_dont_indent=[t for t in types if not types[t].indent], | 375 tags_that_dont_indent=[t for t in types if not types[t].indent], |
| 371 tags_that_allow_single_line=[t for t in types if types[t].single_line], | 376 tags_that_allow_single_line=[t for t in types if types[t].single_line], |
| 372 tags_alphabetization_rules={}) | 377 tags_alphabetization_rules={t: types[t].alphabetization for t in types |
| 378 if types[t].alphabetization}) |
| 373 | 379 |
| 374 def _ToXML(self, obj): | 380 def _ToXML(self, obj): |
| 375 """Converts an object into an XML document. | 381 """Converts an object into an XML document. |
| 376 | 382 |
| 377 Args: | 383 Args: |
| 378 obj: An object to serialize to XML. | 384 obj: An object to serialize to XML. |
| 379 | 385 |
| 380 Returns: | 386 Returns: |
| 381 An XML minidom Document object. | 387 An XML minidom Document object. |
| 382 """ | 388 """ |
| 383 doc = minidom.Document() | 389 doc = minidom.Document() |
| 384 self.root_type.MarshallIntoNode(doc, doc, obj) | 390 self.root_type.MarshallIntoNode(doc, doc, obj) |
| 385 return doc | 391 return doc |
| 386 | 392 |
| 387 def PrettyPrint(self, obj): | 393 def PrettyPrint(self, obj): |
| 388 """Converts an object into pretty-printed XML as a string. | 394 """Converts an object into pretty-printed XML as a string. |
| 389 | 395 |
| 390 Args: | 396 Args: |
| 391 obj: An object to serialize to XML. | 397 obj: An object to serialize to XML. |
| 392 | 398 |
| 393 Returns: | 399 Returns: |
| 394 A string containing pretty printed XML. | 400 A string containing pretty printed XML. |
| 395 """ | 401 """ |
| 396 return self.GetPrintStyle().PrettyPrintNode(self._ToXML(obj)) | 402 return self.GetPrintStyle().PrettyPrintXml(self._ToXML(obj)) |
| OLD | NEW |