Chromium Code Reviews| 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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 68 | 68 |
| 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. |
|
rkaplow
2017/03/30 01:26:20
add alpha comment
Steven Holte
2017/03/30 22:42:23
Done.
| |
| 79 """ | 79 """ |
| 80 __metaclass__ = abc.ABCMeta | 80 __metaclass__ = abc.ABCMeta |
| 81 | 81 |
| 82 def __init__(self, tag, | 82 def __init__(self, tag, |
| 83 indent=True, | 83 indent=True, |
| 84 extra_newlines=None, | 84 extra_newlines=None, |
| 85 single_line=False): | 85 single_line=False, |
| 86 alphabetization=None): | |
| 86 self.tag = tag | 87 self.tag = tag |
| 87 self.indent = indent | 88 self.indent = indent |
| 88 self.extra_newlines = extra_newlines | 89 self.extra_newlines = extra_newlines |
| 89 self.single_line = single_line | 90 self.single_line = single_line |
| 91 self.alphabetization = alphabetization | |
| 90 | 92 |
| 91 @abc.abstractmethod | 93 @abc.abstractmethod |
| 92 def Unmarshall(self, node): | 94 def Unmarshall(self, node): |
| 93 """Extracts the content of the node to an object. | 95 """Extracts the content of the node to an object. |
| 94 | 96 |
| 95 Args: | 97 Args: |
| 96 node: The XML node to extract data from. | 98 node: The XML node to extract data from. |
| 97 | 99 |
| 98 Returns: | 100 Returns: |
| 99 An object extracted from the node. | 101 An object extracted from the node. |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 362 Returns: | 364 Returns: |
| 363 An XML style object. | 365 An XML style object. |
| 364 """ | 366 """ |
| 365 types = self.root_type.GetNodeTypes() | 367 types = self.root_type.GetNodeTypes() |
| 366 return pretty_print_xml.XmlStyle( | 368 return pretty_print_xml.XmlStyle( |
| 367 attribute_order={t: types[t].GetAttributes() for t in types}, | 369 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 | 370 tags_that_have_extra_newline={t: types[t].extra_newlines for t in types |
| 369 if types[t].extra_newlines}, | 371 if types[t].extra_newlines}, |
| 370 tags_that_dont_indent=[t for t in types if not types[t].indent], | 372 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], | 373 tags_that_allow_single_line=[t for t in types if types[t].single_line], |
| 372 tags_alphabetization_rules={}) | 374 tags_alphabetization_rules={t: types[t].alphabetization for t in types |
| 375 if types[t].alphabetization}) | |
| 373 | 376 |
| 374 def _ToXML(self, obj): | 377 def _ToXML(self, obj): |
| 375 """Converts an object into an XML document. | 378 """Converts an object into an XML document. |
| 376 | 379 |
| 377 Args: | 380 Args: |
| 378 obj: An object to serialize to XML. | 381 obj: An object to serialize to XML. |
| 379 | 382 |
| 380 Returns: | 383 Returns: |
| 381 An XML minidom Document object. | 384 An XML minidom Document object. |
| 382 """ | 385 """ |
| 383 doc = minidom.Document() | 386 doc = minidom.Document() |
| 384 self.root_type.MarshallIntoNode(doc, doc, obj) | 387 self.root_type.MarshallIntoNode(doc, doc, obj) |
| 385 return doc | 388 return doc |
| 386 | 389 |
| 387 def PrettyPrint(self, obj): | 390 def PrettyPrint(self, obj): |
| 388 """Converts an object into pretty-printed XML as a string. | 391 """Converts an object into pretty-printed XML as a string. |
| 389 | 392 |
| 390 Args: | 393 Args: |
| 391 obj: An object to serialize to XML. | 394 obj: An object to serialize to XML. |
| 392 | 395 |
| 393 Returns: | 396 Returns: |
| 394 A string containing pretty printed XML. | 397 A string containing pretty printed XML. |
| 395 """ | 398 """ |
| 396 return self.GetPrintStyle().PrettyPrintNode(self._ToXML(obj)) | 399 return self.GetPrintStyle().PrettyPrintXml(self._ToXML(obj)) |
| OLD | NEW |