Merge "Add some helpers to projectconfig_ruamellib"

This commit is contained in:
Zuul 2019-04-10 14:08:24 +00:00 committed by Gerrit Code Review
commit 489c09ce30
3 changed files with 16 additions and 13 deletions

View File

@ -17,11 +17,10 @@
import locale
import sys
import projectconfig_ruamellib
import projectconfig_ruamellib as yaml
def main():
yaml = projectconfig_ruamellib.YAML(strip=False)
locale.setlocale(locale.LC_COLLATE, 'C')
chandata = yaml.load(open('gerritbot/channels.yaml'))

View File

@ -15,12 +15,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import projectconfig_ruamellib
import projectconfig_ruamellib as yaml
def main():
yaml = projectconfig_ruamellib.YAML()
data = yaml.load(open('gerrit/projects.yaml'))
for project in data:

View File

@ -21,18 +21,12 @@ def none_representer(dumper, data):
class YAML(object):
def __init__(self, strip=True):
"""Wrap construction of ruamel yaml object.
:param bool strip:
Whether or not to strip additional leading spaces at the beginning
of the line. This is only needed when the root object is a list.
"""
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)
self.strip = strip
def load(self, stream):
return self.yaml.load(stream)
@ -48,6 +42,17 @@ class YAML(object):
return '\n'.join(newlines)
def dump(self, data, *args, **kwargs):
if self.strip:
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)