当前位置: 首页>>代码示例>>Python>>正文


Python SafeConstructor.add_constructor方法代码示例

本文整理汇总了Python中yaml.constructor.SafeConstructor.add_constructor方法的典型用法代码示例。如果您正苦于以下问题:Python SafeConstructor.add_constructor方法的具体用法?Python SafeConstructor.add_constructor怎么用?Python SafeConstructor.add_constructor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在yaml.constructor.SafeConstructor的用法示例。


在下文中一共展示了SafeConstructor.add_constructor方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: construct_yaml_map_with_ordered_dict

# 需要导入模块: from yaml.constructor import SafeConstructor [as 别名]
# 或者: from yaml.constructor.SafeConstructor import add_constructor [as 别名]
        value = self.construct_object(value_node, deep=deep)
        mapping[key] = value
    
    return mapping

BaseConstructor.construct_mapping = construct_ordered_mapping


def construct_yaml_map_with_ordered_dict(self, node):
    data = OrderedDict()
    yield data
    value = self.construct_mapping(node)
    data.update(value)

for t in [u'tag:yaml.org,2002:map', u'tag:yaml.org,2002:omap']:
    SafeConstructor.add_constructor( t, construct_yaml_map_with_ordered_dict )
    Constructor.add_constructor( t, construct_yaml_map_with_ordered_dict )
    yaml.add_constructor( t, construct_yaml_map_with_ordered_dict )


def represent_ordered_mapping(self, tag, mapping, flow_style=None):
    value = []
    node = yaml.MappingNode(tag, value, flow_style=flow_style)
    best_style = True
    
    if self.alias_key is not None:
        self.represented_objects[self.alias_key] = node
    
    if hasattr(mapping, 'items'):
        mapping = list(mapping.items())
    
开发者ID:dsc,项目名称:py-lessly,代码行数:32,代码来源:yaml_omap.py

示例2: parse

# 需要导入模块: from yaml.constructor import SafeConstructor [as 别名]
# 或者: from yaml.constructor.SafeConstructor import add_constructor [as 别名]
    def parse(self, content, **kwargs):
        """ Parses the given YAML content to create stringset and template

        Steps are:
            1. Load yaml content using our custom loader TxYamlLoader that
               in addition to the value for each key notes the `start` and
               `end` index of each node in the file and some metadata.
            2. Flattens the output of the loader to be a list of the form:
               ```
               [{
                   'key': 'string_key1',
                   'value': 'string1',
                   'end': <end_index_of_node>,
                   'start': <start_index_value>,
                   'style': '|, >, ...'
                },
                ...
               ]
               ```
            3. Iterates over the flattened list and for each entry creates an
               OpenString object, appends it to stringset and replace its value
               with the template_replacement in the template.
            4. Returns the (template, stringset) tuple.
        """
        template = []
        stringset = []
        # The first argument of the add_constructor method is the tag you want
        # to handle. If you provide None as an argument, all the unknown tags
        # will be handled by the constructor specified in the second argument.
        # We need this in order parse all unknown custom-tagged values as
        # strings.
        SafeConstructor.add_constructor(None,
                                        SafeConstructor.construct_yaml_str)
        yaml_data = self._load_yaml(content, loader=TxYamlLoader)
        yaml_data = self._get_yaml_data_to_parse(yaml_data)
        # Helper to store the processed data while parsing the file
        self._parsed_data = []
        self._parse_yaml_data(yaml_data, '', '')
        self._parsed_data = sorted(self._parsed_data,
                                   key=lambda node: node.get('start'))

        end = 0
        order = 0
        for node in self._parsed_data:
            start = node.get('start')
            end_ = node.get('end')
            key = node.get('key')
            tag = node.get('tag')
            value = node.get('value')
            style = node.get('style')
            if not value:
                continue
            if isinstance(value, dict) and not all(six.itervalues(value)):
                continue
            string_object = OpenString(
                key, value, context=tag or '', flags=style, order=order,
            )
            stringset.append(string_object)
            order += 1
            template.append(u"{}{}".format(content[end:start],
                                           string_object.template_replacement))
            comment = self._find_comment(content, end, start)
            string_object.developer_comment = comment
            end = end_

        template.append(content[end:])
        template = u''.join(template)
        return template, stringset
开发者ID:transifex,项目名称:openformats,代码行数:70,代码来源:yaml.py


注:本文中的yaml.constructor.SafeConstructor.add_constructor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。