示例#1
0
    def as_yaml_node(self, dumper: yaml.Dumper) -> yaml.Node:
        # Ideally, we would create a tag that contains the object name
        # (eg. "!Bucket") for completeness. Unfortunately, there is no way
        # to then prevent the YAML dumper from printing tags unless it
        # determines that the tag is implicit (ie. the default tag for a
        # mapping node is the tag being used), so we end up just using the
        # default tag.
        # TODO investigate hacking a Dumper subclass like we did for aliasing
        tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG

        # Get all cloud formation attributes that are set, in sorted order
        attributes = [(key, self[key]) for key in self
                      if self.is_attribute_set(key)]

        # Create neater YAML by filtering out empty blocks at this level
        attributes = [(key, value) for key, value in attributes
                      if is_non_empty_attribute(value)]

        # Create neater YAML by filtering out empty entries in sub-lists
        attributes = [(key, remove_empty_values_from_attribute(value))
                      for key, value in attributes]

        # Represent this object as a mapping of it's AWS attributes.
        # Note that `represent_mapping` works on a list of 2-tuples, not a map!
        return dumper.represent_mapping(tag, attributes)
示例#2
0
def ordered_dict_representer(dumper: yaml.Dumper,
                             data:   Mapping) -> yaml.MappingNode:
    '''
    Register OrderedDict in order to be able to dump it.
    '''
    return dumper.represent_mapping(u'tag:yaml.org,2002:map',
                                    data.items(),
                                    flow_style=False)  # block flow style
示例#3
0
def _range_representer(dumper: yaml.Dumper, data: Range) -> yaml.Node:
    begin, end = data

    # pyyaml doesn't output timestamps in flow style as timestamps(?)
    if isinstance(begin, datetime):
        begin = begin.isoformat()
    if isinstance(end, datetime):
        end = end.isoformat()

    return dumper.represent_mapping(
        yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
        (('begin', begin), ('end', end)),
        flow_style=True)
示例#4
0
文件: packages.py 项目: lsst/utils
def pkg_representer(dumper: yaml.Dumper, data: Any) -> yaml.MappingNode:
    """Represent Packages as a simple dict"""
    return dumper.represent_mapping("lsst.utils.packages.Packages",
                                    data,
                                    flow_style=None)
示例#5
0
 def dict_representer(
         dumper: yaml.Dumper,
         data) -> yaml.representer.Representer:  # type: ignore
     return dumper.represent_mapping(_mapping_tag, data.items())
示例#6
0
 def dict_representer(dumper: yaml.Dumper, data: Dict[str, Any]):
     return dumper.represent_mapping(
         yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, data.items())
示例#7
0
 def _dump_yaml(cls, dumper: yaml.Dumper,
                source: "YamlModifier") -> typing.Any:
     """Convert to a yaml node representation for writing to file."""
     if isinstance(source.value, (list, tuple)):
         return dumper.represent_sequence(source.label(), source.value)
     return dumper.represent_mapping(source.label(), source.value)
示例#8
0
 def _dump_yaml(cls, dumper: yaml.Dumper,
                source: "YamlModifier") -> typing.Any:
     """Convert to a yaml node representation for writing to file."""
     return dumper.represent_mapping(source.label(), source.value or {})