Skip to content

Commit

Permalink
Allow redefining node anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasloven committed Nov 15, 2019
1 parent 1878c92 commit 5870a3f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ elements:

![lovelace_gen](https://user-images.githubusercontent.com/1299821/62565373-ecd4ac80-b886-11e9-9dcb-c41b43027b2b.png)

## Hidden bonus
With lovelace_gen installed, you'll be able to redefine node anchors in the same file. A feature in the YAML specification, but an error in the engine Home Assistant normally uses...

# FAQ

### How can I do global variables?
Expand Down
25 changes: 25 additions & 0 deletions custom_components/lovelace_gen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,28 @@ def _uncache_file(ldr, node):
async def async_setup(hass, config):
llgen_config.update(config.get("lovelace_gen"));
return True

# Allow redefinition of node anchors
import yaml

def compose_node(self, parent, index):
if self.check_event(yaml.events.AliasEvent):
event = self.get_event()
anchor = event.anchor
if anchor not in self.anchors:
raise yaml.composer.ComposerError(None, None, "found undefined alias %r"
% anchor, event.start_mark)
return self.anchors[anchor]
event = self.peek_event()
anchor = event.anchor
self.descend_resolver(parent, index)
if self.check_event(yaml.events.ScalarEvent):
node = self.compose_scalar_node(anchor)
elif self.check_event(yaml.events.SequenceStartEvent):
node = self.compose_sequence_node(anchor)
elif self.check_event(yaml.events.MappingStartEvent):
node = self.compose_mapping_node(anchor)
self.ascend_resolver()
return node

yaml.composer.Composer.compose_node = compose_node

0 comments on commit 5870a3f

Please sign in to comment.