diff --git a/CHANGELOG b/CHANGELOG index 0d91ddc6..43fff3a4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -14,6 +14,7 @@ jc changelog - Fix `pkg-index-deb`, `apt-cache-show`, and `rpm-qi` parsers to correctly convert contiguous packages with the same name - Fix `traceroute` parser to support extreme IPv6 cases - Fix `uptime` parser for data that contains `user` instead of `users` +- Fix `yaml` parser to support values that start with an equal sign - Enhance `jc.utils.convert_size_to_int()` to add `posix_mode` and `decimal_bias` parameters 20240609 v1.25.3 diff --git a/jc/parsers/yaml.py b/jc/parsers/yaml.py index 8a13a5b6..eb536d58 100644 --- a/jc/parsers/yaml.py +++ b/jc/parsers/yaml.py @@ -87,7 +87,7 @@ from jc.exceptions import LibraryNotInstalled class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.7' + version = '1.8' description = 'YAML file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -111,7 +111,6 @@ def _process(proc_data): List of Dictionaries. Each dictionary represents a YAML document. """ - # No further processing return proc_data @@ -148,17 +147,20 @@ def parse(data, raw=False, quiet=False): # plugin code is incompatible with the pyoxidizer packager YAML.official_plug_ins = lambda a: [] - yaml = YAML(typ='safe') + # use the default `typ` to correctly load values that start with a literal "=" + yaml = YAML(typ=None) # modify the timestamp constructor to output datetime objects as # strings since JSON does not support datetime objects yaml.constructor.yaml_constructors['tag:yaml.org,2002:timestamp'] = \ yaml.constructor.yaml_constructors['tag:yaml.org,2002:str'] + # modify the value constructor to output values starting with a + # literal "=" as a string. + yaml.constructor.yaml_constructors['tag:yaml.org,2002:value'] = \ + yaml.constructor.yaml_constructors['tag:yaml.org,2002:str'] + for document in yaml.load_all(data): raw_output.append(document) - if raw: - return raw_output - else: - return _process(raw_output) + return raw_output if raw else _process(raw_output) diff --git a/tests/test_yaml.py b/tests/test_yaml.py index b61150d7..0579b47e 100644 --- a/tests/test_yaml.py +++ b/tests/test_yaml.py @@ -56,6 +56,14 @@ class MyTests(unittest.TestCase): expected = [{"deploymentTime":"2022-04-18T11:12:47"}] self.assertEqual(jc.parsers.yaml.parse(data, quiet=True), expected) + def test_yaml_equalsign(self): + """ + Test yaml file with a value that starts with a literal equal sign "=" (should convert to a string) + """ + data = 'key: =' + expected = [{"key":"="}] + self.assertEqual(jc.parsers.yaml.parse(data, quiet=True), expected) + if __name__ == '__main__': unittest.main()