| 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 self.if_then_else = False |
| 96 if any(isinstance(node, (ThenNode, ElseNode)) for node in children): |
| 97 if (len(children) != 2 or not isinstance(children[0], ThenNode) or |
| 98 not isinstance(children[1], ElseNode)): |
| 99 raise exception.UnexpectedChild( |
| 100 '<if> element must be <if><then>...</then><else>...</else></if>') |
| 101 self.if_then_else = True |
| 102 |
| 89 def ActiveChildren(self): | 103 def ActiveChildren(self): |
| 90 if not self.EvaluateCondition(self.attrs['expr']): | 104 cond = self.EvaluateCondition(self.attrs['expr']) |
| 91 return [] | 105 if self.if_then_else: |
| 92 return super(IfNode, self).ActiveChildren() | 106 return self.children[0 if cond else 1].ActiveChildren() |
| 107 else: |
| 108 # Equivalent to having all children inside <then> with an empty <else> |
| 109 return super(IfNode, self).ActiveChildren() if cond else [] |
| 110 |
| 111 |
| 112 class ThenNode(SplicingNode): |
| 113 """A <then> node. Can only appear directly inside an <if> node.""" |
| 114 pass |
| 115 |
| 116 |
| 117 class ElseNode(SplicingNode): |
| 118 """An <else> node. Can only appear directly inside an <if> node.""" |
| 119 pass |
| 93 | 120 |
| 94 | 121 |
| 95 class PartNode(SplicingNode): | 122 class PartNode(SplicingNode): |
| 96 """A node for inclusion of sub-grd (*.grp) files. | 123 """A node for inclusion of sub-grd (*.grp) files. |
| 97 """ | 124 """ |
| 98 | 125 |
| 99 def __init__(self): | 126 def __init__(self): |
| 100 super(PartNode, self).__init__() | 127 super(PartNode, self).__init__() |
| 101 self.started_inclusion = False | 128 self.started_inclusion = False |
| 102 | 129 |
| (...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 477 by parameters of the same name. | 504 by parameters of the same name. |
| 478 """ | 505 """ |
| 479 node = IdentifierNode() | 506 node = IdentifierNode() |
| 480 node.StartParsing('identifier', parent) | 507 node.StartParsing('identifier', parent) |
| 481 node.HandleAttribute('name', name) | 508 node.HandleAttribute('name', name) |
| 482 node.HandleAttribute('id', id) | 509 node.HandleAttribute('id', id) |
| 483 node.HandleAttribute('comment', comment) | 510 node.HandleAttribute('comment', comment) |
| 484 node.HandleAttribute('systemid', systemid) | 511 node.HandleAttribute('systemid', systemid) |
| 485 node.EndParsing() | 512 node.EndParsing() |
| 486 return node | 513 return node |
| OLD | NEW |