From 94de2d767ffb9c4bc13470b4bbc7219e96119565 Mon Sep 17 00:00:00 2001 From: Jeremy Stanley Date: Wed, 29 Nov 2017 16:50:16 +0000 Subject: [PATCH] Indent block sequences in emitted files Override the default Emitter and Dumper from the pyyaml library to introduce (or more specifically stop suppressing) indentation when writing block sequences. Also adjust the dump parameters to match the indentation and related style used for included sample data and configuration files. --- mudpy/data.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/mudpy/data.py b/mudpy/data.py index 5233da2..d9eb8eb 100644 --- a/mudpy/data.py +++ b/mudpy/data.py @@ -12,6 +12,26 @@ import mudpy import yaml +class _IBSEmitter(yaml.emitter.Emitter): + + """Override the default YAML Emitter to indent block sequences.""" + + def expect_block_sequence(self): + """Match the expectations of the ``yamllint`` style checker.""" + + # TODO(fungi) Get an option for this implemented upstream in + # the pyyaml library + self.increase_indent(flow=False, indentless=False) + self.state = self.expect_first_block_sequence_item + + +class _IBSDumper(yaml.SafeDumper, _IBSEmitter): + + """Use our _IBSEmitter instead of the default implementation.""" + + pass + + class Data: """A file containing universe elements and their facets.""" @@ -144,8 +164,9 @@ class Data: os.umask(old_umask) # write and close the file - yaml.safe_dump(self.data, allow_unicode=True, - default_flow_style=False, stream=file_descriptor) + yaml.dump(self.data, Dumper=_IBSDumper, allow_unicode=True, + default_flow_style=False, explicit_start=True, indent=4, + stream=file_descriptor) file_descriptor.close() # unset the modified flag -- 2.11.0