Python ruamel.yaml.org() Examples

The following are 11 code examples of ruamel.yaml.org(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ruamel.yaml , or try the search function .
Example #1
Source File: load.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def flatten_mapping(self, node):
        '''
        Support the special case of shallow merging included configuration into an existing mapping
        using the YAML '<<' merge key. Example syntax:

        ```
        retention:
            keep_daily: 1
            <<: !include common.yaml
        ```
        '''
        representer = ruamel.yaml.representer.SafeRepresenter()

        for index, (key_node, value_node) in enumerate(node.value):
            if key_node.tag == u'tag:yaml.org,2002:merge' and value_node.tag == '!include':
                included_value = representer.represent_data(self.construct_object(value_node))
                node.value[index] = (key_node, included_value)

        super(Include_constructor, self).flatten_mapping(node) 
Example #2
Source File: utils.py    From calm-dsl with Apache License 2.0 5 votes vote down vote up
def str_presenter(dumper, data):
    """For handling multiline strings"""
    if len(data.splitlines()) > 1:  # check for multiline string
        return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|")
    return dumper.represent_scalar("tag:yaml.org,2002:str", data) 
Example #3
Source File: _yaml.py    From pyuavcan with MIT License 5 votes vote down vote up
def _represent_decimal(self: ruamel.yaml.BaseRepresenter,
                       data: decimal.Decimal) -> ruamel.yaml.ScalarNode:
    if data.is_finite():
        s = str(_POINT_ZERO_DECIMAL + data)  # The zero addition is to force float-like string representation
    elif data.is_nan():
        s = '.nan'
    elif data.is_infinite():
        s = '.inf' if data > 0 else '-.inf'
    else:
        assert False
    return self.represent_scalar('tag:yaml.org,2002:float', s)  # type: ignore 
Example #4
Source File: inout.py    From patch_linemod with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def float_representer(dumper, value):
    text = '{0:.8f}'.format(value)
    return dumper.represent_scalar(u'tag:yaml.org,2002:float', text) 
Example #5
Source File: inout.py    From sixd_toolkit with MIT License 5 votes vote down vote up
def float_representer(dumper, value):
    text = '{0:.8f}'.format(value)
    return dumper.represent_scalar(u'tag:yaml.org,2002:float', text) 
Example #6
Source File: hardware.py    From uchroma with GNU Lesser General Public License v3.0 5 votes vote down vote up
def represent_hex_quad(dumper, data):
    return dumper.represent_scalar(u'tag:yaml.org,2002:int', '0x%04x' % data) 
Example #7
Source File: config.py    From uchroma with GNU Lesser General Public License v3.0 5 votes vote down vote up
def represent_flow_seq(dumper, data):
    """
    Dump sequences in flow style
    """
    return dumper.represent_sequence(u'tag:yaml.org,2002:seq', data, flow_style=True) 
Example #8
Source File: configure.py    From artman with Apache License 2.0 5 votes vote down vote up
def _represent_ordereddict(dumper, data):
    value = []
    for item_key, item_value in data.items():
        node_key = dumper.represent_data(item_key)
        node_value = dumper.represent_data(item_value)

        value.append((node_key, node_value))

    return yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', value) 
Example #9
Source File: __init__.py    From rasa_nlu with Apache License 2.0 5 votes vote down vote up
def fix_yaml_loader() -> None:
    """Ensure that any string read by yaml is represented as unicode."""

    def construct_yaml_str(self, node):
        # Override the default string handling function
        # to always return unicode objects
        return self.construct_scalar(node)

    yaml.Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
    yaml.SafeLoader.add_constructor(u'tag:yaml.org,2002:str',
                                    construct_yaml_str) 
Example #10
Source File: yamlutils.py    From scriptcwl with Apache License 2.0 5 votes vote down vote up
def str_presenter(dmpr, data):
    """Return correct str_presenter to write multiple lines to a yaml field.


    Source: http://stackoverflow.com/a/33300001
    """
    if is_multiline(data):
        return dmpr.represent_scalar('tag:yaml.org,2002:str', data, style='|')
    return dmpr.represent_scalar('tag:yaml.org,2002:str', data) 
Example #11
Source File: io.py    From rasa-for-botfront with Apache License 2.0 5 votes vote down vote up
def fix_yaml_loader() -> None:
    """Ensure that any string read by yaml is represented as unicode."""

    def construct_yaml_str(self, node):
        # Override the default string handling function
        # to always return unicode objects
        return self.construct_scalar(node)

    yaml.Loader.add_constructor("tag:yaml.org,2002:str", construct_yaml_str)
    yaml.SafeLoader.add_constructor("tag:yaml.org,2002:str", construct_yaml_str)