>From 3245cdfd1d8d5f4e7ac918159040d8953e8e1d19 Mon Sep 17 00:00:00 2001 From: Richard Dale Date: Thu, 12 Jul 2018 15:17:16 +0100 Subject: [PATCH] Handle trailing spaces in build commands A sequence of command lines where one or more of contain trailing spaces is converted to a DoubleQuotedScalarString by the YAML parser. Handle this type and remove any trailing spaces before converting the YAML. --- defs2bst/bstDumper.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/defs2bst/bstDumper.py b/defs2bst/bstDumper.py index a2f4418..2d3f2c0 100644 --- a/defs2bst/bstDumper.py +++ b/defs2bst/bstDumper.py @@ -72,6 +72,19 @@ class BstDumper(yaml.SafeDumper): return dumper.represent_scalar(u'tag:yaml.org,2002:str', data, style='|') + # If a line in a build command ends in trailing spaces, the commands + # are returned as a yaml.scalarstring.DoubleQuotedScalarString + # + # Remove the trailing spaces before converting to yaml + # + def _represent_double_quoted_scalarstring(cls, dumper, data): + if data.count('\n') == 0: + return yaml.representer.SafeRepresenter.represent_str(dumper, str) + lines = "\n".join([line.rstrip() for line in data.split('\n')]) + data = yaml.scalarstring.DoubleQuotedScalarString(lines) + return dumper.represent_scalar(u'tag:yaml.org,2002:str', + data, style='|') + def _represent_bytes(cls, dumper, orig_data): fallback_representer = yaml.representer.SafeRepresenter.represent_bytes try: @@ -96,6 +109,8 @@ class BstDumper(yaml.SafeDumper): self.add_representer(bytes, self._represent_bytes) self.add_representer(yaml.scalarstring.PreservedScalarString, yaml.representer.RoundTripRepresenter.represent_preserved_scalarstring) + self.add_representer(yaml.scalarstring.DoubleQuotedScalarString, + self._represent_double_quoted_scalarstring) def bst_dump(node, filename): -- 2.7.4