readers.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:central 作者: viniciuschiele 项目源码 文件源码
def _get_loader(self):
        """
        Get a loader that uses an IgnoreCaseDict for
        complex objects.
        :return yaml.Loader: The loader object.
        """
        # this class was copied from
        # https://github.com/fmenabe/python-yamlordereddictloader/blob/master/yamlordereddictloader.py
        # and adapted to use IgnoreCaseDict

        class Loader(yaml.Loader):
            def __init__(self, *args, **kwargs):
                yaml.Loader.__init__(self, *args, **kwargs)
                self.add_constructor(
                    'tag:yaml.org,2002:map', type(self).construct_yaml_map)
                self.add_constructor(
                    'tag:yaml.org,2002:omap', type(self).construct_yaml_map)

            def construct_yaml_map(self, node):
                data = IgnoreCaseDict()
                yield data
                value = self.construct_mapping(node)
                data.update(value)

            def construct_mapping(self, node, deep=False):
                if isinstance(node, yaml.MappingNode):
                    self.flatten_mapping(node)
                else:
                    raise yaml.constructor.ConstructorError(
                        None, None, 'expected a mapping node, but found %s' % node.id, node.start_mark)

                mapping = IgnoreCaseDict()
                for key_node, value_node in node.value:
                    key = self.construct_object(key_node, deep=deep)
                    try:
                        hash(key)
                    except TypeError as err:
                        raise yaml.constructor.ConstructorError(
                            'while constructing a mapping', node.start_mark,
                            'found unacceptable key (%s)' % err, key_node.start_mark)
                    value = self.construct_object(value_node, deep=deep)
                    mapping[key] = value

                return mapping

        return Loader
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号