Example #1
0
 def format_node(cls, mapping, metric):
     if mapping.tag in [
             'tag:yaml.org,2002:str', Bytes2Kibibytes.yaml_tag,
             Number.yaml_tag, StripExtraDash.yaml_tag
     ]:
         return yaml.ScalarNode(mapping.tag, mapping.value.format(**metric))
     elif mapping.tag == 'tag:yaml.org,2002:map':
         values = []
         for key, value in mapping.value:
             values.append((yaml.ScalarNode(key.tag, key.value),
                            cls.format_node(value, metric)))
         return yaml.MappingNode(mapping.tag, values)
     elif mapping.tag in [ArrayItem.yaml_tag, ValueItem.yaml_tag]:
         values = []
         for seq in mapping.value:
             map_values = list()
             for key, value in seq.value:
                 if key.value == 'SELECT':
                     map_values.append((yaml.ScalarNode(key.tag, key.value),
                                        cls.format_node(value, metric)))
                 else:
                     map_values.append((yaml.ScalarNode(key.tag,
                                                        key.value), value))
             values.append(yaml.MappingNode(seq.tag, map_values))
         return yaml.SequenceNode(mapping.tag, values)
     elif mapping.tag in [MapValue.yaml_tag]:
         values = []
         for key, value in mapping.value:
             if key.value == 'VALUE':
                 values.append((yaml.ScalarNode(key.tag, key.value),
                                cls.format_node(value, metric)))
             else:
                 values.append((yaml.ScalarNode(key.tag, key.value), value))
         return yaml.MappingNode(mapping.tag, values)
     return mapping
Example #2
0
 def represent_YAMLDict(self, mapping):
     value = []
     node = yaml.MappingNode(u'tag:yaml.org,2002:map',
                             value,
                             flow_style=None)
     if self.alias_key is not None:
         self.represented_objects[self.alias_key] = node
     best_style = True
     if hasattr(mapping, 'items'):
         mapping = mapping.items()
     for item_key, item_value in mapping:
         node_key = self.represent_data(item_key)
         node_value = self.represent_data(item_value)
         if not (isinstance(node_key, yaml.ScalarNode)
                 and not node_key.style):
             best_style = False
         if not (isinstance(node_value, yaml.ScalarNode)
                 and not node_value.style):
             best_style = False
         value.append((node_key, node_value))
     if self.default_flow_style is not None:
         node.flow_style = self.default_flow_style
     else:
         node.flow_style = best_style
     return node
Example #3
0
def _represent_ordered_dict(dumper, tag, mapping, flow_style=None):
    # PyYAML representer for ordered dicts. Used internally.

    value = []
    node = yaml.MappingNode(tag, value, flow_style=flow_style)
    if dumper.alias_key is not None:
        dumper.represented_objects[dumper.alias_key] = node
    best_style = True
    if hasattr(mapping, 'items'):
        mapping = list(mapping.items())
        # The only real difference between
        # BaseRepresenter.represent_mapping and this function is that
        # we omit the sort here. Ugh!
    for item_key, item_value in mapping:
        node_key = dumper.represent_data(item_key)
        node_value = dumper.represent_data(item_value)
        if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
            best_style = False
        if not (isinstance(node_value, yaml.ScalarNode)
                and not node_value.style):
            best_style = False
        value.append((node_key, node_value))
    if flow_style is None:
        if dumper.default_flow_style is not None:
            node.flow_style = dumper.default_flow_style
        else:
            node.flow_style = best_style
    return node
Example #4
0
def represent_odict(dump, tag, mapping, flow_style=None):
    """Like BaseRepresenter.represent_mapping, but does not issue the sort().

    http://blog.elsdoerfer.name/2012/07/26/make-pyyaml-output-an-ordereddict/
    """
    value = list()
    node = yaml.MappingNode(tag, value, flow_style=flow_style)
    if dump.alias_key is not None:
        dump.represented_objects[dump.alias_key] = node
    best_style = True
    if hasattr(mapping, "items"):
        mapping = mapping.items()
    for item_key, item_value in mapping:
        node_key = dump.represent_data(item_key)
        node_value = dump.represent_data(item_value)
        if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
            best_style = False
        if not (isinstance(node_value, yaml.ScalarNode)
                and not node_value.style):
            best_style = False
        value.append((node_key, node_value))
    if flow_style is None:
        if dump.default_flow_style is not None:
            node.flow_style = dump.default_flow_style
        else:
            node.flow_style = best_style
    return node
