MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / get_yaml_safe_text

Function get_yaml_safe_text

src/licensedcode/models.py:778–794  ·  view source on GitHub ↗

Given a `text` string return a YAML-safe version of the string suitable for use in a YAML output, i.e. that would not produce a invalid yaml if used in the output. This is done by trying to dump and load the text inside a data dictionary and if it fails, padding all the empty n

(text)

Source from the content-addressed store, hash-verified

776
777
778def get_yaml_safe_text(text):
779 """
780 Given a `text` string return a YAML-safe version of the
781 string suitable for use in a YAML output, i.e. that would not
782 produce a invalid yaml if used in the output.
783
784 This is done by trying to dump and load the text inside a data
785 dictionary and if it fails, padding all the empty newlines with
786 a space.
787 """
788 data = {"text": text}
789 yaml_string = saneyaml_dump(data)
790 try:
791 saneyaml_load(yaml_string)
792 except Exception:
793 text = text.replace('\n\n', '\n \n')
794 return text
795
796
797def ignore_editor_tmp_files(location):

Callers 1

dumpMethod · 0.85

Calls 1

replaceMethod · 0.45

Tested by

no test coverage detected