From 18598b671bc67542e14bab8aadfcb9f806fd0e14 Mon Sep 17 00:00:00 2001 From: Felipe Monteiro Date: Sat, 6 Oct 2018 22:16:12 -0400 Subject: [PATCH] fix: Stop adding unwanted newlines in multiline strings This patch set fixes a `pegleg site render` issue in which pyyaml is adding unwanted (multiple) newlines in multiline strings in the output. In other words, currently, `pegleg site render` keeps injecting additional newline characters in multiline certificates, such that each newline is rendered, in effect, as two newlines. For example: -----BEGIN CERTIFICATE----- MIIDSDCCAjCgAwIBAgIUegkh/antB1XyDVHdP5dv+0MZyBcwDQYJKoZIhvcNAQEL BQAwKjETMBEGA1UEChMKS3ViZXJuZXRlczETMBEGA1UEAxMKa3ViZXJuZXRlczAe Currently renders as: -----BEGIN CERTIFICATE----- MIIDSDCCAjCgAwIBAgIUegkh/antB1XyDVHdP5dv+0MZyBcwDQYJKoZIhvcNAQEL BQAwKjETMBEGA1UEChMKS3ViZXJuZXRlczETMBEGA1UEAxMKa3ViZXJuZXRlczAe This patch set adds code to pegleg.engine.__init__ to use the solution prescribed in [0] to force pyyaml to ensure that the rendered output for such multiline strings matches the original input. Note that this requires using safe_dump(_all) which should be used in Pegleg ubiquitously for the same reason as [1]. Using safe_dump(_all) for all CLI actions will be done in a follow up change. [0] https://stackoverflow.com/questions/45004464/yaml-dump-adding-unwanted-newlines-in-multiline-strings [1] https://review.openstack.org/#/c/578240/ Change-Id: I3d53b310869e5eacf3d7a17b51ff9ae462d64736 --- pegleg/engine/__init__.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/pegleg/engine/__init__.py b/pegleg/engine/__init__.py index 1be97f21..f4c42b72 100644 --- a/pegleg/engine/__init__.py +++ b/pegleg/engine/__init__.py @@ -12,8 +12,30 @@ # See the License for the specific language governing permissions and # limitations under the License. +import yaml + # flake8: noqa -from . import lint -from . import repository -from . import site -from . import type +from pegleg.engine import lint +from pegleg.engine import repository +from pegleg.engine import site +from pegleg.engine import type + + +def __represent_multiline_yaml_str(): + """Compel ``yaml`` library to use block style literals for multi-line + strings to prevent unwanted multiple newlines. + + """ + + yaml.SafeDumper.org_represent_str = yaml.SafeDumper.represent_str + + def repr_str(dumper, data): + if '\n' in data: + return dumper.represent_scalar( + 'tag:yaml.org,2002:str', data, style='|') + return dumper.org_represent_str(data) + + yaml.add_representer(str, repr_str, Dumper=yaml.SafeDumper) + + +__represent_multiline_yaml_str()