def dump_format(dump, tag, mapping, flow_style=None):
    """
    Better output formatting for YAML dictionaries
    """
    value = []
    node = yaml.MappingNode(tag, value, flow_style=flow_style)
    if dump.alias_key is not None:
        dump.represented_objects[dump.alias_key] = node
    best_style = True
    if hasattr(mapping, 'items'):
        mapping = mapping.items()
    for item_key, item_value in mapping:
        node_key = dump.represent_data(item_key)
        node_value = dump.represent_data(item_value)
        if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
            best_style = False
        if not (isinstance(node_value, yaml.ScalarNode)
                and not node_value.style):
            best_style = False
        value.append((node_key, node_value))
    if flow_style is None:
        if dump.default_flow_style is not None:
            node.flow_style = dump.default_flow_style
        else:
            node.flow_style = best_style
    return node
Example #6
0
def represent_odict(dump, tag, mapping, flow_style=None):
    """Taken from https://gist.github.com/miracle2k/3184458
    OrderedDict doesn't get dumped by default, but we want to
    preserve the order, so we have to add another representer
    to PyYAML to achieve this.
    """
    value = []
    node = yaml.MappingNode(tag, value, flow_style=flow_style)
    if dump.alias_key is not None:
        dump.represented_objects[dump.alias_key] = node
    best_style = True
    if hasattr(mapping, 'items'):
        mapping = mapping.items()
    for item_key, item_value in mapping:
        node_key = dump.represent_data(item_key)
        node_value = dump.represent_data(item_value)
        if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
            best_style = False
        if not (isinstance(node_value, yaml.ScalarNode)
                and not node_value.style):
            best_style = False
        value.append((node_key, node_value))
    if flow_style is None:
        if dump.default_flow_style is not None:
            node.flow_style = dump.default_flow_style
        else:
            node.flow_style = best_style
    return node
Example #7
0
def represent_odict(dump, tag, mapping, flow_style=None):
    """For writing out OrderedDicts for easier user-end configuration.

    This is not my original code and was provided by Michael Elsdorfer
    https://gist.github.com/miracle2k/3184458#file-odict-py
    """
    value = []
    node = yaml.MappingNode(tag, value, flow_style=flow_style)
    if dump.alias_key is not None:
        dump.represented_objects[dump.alias_key] = node
    best_style = True
    if hasattr(mapping, 'items'):
        mapping = mapping.items()
    for item_key, item_value in mapping:
        node_key = dump.represent_data(item_key)
        node_value = dump.represent_data(item_value)
        if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
            best_style = False
        if not (isinstance(node_value, yaml.ScalarNode) and not node_value.style):
            best_style = False
        value.append((node_key, node_value))
    if flow_style is None:
        if dump.default_flow_style is not None:
            node.flow_style = dump.default_flow_style
        else:
            node.flow_style = best_style
    return node
Example #8
0
    def representTextStruct(dumper, data):
        dataList = [
            #(stringNode('index'), stringNode('0x' + myhex(data.index, 4))),
        ]

        if len(data.indices) != 1:
            extraIndexList = data.indices
            assert (extraIndexList == sorted(extraIndexList))

            nameList = [stringNode(getTextName(x)) for x in extraIndexList]
            dataList.append((stringNode('name'),
                             yaml.SequenceNode('tag:yaml.org,2002:seq',
                                               nameList)))

            extraIndexList = [
                intNode('0x' + myhex(x & 0xff, 2)) for x in extraIndexList
            ]
            dataList.append((stringNode('index'),
                             yaml.SequenceNode('tag:yaml.org,2002:seq',
                                               extraIndexList)))
        else:
            assert (data.indices[0] == data.index)
            dataList.append(stringMap('name', data.name))
            dataList.append((stringNode('index'),
                             intNode('0x' + myhex(data.index & 0xff, 2))))

        dataList.append((stringNode('text'),
                         dumper.represent_scalar(stringTag, data.textData,
                                                 '|')))
        if not data.hasNullTerminator:
            dataList.append((stringNode('null_terminator'),
                             boolNode(data.hasNullTerminator)))

        return yaml.MappingNode('tag:yaml.org,2002:map', dataList)
