Files
project-config/tools/projectconfig_ruamellib.py
Clark Boylan 9b8a1f2796 Disable ruamel automagic line wrapping
Recent releases of ruamel have resulted in weird linewrapping. Ruamel
seems to be aware of this based on the ToDo comment in the diff that
introduced the behavior [0]. Rather than fight this by continuing to
exclude ruamel releases we simply set the width to wrap on to the max
int size. This solution was inspired by this Github comment [1].

This means that we will be responsible for line wrapping on widths
manually. I think that is a reasonable compromise if it gives us more
predictable results.

[0] 375c4db4c7/tree/emitter.py
[1] https://github.com/iterative/dvc/issues/9397#issuecomment-1534045898

Change-Id: I9a8f831009f4811b3633847094bca7b71169bedb
2025-09-03 15:05:39 -07:00

64 lines
1.9 KiB
Python
Executable File

# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import ruamel.yaml
def none_representer(dumper, data):
return dumper.represent_scalar('tag:yaml.org,2002:null', 'null')
class YAML(object):
def __init__(self):
"""Wrap construction of ruamel yaml object."""
self.yaml = ruamel.yaml.YAML()
self.yaml.allow_duplicate_keys = True
self.yaml.representer.add_representer(type(None), none_representer)
self.yaml.indent(mapping=2, sequence=4, offset=2)
# Ruamel does weird things with linewrapping like adding extra
# whitespace at the end of lines. Just avoid wrapping entirely.
self.yaml.width = sys.maxsize
def load(self, stream):
return self.yaml.load(stream)
def tr(self, x):
x = x.replace('\n-', '\n\n-')
newlines = []
for line in x.split('\n'):
if '#' in line:
newlines.append(line)
else:
newlines.append(line[2:])
return '\n'.join(newlines)
def dump(self, data, *args, **kwargs):
if isinstance(data, list):
kwargs['transform'] = self.tr
self.yaml.dump(data, *args, **kwargs)
_yaml = YAML()
def load(*args, **kwargs):
return _yaml.load(*args, **kwargs)
def dump(*args, **kwargs):
return _yaml.dump(*args, **kwargs)