1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-08-06 22:32:54 +02:00

fix yaml parser to support values starting with an equal sign

This commit is contained in:
Kelly Brazil
2024-11-19 14:12:21 -08:00
parent 34ab34cc66
commit 7ddd2a4ce2
3 changed files with 18 additions and 7 deletions

View File

@ -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

View File

@ -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)

View File

@ -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()