Example #9
0
def Effect_repr(dumper, obj):
    ' Custom dump, avoid empty items, use flow style for calls_made '
    values = []
    if obj.calls_made is not None:
        # Render calls_made as a flow list
        values.append((
            dumper.represent_str(nice_name('calls_made')),
            dumper.represent_sequence('tag:yaml.org,2002:seq', obj.calls_made,
                                      True),
        ))
    if obj.exception is not None:
        values.append((
            dumper.represent_str(nice_name('exception')),
            dumper.represent_str(obj.exception),
        ))
    if obj.local_changes is not None:
        values.append((
            dumper.represent_str(nice_name('local_changes')),
            dumper.represent_dict(obj.local_changes),
        ))
    if obj.returns is not None:
        values.append((
            dumper.represent_str(nice_name('returns')),
            dumper.represent_str(obj.returns),
        ))
    return yaml.MappingNode(StEffect.yaml_tag, values)
def represent_odict(dump, tag, mapping, flow_style=None):
    """
    Dump an ordered dictionary to YAML, maintaining the key order but making it
    look like a normal dictionary (without the !!python/object extra stuff).

    From https://gist.github.com/miracle2k/3184458
    """
    value = []
    node = yaml.MappingNode(tag, value, flow_style=flow_style)
    if dump.alias_key is not None:
        dump.represented_objects[dump.alias_key] = node
    best_style = True
    if hasattr(mapping, 'items'):
        mapping = mapping.items()
    for item_key, item_value in mapping:
        node_key = dump.represent_data(item_key)
        node_value = dump.represent_data(item_value)
        if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
            best_style = False
        if not (isinstance(node_value, yaml.ScalarNode)
                and not node_value.style):
            best_style = False
        value.append((node_key, node_value))
    if flow_style is None:
        if dump.default_flow_style is not None:
            node.flow_style = dump.default_flow_style
        else:
            node.flow_style = best_style
    return node
def represent_mapping(dumper, tag, mapping, flow_style=None, sort=False):
    value = []  # type: ignore
    node = yaml.MappingNode(tag, value, flow_style=flow_style)
    best_style = True
    if hasattr(mapping, 'items'):
        mapping = list(mapping.items())
        if sort:
            try:
                mapping = sorted(mapping)
            except TypeError:
                pass
    for item_key, item_value in mapping:
        node_key = dumper.represent_data(item_key)
        node_value = dumper.represent_data(item_value)
        if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
            best_style = False
        if not (isinstance(node_value, yaml.ScalarNode)
                and not node_value.style):
            best_style = False
        value.append((node_key, node_value))
    if flow_style is None:
        if dumper.default_flow_style is not None:
            node.flow_style = dumper.default_flow_style
        else:
            node.flow_style = best_style
    return node
Example #12
0
    def represent_mapping(self, tag, mapping="0,0", flow_style=None):
        def insideFunc(inside):
            return "Inside"

        value = []
        node = yaml.MappingNode(tag, value, flow_style=flow_style)
        if self.alias_key is not None:
            self.represented_objects[self.alias_key] = node
        best_style = True
        testFunc = outsideFunc()
        testFunc = insideFunc()
        testFunc = outsideClass()
        testFunc = Dumper.insideClass()
        if hasattr(mapping, 'items'):
            mapping = list(mapping.items())
        for item_key, item_value in mapping:
            node_key = self.represent_data(item_key)
            node_value = self.represent_data(item_value)
            if not (isinstance(node_key, yaml.ScalarNode)
                    and not node_key.style):
                best_style = False
            if not (isinstance(node_value, yaml.ScalarNode)
                    and not node_value.style):
                best_style = False
            value.append((node_key, node_value))
        if flow_style is None:
            if self.default_flow_style is not None:
                node.flow_style = self.default_flow_style
            else:
                node.flow_style = best_style
        return node
Example #13
0
 def represent_ordereddict(self, data):
     value = []
     for item_key, item_value in data.items():
         node_key = self.represent_data(item_key)
         node_value = self.represent_data(item_value)
         value.append((node_key, node_value))
     return yaml.MappingNode(u'tag:yaml.org,2002:map', value)
