diff --git a/pyproject.toml b/pyproject.toml index 9eeb26f..bce3fda 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,7 +17,7 @@ namespaces = true # ----------------------------------------- Project Metadata ------------------------------------- # [project] -version = "0.0.0.dev55" +version = "0.0.0.dev56" name = "PySerials" dependencies = [ "jsonschema >= 4.21.0, < 5", @@ -26,8 +26,8 @@ dependencies = [ "ruamel.yaml >= 0.18", # https://yaml.readthedocs.io/en/stable/ "ruamel.yaml.string >= 0.1.1, < 1", "tomlkit >= 0.11.8, < 0.12", # https://tomlkit.readthedocs.io/en/stable/, - "MDit == 0.0.0.dev52", - "ExceptionMan == 0.0.0.dev52", + "MDit == 0.0.0.dev53", + "ExceptionMan == 0.0.0.dev53", "ProtocolMan == 0.0.0.dev2", ] requires-python = ">=3.10" diff --git a/requirements.txt b/requirements.txt index 544d842..9e0d480 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,6 @@ jsonpath-ng >= 1.6.1, < 2 ruamel.yaml >= 0.18 ruamel.yaml.string >= 0.1.1, < 1 tomlkit >= 0.11.8, < 0.12 -MDit == 0.0.0.dev52 -ExceptionMan == 0.0.0.dev52 +MDit == 0.0.0.dev53 +ExceptionMan == 0.0.0.dev53 ProtocolMan == 0.0.0.dev2 \ No newline at end of file diff --git a/src/pyserials/write.py b/src/pyserials/write.py index 78b0f7c..bef4f4f 100644 --- a/src/pyserials/write.py +++ b/src/pyserials/write.py @@ -31,12 +31,18 @@ def to_yaml_string( indent_sequence: int = 4, indent_sequence_offset: int = 2, multiline_string_to_block: bool = True, + remove_top_level_indent: bool = True ) -> str: yaml = _yaml.YAML(typ=["rt", "string"]) yaml.indent(mapping=indent_mapping, sequence=indent_sequence, offset=indent_sequence_offset) if multiline_string_to_block: _yaml_scalar_string.walk_tree(data) yaml_syntax = yaml.dumps(data, add_final_eol=False).removesuffix("\n...") + if remove_top_level_indent: + yaml_lines = yaml_syntax.splitlines() + count_leading_spaces = len(yaml_lines[0]) - len(yaml_lines[0].lstrip(" ")) + if count_leading_spaces: + yaml_syntax = "\n".join(yaml_line.removeprefix(" " * count_leading_spaces) for yaml_line in yaml_lines) return f"{yaml_syntax}\n" if end_of_file_newline else yaml_syntax