| 18 | | __all__ = ['Node', 'Alias', 'Group', 'Action', 'Variable', 'Grammar', 'Help', |
|---|
| 19 | | 'LazyHelp', 'Word', 'String', 'URI', 'LDAPDN', 'Integer', 'Float', |
|---|
| 20 | | 'IP', 'Hostname', 'Host', 'EMail', 'File'] |
|---|
| | 18 | __all__ = ['Node', 'Alias', 'Set', 'Action', 'Variable', 'Grammar', 'Help', |
|---|
| | 19 | 'LazyHelp', 'Word', 'String', 'URI', 'LDAPDN', 'Integer', 'Float', 'IP', |
|---|
| | 20 | 'Hostname', 'Host', 'EMail', 'File'] |
|---|
| 394 | | class Group(Node): |
|---|
| 395 | | """Group all children together at this location. |
|---|
| 396 | | |
|---|
| 397 | | >>> import sys |
|---|
| 398 | | >>> from cly.parser import HelpParser, Context |
|---|
| 399 | | >>> group = Group(1) |
|---|
| 400 | | >>> top = Node('Top', group(name='top', one=Node('One'), two=Node('Two')), three=Node('Three')) |
|---|
| 401 | | >>> context = Context(None, None) |
|---|
| 402 | | >>> list(HelpParser(context, top)) |
|---|
| 403 | | [(0, 'three', 'Three'), (1, 'one', 'One'), (1, 'two', 'Two')] |
|---|
| 404 | | """ |
|---|
| 405 | | |
|---|
| | 400 | class Set(Node): |
|---|
| | 401 | """Apply settings to all ancestor nodes. |
|---|
| | 402 | |
|---|
| | 403 | Terminates application of settings on any deeper Set node. |
|---|
| | 404 | |
|---|
| | 405 | Before applying settings: |
|---|
| | 406 | |
|---|
| | 407 | >>> top = Node('Top')(one=Node('One'), |
|---|
| | 408 | ... two=Node('Two', three=Node('Three'))) |
|---|
| | 409 | >>> [node.traversals for node in top.walk()] |
|---|
| | 410 | [1, 1, 1, 1] |
|---|
| | 411 | |
|---|
| | 412 | And after applying settings: |
|---|
| | 413 | |
|---|
| | 414 | >>> apply = Set(traversals=0)(top) |
|---|
| | 415 | >>> [node.traversals for node in top.walk()] |
|---|
| | 416 | [0, 0, 0, 0] |
|---|
| | 417 | """ |
|---|
| 413 | | Node.__call__(self, *args, **kwargs) |
|---|
| 414 | | for child in self: |
|---|
| 415 | | child.group = self.group |
|---|
| 416 | | return self |
|---|
| | 425 | result = Node.__call__(self, *args, **kwargs) |
|---|
| | 426 | |
|---|
| | 427 | def stop_on_ancestors(node): |
|---|
| | 428 | return node is self or not isinstance(node, Set) |
|---|
| | 429 | |
|---|
| | 430 | for child in self.walk(predicate=stop_on_ancestors): |
|---|
| | 431 | if child is self: |
|---|
| | 432 | Node.__call__(self, **self._apply) |
|---|
| | 433 | else: |
|---|
| | 434 | child(**self._apply) |
|---|
| | 435 | |
|---|
| | 436 | return result |
|---|