Example #14
0
 def represent_mapping(self, tag, mapping, flow_style=None):
     value = []
     node = yaml.MappingNode(tag, value, flow_style=flow_style)
     if self.alias_key is not None:
         self.represented_objects[self.alias_key] = node
     best_style = True
     if hasattr(mapping, 'items'):
         mapping = list(mapping.items())
         if not isinstance(mapping, SortedDict):
             mapping.sort()
     for item_key, item_value in mapping:
         node_key = self.represent_data(item_key)
         node_value = self.represent_data(item_value)
         if not (isinstance(node_key, yaml.ScalarNode)
                 and not node_key.style):
             best_style = False
         if not (isinstance(node_value, yaml.ScalarNode)
                 and not node_value.style):
             best_style = False
         value.append((node_key, node_value))
     if flow_style is None:
         if self.default_flow_style is not None:
             node.flow_style = self.default_flow_style
         else:
             node.flow_style = best_style
     return node
Example #15
0
def represent_odict(dump, tag, mapping, flow_style=None):
    """Like BaseRepresenter.represent_mapping, but does not issue the sort().
    """
    value = []
    node = yaml.MappingNode(tag, value, flow_style=flow_style)
    if dump.alias_key is not None:
        dump.represented_objects[dump.alias_key] = node
    best_style = True
    if hasattr(mapping, 'items'):
        mapping = list(mapping.items())
    for item_key, item_value in mapping:
        node_key = dump.represent_data(item_key)
        node_value = dump.represent_data(item_value)
        if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
            best_style = False
        if not (isinstance(node_value, yaml.ScalarNode)
                and not node_value.style):
            best_style = False
        value.append((node_key, node_value))
    if flow_style is None:
        if dump.default_flow_style is not None:
            node.flow_style = dump.default_flow_style
        else:
            node.flow_style = best_style
    return node
Example #16
0
def _represent_ordereddict(dump, tag, mapping, flow_style=None):
    """PyYAML representer function for OrderedDict.
    This is needed for yaml.safe_dump() to support OrderedDict.

    Courtesy:
    http://blog.elsdoerfer.name/2012/07/26/make-pyyaml-output-an-ordereddict/
    """
    value = []
    node = yaml.MappingNode(tag, value, flow_style=flow_style)
    if dump.alias_key is not None:
        dump.represented_objects[dump.alias_key] = node
    best_style = True
    if hasattr(mapping, 'items'):
        mapping = mapping.items()
    for item_key, item_value in mapping:
        node_key = dump.represent_data(item_key)
        node_value = dump.represent_data(item_value)
        if not (isinstance(node_key, yaml.ScalarNode) and
                not node_key.style):
                    best_style = False    # pylint: disable=bad-indentation
        if not (isinstance(node_value, yaml.ScalarNode) and
                not node_value.style):
                    best_style = False    # pylint: disable=bad-indentation
        value.append((node_key, node_value))
    if flow_style is None:
        if dump.default_flow_style is not None:
            node.flow_style = dump.default_flow_style
        else:
            node.flow_style = best_style
    return node
