OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Miscellaneous node types. | 6 """Miscellaneous node types. |
7 """ | 7 """ |
8 | 8 |
9 import os.path | 9 import os.path |
10 import re | 10 import re |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 return self.parent._IsValidChild(child) | 79 return self.parent._IsValidChild(child) |
80 | 80 |
81 | 81 |
82 class IfNode(SplicingNode): | 82 class IfNode(SplicingNode): |
83 """A node for conditional inclusion of resources. | 83 """A node for conditional inclusion of resources. |
84 """ | 84 """ |
85 | 85 |
86 def MandatoryAttributes(self): | 86 def MandatoryAttributes(self): |
87 return ['expr'] | 87 return ['expr'] |
88 | 88 |
89 def _IsValidChild(self, child): | |
90 return (isinstance(child, (ThenNode, ElseNode)) or | |
91 super(IfNode, self)._IsValidChild(child)) | |
92 | |
93 def EndParsing(self): | |
94 children = self.children | |
95 if any(isinstance(node, (ThenNode, ElseNode)) for node in children): | |
96 if (len(children) != 2 or not isinstance(children[0], ThenNode) or | |
97 not isinstance(children[1], ElseNode)): | |
98 raise exception.UnexpectedChild( | |
99 '<if> element must be <if><then>...</then><else>...</else></if>') | |
100 | |
89 def ActiveChildren(self): | 101 def ActiveChildren(self): |
90 if not self.EvaluateCondition(self.attrs['expr']): | 102 cond = self.EvaluateCondition(self.attrs['expr']) |
91 return [] | 103 if len(self.children) == 2 and isinstance(self.children[0], ThenNode): |
Jói
2012/10/16 11:21:11
This is slightly hard to follow; I think a comment
| |
92 return super(IfNode, self).ActiveChildren() | 104 return self.children[0 if cond else 1].ActiveChildren() |
105 else: | |
106 return super(IfNode, self).ActiveChildren() if cond else [] | |
107 | |
108 | |
109 class ThenNode(SplicingNode): | |
110 """A <then> node. Can only appear directly inside an <if> node.""" | |
111 pass | |
112 | |
113 | |
114 class ElseNode(SplicingNode): | |
115 """An <else> node. Can only appear directly inside an <if> node.""" | |
116 pass | |
93 | 117 |
94 | 118 |
95 class PartNode(SplicingNode): | 119 class PartNode(SplicingNode): |
96 """A node for inclusion of sub-grd (*.grp) files. | 120 """A node for inclusion of sub-grd (*.grp) files. |
97 """ | 121 """ |
98 | 122 |
99 def __init__(self): | 123 def __init__(self): |
100 super(PartNode, self).__init__() | 124 super(PartNode, self).__init__() |
101 self.started_inclusion = False | 125 self.started_inclusion = False |
102 | 126 |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
475 by parameters of the same name. | 499 by parameters of the same name. |
476 """ | 500 """ |
477 node = IdentifierNode() | 501 node = IdentifierNode() |
478 node.StartParsing('identifier', parent) | 502 node.StartParsing('identifier', parent) |
479 node.HandleAttribute('name', name) | 503 node.HandleAttribute('name', name) |
480 node.HandleAttribute('id', id) | 504 node.HandleAttribute('id', id) |
481 node.HandleAttribute('comment', comment) | 505 node.HandleAttribute('comment', comment) |
482 node.HandleAttribute('systemid', systemid) | 506 node.HandleAttribute('systemid', systemid) |
483 node.EndParsing() | 507 node.EndParsing() |
484 return node | 508 return node |
OLD | NEW |