cmd_queue.util.util_yaml module¶
- cmd_queue.util.util_yaml._custom_ruaml_loader()[source]¶
References
https://stackoverflow.com/questions/59635900/ruamel-yaml-custom-commentedmapping-for-custom-tags https://stackoverflow.com/questions/528281/how-can-i-include-a-yaml-file-inside-another
- cmd_queue.util.util_yaml._custom_ruaml_dumper()[source]¶
References
https://stackoverflow.com/questions/59635900/ruamel-yaml-custom-commentedmapping-for-custom-tags
- class cmd_queue.util.util_yaml.Yaml[source]¶
Bases:
object
Namespace for yaml functions
- static dumps(data, backend='ruamel')[source]¶
Dump yaml to a string representation (and account for some of our use-cases)
- Parameters:
data (Any) – yaml representable data
backend (str) – either ruamel or pyyaml
- Returns:
yaml text
- Return type:
Example
>>> import ubelt as ub >>> data = { >>> 'a': 'hello world', >>> 'b': ub.udict({'a': 3}) >>> } >>> text1 = Yaml.dumps(data, backend='ruamel') >>> print(text1) >>> text2 = Yaml.dumps(data, backend='pyyaml') >>> print(text2) >>> assert text1 == text2
- static load(file, backend='ruamel')[source]¶
Load yaml from a file
- Parameters:
file (io.TextIOBase | PathLike | str) – yaml file path or file object
backend (str) – either ruamel or pyyaml
- Returns:
object
- static loads(text, backend='ruamel')[source]¶
Load yaml from a text
- Parameters:
text (str) – yaml text
backend (str) – either ruamel or pyyaml
- Returns:
object
Example
>>> import ubelt as ub >>> data = { >>> 'a': 'hello world', >>> 'b': ub.udict({'a': 3}) >>> } >>> print('data = {}'.format(ub.urepr(data, nl=1))) >>> print('---') >>> text = Yaml.dumps(data) >>> print(ub.highlight_code(text, 'yaml')) >>> print('---') >>> data2 = Yaml.loads(text) >>> assert data == data2 >>> data3 = Yaml.loads(text, backend='pyyaml') >>> print('data2 = {}'.format(ub.urepr(data2, nl=1))) >>> print('data3 = {}'.format(ub.urepr(data3, nl=1))) >>> assert data == data3
- static coerce(data, backend='ruamel')[source]¶
Attempt to convert input into a parsed yaml / json data structure. If the data looks like a path, it tries to load and parse file contents. If the data looks like a yaml/json string it tries to parse it. If the data looks like parsed data, then it returns it as-is.
- Parameters:
data (str | PathLike | dict | list)
backend (str) – either ruamel or pyyaml
- Returns:
parsed yaml data
- Return type:
Note
The input to the function cannot distinguish a string that should be loaded and a string that should be parsed. If it looks like a file that exists it will read it. To avoid this coerner case use this only for data where you expect the output is a List or Dict.
References
https://stackoverflow.com/questions/528281/how-can-i-include-a-yaml-file-inside-another
Example
>>> Yaml.coerce('"[1, 2, 3]"') [1, 2, 3] >>> fpath = ub.Path.appdir('cmd_queue/tests/util_yaml').ensuredir() / 'file.yaml' >>> fpath.write_text(Yaml.dumps([4, 5, 6])) >>> Yaml.coerce(fpath) [4, 5, 6] >>> Yaml.coerce(str(fpath)) [4, 5, 6] >>> dict(Yaml.coerce('{a: b, c: d}')) {'a': 'b', 'c': 'd'} >>> Yaml.coerce(None) None
Example
>>> assert Yaml.coerce('') is None
Example
>>> dpath = ub.Path.appdir('cmd_queue/tests/util_yaml').ensuredir() >>> fpath = dpath / 'external.yaml' >>> fpath.write_text(Yaml.dumps({'foo': 'bar'})) >>> text = ub.codeblock( >>> f''' >>> items: >>> - !include {dpath}/external.yaml >>> ''') >>> data = Yaml.coerce(text, backend='ruamel') >>> print(Yaml.dumps(data, backend='ruamel')) items: - foo: bar
>>> text = ub.codeblock( >>> f''' >>> items: >>> !include [{dpath}/external.yaml, blah, 1, 2, 3] >>> ''') >>> data = Yaml.coerce(text, backend='ruamel') >>> print('data = {}'.format(ub.urepr(data, nl=1))) >>> print(Yaml.dumps(data, backend='ruamel'))
- static Dict(data)[source]¶
Get a ruamel-enhanced dictionary
Example
>>> data = {'a': 'avalue', 'b': 'bvalue'} >>> data = Yaml.Dict(data) >>> data.yaml_set_start_comment('hello') >>> # Note: not working https://sourceforge.net/p/ruamel-yaml/tickets/400/ >>> data.yaml_set_comment_before_after_key('a', before='a comment', indent=2) >>> data.yaml_set_comment_before_after_key('b', 'b comment') >>> print(Yaml.dumps(data))