Example #17
0
 def ordereddict_representer(dumper, data):
     value = []
     node = yaml.MappingNode("tag:yaml.org,2002:map", 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 node
Example #18
0
 def to_yaml(cls, dumper, data):
     return yaml.MappingNode(cls.yaml_tag, value=[
         (yaml.ScalarNode(tag='tag:yaml.org,2002:str', value='start'),
          yaml.ScalarNode(tag='tag:yaml.org,2002:str', value=TimeStamp.format_datetime(data.start))),
         (yaml.ScalarNode(tag='tag:yaml.org,2002:str', value='run'),
          yaml.ScalarNode(tag='tag:yaml.org,2002:str', value=TimeStamp.format_duration(data.run))),
         (yaml.ScalarNode(tag='tag:yaml.org,2002:str', value='end'),
          yaml.ScalarNode(tag='tag:yaml.org,2002:str', value=TimeStamp.format_datetime(data.end)))
     ])
Example #19
0
 def represent_ordered_dict(self, odict):
     tag = u'tag:yaml.org,2002:map'
     value = []
     node = yaml.MappingNode(tag, value, flow_style=None)
     for item_key, item_value in odict.items():
         node_key = self.represent_data(item_key)
         node_value = self.represent_data(item_value)
         value.append((node_key, node_value))
     return node
Example #20
0
 def read_configuration(cls, config_file):
     """read YAML configuration file"""
     # load YAML events/measurements definition
     f = open(config_file, 'r')
     doc_yaml = yaml.compose(f)
     f.close()
     # split events & measurements definitions
     measurements, events = list(), list()
     for key, value in doc_yaml.value:
         if value.tag == Measurements.yaml_tag:
             measurements.append((key, value))
         if value.tag == Events.yaml_tag:
             events.append((key, value))
     measurements_yaml = yaml.MappingNode(u'tag:yaml.org,2002:map',
                                          measurements)
     measurements_stream = yaml.serialize(measurements_yaml)
     events_yaml = yaml.MappingNode(u'tag:yaml.org,2002:map', events)
     events_stream = yaml.serialize(events_yaml)
     # return event & measurements definition
     return events_stream, measurements_stream
Example #21
0
def orderedDict_representer(dumper, data):
    mappingSequence = []
    n = yaml.MappingNode(u'tag:yaml.org,2002:map',
                         mappingSequence,
                         flow_style=False)
    dumper.represented_objects[id(data)] = n
    for item_key, item_value in data.iteritems():
        node_key = dumper.represent_data(item_key)
        node_value = dumper.represent_data(item_value)
        mappingSequence.append((node_key, node_value))

    return n
Example #22
0
    def represent_ordered_mapping(self, data):
        value = []
        node = yaml.MappingNode(u'tag:yaml.org,2002:map',
                                value, flow_style=False)

        if self.alias_key is not None:
            self.represented_objects[self.alias_key] = node

        for item_key, item_value in data.items():
            node_key = self.represent_data(item_key)
            node_value = self.represent_data(item_value)
            value.append((node_key, node_value))

        return node
Example #23
0
def represent_ordereddict(dumper, data):
    # TODO: Again, adjust for preferred flow style, and other stylistic details
    # NOTE: For block style this uses the compact omap notation, but for flow style
    # it does not.
    values = []
    node = yaml.SequenceNode(u'tag:yaml.org,2002:seq', values, flow_style=True)
    if dumper.alias_key is not None:
        dumper.represented_objects[dumper.alias_key] = node
    for key, value in data.items():
        key_item = dumper.represent_data(key)
        value_item = dumper.represent_data(value)
        node_item = yaml.MappingNode(u'tag:yaml.org,2002:map', [(key_item, value_item)],
                                     flow_style=False)
        values.append(node_item)
    return node
Example #24
0
def represent_ordered_mapping(dumper, tag, data):
    # TODO: Again, adjust for preferred flow style, and other stylistic details
    # NOTE: For block style this uses the compact omap notation, but for flow style
    # it does not.

    # TODO: Need to see if I can figure out a mechanism so that classes that
    # use this representer can specify which values should use flow style
    values = []
    node = yaml.SequenceNode(tag, values, flow_style=dumper.default_flow_style)
    if dumper.alias_key is not None:
        dumper.represented_objects[dumper.alias_key] = node
    for key, value in data.items():
        key_item = dumper.represent_data(key)
        value_item = dumper.represent_data(value)
        node_item = yaml.MappingNode(YAML_OMAP_TAG, [(key_item, value_item)],
                                     flow_style=False)
        values.append(node_item)
    return node
Example #25
0
def represent_mapping(dump, tag, mapping, flow_style=None):
    value = []
    node = yaml.MappingNode(tag, value, flow_style=flow_style)
    if dump.alias_key is not None:
        dump.represented_objects[dump.alias_key] = node
    best_style = True
    for item_key, item_value in mapping.iteritems():
        node_key = dump.represent_data(item_key)
        node_value = dump.represent_data(item_value)
        if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style):
            best_style = False
        if not (isinstance(node_value, yaml.ScalarNode)
                and not node_value.style):
            best_style = False
        value.append((node_key, node_value))
    if flow_style is None:
        if dump.default_flow_style is not None:
            node.flow_style = dump.default_flow_style
        else:
            node.flow_style = best_style
    return node
Example #26
0
 def represent_ordered_mapping(self, tag, omap):
     value = []
     node = yaml.MappingNode(tag, value)
     if self.alias_key is not None:
         self.represented_objects[self.alias_key] = node
     best_style = True
     for item_key, item_value in omap.iteritems():
         node_key = self.represent_data(item_key)
         node_value = self.represent_data(item_value)
         if not (isinstance(node_key, yaml.ScalarNode)
                 and not node_key.style):
             best_style = False
         if not (isinstance(node_value, yaml.ScalarNode)
                 and not node_value.style):
             best_style = False
         value.append((node_key, node_value))
     if self.default_flow_style is not None:
         node.flow_style = self.default_flow_style
     else:
         node.flow_style = best_style
     return node
