Prevent multiple pending UpdatePolicy tasks.
[chromium-blink-merge.git] / tools / metrics / common / models.py
blob3ba301c4eec8bfd368f0d983c9c24c7cb47ef4aa
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
3 # found in the LICENSE file.
5 """Model types for describing description xml models."""
7 from xml.dom import minidom
9 import sys
10 import os
12 import pretty_print_xml
15 def GetComments(node):
16 """Extracts comments in the current node.
18 Args:
19 node: The DOM node to extract comments from.
20 Returns:
21 A list of comment DOM nodes.
22 """
23 return [n for n in node.childNodes if n.nodeType == minidom.Node.COMMENT_NODE]
26 def PutComments(node, comments):
27 """Append comments to the DOM node.
29 Args:
30 node: The DOM node to write comments to.
31 comments: A list of comment DOM nodes.
32 """
33 for n in comments:
34 node.appendChild(n)
37 class NodeType(object):
38 """Base type for a type of XML node.
40 Args:
41 dont_indent: True iff this node should not have it's children indented
42 when pretty printing.
43 extra_newlines: None or a triple of integers describing the number of
44 newlines that should be printed (after_open, before_close, after_close)
45 single_line: True iff this node may be squashed into a single line.
46 """
47 def __init__(self, tag,
48 dont_indent=False,
49 extra_newlines=None,
50 single_line=False):
51 self.tag = tag
52 self.dont_indent = dont_indent
53 self.extra_newlines = extra_newlines
54 self.single_line = single_line
56 def Unmarshall(self, node):
57 return None
59 def Marshall(self, doc, obj):
60 return None
62 def GetAttributes(self):
63 return []
65 def GetNodeTypes(self):
66 return {self.tag: self}
69 class TextNodeType(NodeType):
70 """A type for simple nodes that just have a tag and some text content.
72 Unmarshalls nodes to strings.
74 Args:
75 tag: The name of XML tag for this type of node.
76 """
77 def __init__(self, tag, **kwargs):
78 NodeType.__init__(self, tag, **kwargs)
80 def __str__(self):
81 return 'TextNodeType("%s")' % self.tag
83 def Unmarshall(self, node):
84 return node.firstChild.nodeValue.strip()
86 def Marshall(self, doc, obj):
87 node = doc.createElement(self.tag)
88 node.appendChild(doc.createTextNode(obj))
89 return node
92 class ChildType(object):
93 """Metadata about a nodes children.
95 Args:
96 attr: The field name of the parents model object storing the child's model.
97 node_type: The NodeType of the child.
98 multiple: True if the child can be repeated.
99 """
100 def __init__(self, attr, node_type, multiple):
101 self.attr = attr
102 self.node_type = node_type
103 self.multiple = multiple
106 class ObjectNodeType(NodeType):
107 """A complex node type that has attributes or other nodes as children.
109 Unmarshalls nodes to objects.
111 Args:
112 tag: The name of XML tag for this type of node.
113 int_attributes: A list of names of integer attributes.
114 float_attributes: A list of names of float attributes.
115 string_attributes: A list of names of string attributes.
116 children: A list of ChildTypes describing the objects children.
118 def __init__(self, tag,
119 int_attributes=[],
120 float_attributes=[],
121 string_attributes=[],
122 children=[],
123 **kwargs):
124 NodeType.__init__(self, tag, **kwargs)
125 self.int_attributes = int_attributes
126 self.float_attributes = float_attributes
127 self.string_attributes = string_attributes
128 self.children = children
130 def __str__(self):
131 return 'ObjectNodeType("%s")' % self.tag
133 def Unmarshall(self, node):
134 obj = {}
136 obj['comments'] = GetComments(node)
138 for attr in self.int_attributes:
139 obj[attr] = int(node.getAttribute(attr))
141 for attr in self.float_attributes:
142 obj[attr] = float(node.getAttribute(attr))
144 for attr in self.string_attributes:
145 obj[attr] = node.getAttribute(attr)
147 for child in self.children:
148 nodes = node.getElementsByTagName(child.node_type.tag)
149 if child.multiple:
150 obj[child.attr] = [child.node_type.Unmarshall(n) for n in nodes]
151 else:
152 obj[child.attr] = child.node_type.Unmarshall(nodes[0])
153 return obj
155 def Marshall(self, doc, obj):
156 node = doc.createElement(self.tag)
157 attributes = (self.int_attributes +
158 self.float_attributes +
159 self.string_attributes)
160 for attr in attributes:
161 node.setAttribute(attr, str(obj[attr]))
163 PutComments(node, obj['comments'])
165 for child in self.children:
166 if child.multiple:
167 for o in obj[child.attr]:
168 node.appendChild(child.node_type.Marshall(doc, o))
169 else:
170 node.appendChild(child.node_type.Marshall(doc, obj[child.attr]))
171 return node
173 def GetAttributes(self):
174 return self.int_attributes + self.float_attributes + self.string_attributes
176 def GetNodeTypes(self):
177 types = {self.tag: self}
178 for child in self.children:
179 types.update(child.node_type.GetNodeTypes())
180 return types
183 class DocumentType(object):
184 """Model for the root of an XML description file.
186 Args:
187 root_type: A NodeType describing the root tag of the document.
189 def __init__(self, root_type):
190 self.root_type = root_type
192 def Parse(self, input_file):
193 tree = minidom.parseString(input_file)
194 comments = GetComments(tree)
195 return comments, self.root_type.Unmarshall(
196 tree.getElementsByTagName(self.root_type.tag)[0])
198 def GetPrintStyle(self):
199 types = self.root_type.GetNodeTypes()
200 return pretty_print_xml.XmlStyle(
201 {t: types[t].GetAttributes() for t in types},
202 {t: types[t].extra_newlines for t in types if types[t].extra_newlines},
203 [t for t in types if types[t].dont_indent],
204 [t for t in types if types[t].single_line])
206 def ToXML(self, comments, obj):
207 doc = minidom.Document()
208 for comment in comments:
209 doc.appendChild(comment)
210 doc.appendChild(self.root_type.Marshall(doc, obj))
211 return doc
213 def PrettyPrint(self, comments, obj):
214 return self.GetPrintStyle().PrettyPrintNode(self.ToXML(comments, obj))