Duncan
Duncan

Reputation: 257

How to fix problems with displaying verbatim tag in YAML?

I have a problem with dumping a YAML file with the verbatim tag. How can I fix the encoding problem?
I try to dump YAML by this way

import sys
from ruamel.yaml import YAML

class Entry:
    yaml_tag = '!<!entry>'

    def __init__(self, value, style=None):
        self.value = value
        self.style = style

    @classmethod
    def to_yaml(cls, representer, node):
        return representer.represent_mapping(cls.yaml_tag, node.value, node.style)

    @classmethod
    def from_yaml(cls, constructor, node):
        return cls(node.value, node.style)

data = {
    'steps': [
        Entry({
            'id': 'Entry-1',
            'actions': [],
        })
    ],
}

yaml = YAML(typ='rt')
yaml.register_class(Entry)
yaml.dump(data, sys.stdout)

But in the output instead of !<!entry> I get it !%3C%21entry%3E.

steps:
- !%3C%21entry%3E
  id: Entry-1
  actions: []

Upvotes: 2

Views: 236

Answers (1)

Anthon
Anthon

Reputation: 76842

There is currently no way to work with absolute tags. ruamel.yaml can read them but not dump them.

I suggest you postprocess the output to revert the encoded tag start and end, which is relatively easy:

import sys
from ruamel.yaml import YAML

class Entry:
    yaml_tag = '!<!entry>'

    def __init__(self, value, style=None):
        self.value = value
        self.style = style

    @classmethod
    def to_yaml(cls, representer, node):
        return representer.represent_mapping(cls.yaml_tag, node.value, node.style)

    @classmethod
    def from_yaml(cls, constructor, node):
        return cls(node.value, node.style)

data = {
    'steps': [
        Entry({
            'id': 'Entry-1',
            'actions': [],
        })
    ],
}

def post(s):
    PAT = '!%3C%21'
    res = []
    for line in s.splitlines(True):
        while PAT in line:
            start, rest = line.split(PAT, 1)
            line = start + '!<!' + (rest.replace('%3E', '>'))
        res.append(line)
    return ''.join(res)


yaml2 = YAML(typ='rt')
yaml2.register_class(Entry)
yaml2.dump(data, sys.stdout, transform=post)

which gives:

steps:
- !<!entry>
  id: Entry-1
  actions: []

It should be able to deal with multiple tags on the same output line. It is a bit ugly, but it at least it seems to be effective.

Upvotes: 2

Related Questions