Example #27
0
def represent_mapping(dumper: yaml.dumper.BaseDumper,
                      mapping: dict,
                      flow_style: bool = None) -> yaml.MappingNode:
    """
    Represents mappings without sorting items.

    Args:
        dumper: The YAML dumper to use
        mapping: The mapping to represent
        flow_style: The mapping flow style

    Returns:
        The YAML mapping node
    """
    value = []
    node = yaml.MappingNode(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
                            value,
                            flow_style=flow_style)
    best_style = True

    if dumper.alias_key is not None:
        dumper.represented_objects[dumper.alias_key] = node

    for item_key, item_value in mapping.items():
        node_key = dumper.represent_data(item_key)
        node_value = dumper.represent_data(item_value)

        if (not (isinstance(node_key, yaml.ScalarNode) and not node_key.style)
                or not (isinstance(node_value, yaml.ScalarNode)
                        and not node_value.style)):
            best_style = False

        value.append((node_key, node_value))

    if flow_style is None:
        node.flow_style = (dumper.default_flow_style
                           if dumper.default_flow_style is not None else
                           best_style)

    return node
Example #28
0
        def represent_mapping(self, tag, mapping, flow_style=None):
            """
            This is a combination of the Python 2 and 3 versions of this method
            in the PyYAML library to allow the required key ordering via the
            ColumnOrderList object.  The Python 3 version insists on turning the
            items() mapping into a list object and sorting, which results in
            alphabetical order for the column keys.
            """
            value = []
            node = yaml.MappingNode(tag, value, flow_style=flow_style)
            if self.alias_key is not None:
                self.represented_objects[self.alias_key] = node
            best_style = True
            if hasattr(mapping, 'items'):
                mapping = mapping.items()
                if hasattr(mapping, 'sort'):
                    mapping.sort()
                else:
                    mapping = list(mapping)
                    try:
                        mapping = sorted(mapping)
                    except TypeError:
                        pass

            for item_key, item_value in mapping:
                node_key = self.represent_data(item_key)
                node_value = self.represent_data(item_value)
                if not (isinstance(node_key, yaml.ScalarNode)
                        and not node_key.style):
                    best_style = False
                if not (isinstance(node_value, yaml.ScalarNode)
                        and not node_value.style):
                    best_style = False
                value.append((node_key, node_value))
            if flow_style is None:
                if self.default_flow_style is not None:
                    node.flow_style = self.default_flow_style
                else:
                    node.flow_style = best_style
            return node
Example #29
0
def ordered_mapping_to_node(self, tag, mapping, flow_style=None):
    mapping_val = []

    mapping_node = yaml.MappingNode(tag, mapping_val, flow_style=flow_style)
    if self.alias_key is not None:
        self.represented_objects[self.alias_key] = mapping_node

    map_items = list(mapping.items()) if hasattr(mapping, 'items') else mapping

    for key, value in map_items:
        n_key = self.represent_data(key)
        n_value = self.represent_data(value)
        mapping_val.append((n_key, n_value))

        default_or_best = lambda: self.default_flow_style\
            if self.default_flow_style is not None else\
            tell_best_node_style(n_key, n_value)

        mapping_node.flow_style = flow_style if flow_style is not None\
            else default_or_best()

    return mapping_node
Example #30
0
    def compose_mapping_node(self, anchor):
        """
        Override mapping node composition in order to move
        start mark from anchor to first key.

        Copied for https://github.com/yaml/pyyaml/blob/master/lib/yaml/composer.py  # noqa
        """
        if anchor is None:
            return super(TxYamlLoader, self).compose_mapping_node(anchor)
        else:
            start_event = self.get_event()
            tag = start_event.tag
            if tag is None or tag == '!':
                tag = self.resolve(
                    yaml.MappingNode, None, start_event.implicit
                )
            node = yaml.MappingNode(tag, [],
                    start_event.start_mark, None,
                    flow_style=start_event.flow_style)
            if anchor is not None:
                self.anchors[anchor] = node

            index = -1
            start_mark = start_event.start_mark
            while not self.check_event(yaml.events.MappingEndEvent):
                item_key = self.compose_node(node, None)
                key_start_mark = item_key.start_mark

                if index == -1 or key_start_mark.index < index :
                    index = key_start_mark.index
                    start_mark = key_start_mark

                item_value = self.compose_node(node, item_key)
                node.value.append((item_key, item_value))

            end_event = self.get_event()
            node.end_mark = end_event.end_mark
            node.start_mark = start_mark
            return node