def represent_string( dumper: yaml.Dumper, data: str, tag: str = BaseResolver.DEFAULT_SCALAR_TAG, basicsep: str = "", ) -> yaml.ScalarNode: """Override the normal string emission rules to produce the most readable text output. Args: data: The string to dump tag: (Optional) The YAML tag to use for this scalar. Defaults to the default YAML scalar tag, which does not get printed. basicsep: (Optional) The scalar string separator to use for "simple" strings (ie. strings where there isn't a more specific rule) """ # TODO test cases if "\n" in data: # '|' style means literal block style, so line breaks and formatting are retained. # This will be especially handy for inline code. return dumper.represent_scalar(tag, data, "|") elif len(data) > 65: # Longer lines will be automatically folded (ie. have line breaks # inserted) by PyYAML, which is likely to cause confusion. We # compromise by using the literal block style ('|'), which doesn't # fold, but does require some special block indicators. # TODO need some way to allow folding. Having a helper function probably isn't really good enough. perhaps have the reflow function return a special subclass of string which we can detect here return dumper.represent_scalar(tag, data, "|") else: return dumper.represent_scalar(tag, data, basicsep)
def _uuid_representer(dumper: yaml.Dumper, data: uuid.UUID) -> yaml.Node: """Generate YAML representation for UUID. This produces a scalar node with a tag "!uuid" and value being a regular string representation of UUID. """ return dumper.represent_scalar("!uuid", str(data))
def arrow_representer(dumper: yaml.Dumper, data: arrow.Arrow) -> str: """ Represent an `arrow.arrow.Arrow` object as a scalar in ISO format. ! '2013-05-07T04:24:24+00:00' """ return dumper.represent_scalar("!", data.isoformat("T"))
def str_representer(dumper: yaml.Dumper, data: str) -> yaml.ScalarNode: with_quotes = yaml_is_bool(data) or is_int(data) or is_float(data) return dumper.represent_scalar( yaml.resolver.BaseResolver.DEFAULT_SCALAR_TAG, data, style=("'" if with_quotes else None), )
def enum_to_string_representer(dumper: yaml.Dumper, data: enum.Enum) -> yaml.ScalarNode: ''' Register some sort of enum as being serialized by `str()`. ''' value = str(data) return dumper.represent_scalar(u'tag:yaml.org,2002:str', value)
def literal_string_representer(dumper: yaml.Dumper, data: Union[LiteralString, str] ) -> yaml.ScalarNode: ''' Register FoldedString as the correct style of literal. ''' return dumper.represent_scalar(u'tag:yaml.org,2002:str', str(data), style='|')
def string_representer( dumper: yaml.Dumper, data: str, ) -> yaml.Dumper.represent_scalar: """ Add a custom string representer to use block literals for multiline strings. Args: dumper: A YAML dumper. data: A data string. Returns: YAML dumper.represent_scalar. """ if len(data.splitlines()) > 1: return dumper.represent_scalar(tag='tag:yaml.org,2002:str', value=data, style='|') return dumper.represent_scalar(tag='tag:yaml.org,2002:str', value=data)
def enum_string_value_representer(dumper: yaml.Dumper, data: enum.Enum) -> yaml.ScalarNode: ''' Register some sort of enum as being serialized by str value. ''' value = data.value if not isinstance(value, str): # Hm... can't log... don't want to print... # Just represent this as best we can? value = str(value) return dumper.represent_scalar(u'tag:yaml.org,2002:str', value)
def repr_str(dumper: Dumper, data: str) -> str: """A YAML Representer that handles strings, breaking multi-line strings into something a lot more readable in the yaml output. This is useful for representing long, multiline strings in templates or in stack parameters. :param dumper: The Dumper that is being used to serialize this object :param data: The string to serialize :return: The represented string """ if '\n' in data: return dumper.represent_scalar(u'tag:yaml.org,2002:str', data, style='|') return dumper.represent_str(data)
def _get_string_node( self, dumper: yaml.Dumper, value: Union["_Function", str], tag: str ) -> yaml.Node: """Get a PyYAML node for a function that returns a string. Lots of the intrinsic functions take a single string as the argument, which we echo directly into the output. However, in CloudFormation, this string could be a reference to another function rather than a literal string, and that needs to be handled specially. """ if isinstance(value, _Function): return dumper.represent_dict({f"Fn::{tag}": value}) else: # TODO be able to control the scalar output, perhaps # TODO use represent_string instead, perhaps return dumper.represent_scalar(f"!{tag}", value, style="")
def to_yaml(cls, dumper: yaml.Dumper, data: astropy.time.Time) -> Any: """Convert astropy Time object into YAML format. Parameters ---------- dumper : `yaml.Dumper` YAML dumper instance. data : `astropy.time.Time` Data to be converted. """ if data is not None: # we store time in ISO format but we need full nanosecond # precision so we have to construct intermediate instance to make # sure its precision is set correctly. data = astropy.time.Time(data.tai, precision=9) data = data.to_value("iso") return dumper.represent_scalar(cls.yaml_tag, data)
def _yaml_str_format(dumper: yaml.Dumper, s: str): if '\n' in s: return dumper.represent_scalar(u'tag:yaml.org,2002:str', s, style='|') return dumper.represent_scalar(u'tag:yaml.org,2002:str', s)
def represent_str(dumper: yaml.Dumper, data: str): if "\n" in data: return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|') return dumper.represent_scalar('tag:yaml.org,2002:str', data)
def represent(dumper: yaml.Dumper, data: 'Py'): return dumper.represent_scalar('!Py', data.value)
def _dump_yaml(cls, dumper: yaml.Dumper, source: "YamlModifier") -> typing.Any: """Convert to a yaml node representation for writing to file.""" return dumper.represent_scalar(source.label(), source.value)
def timedelta_representer(dumper: yaml.Dumper, data: timedelta) -> yaml.ScalarNode: return dumper.represent_scalar(TIMEDELTA_TAG, format_timedelta(data))
def time_representer(dumper: yaml.Dumper, data: time) -> yaml.ScalarNode: return dumper.represent_scalar(TIME_TAG, data.isoformat())
def float_representer(dumper: yaml.Dumper, value: float): text = "{0:.9f}".format(value) return dumper.represent_scalar("tag:yaml.org,2002:float", text)
def _path_representer(dumper: yaml.Dumper, path: Path): return dumper.represent_scalar(u"!path", str(path))
def to_yaml(cls, dumper: yaml.Dumper, data): return dumper.represent_scalar(cls.yaml_tag, data._value)