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:
@ -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 `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 `traceroute` parser to support extreme IPv6 cases
|
||||||
- Fix `uptime` parser for data that contains `user` instead of `users`
|
- 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
|
- Enhance `jc.utils.convert_size_to_int()` to add `posix_mode` and `decimal_bias` parameters
|
||||||
|
|
||||||
20240609 v1.25.3
|
20240609 v1.25.3
|
||||||
|
@ -87,7 +87,7 @@ from jc.exceptions import LibraryNotInstalled
|
|||||||
|
|
||||||
class info():
|
class info():
|
||||||
"""Provides parser metadata (version, author, etc.)"""
|
"""Provides parser metadata (version, author, etc.)"""
|
||||||
version = '1.7'
|
version = '1.8'
|
||||||
description = 'YAML file parser'
|
description = 'YAML file parser'
|
||||||
author = 'Kelly Brazil'
|
author = 'Kelly Brazil'
|
||||||
author_email = 'kellyjonbrazil@gmail.com'
|
author_email = 'kellyjonbrazil@gmail.com'
|
||||||
@ -111,7 +111,6 @@ def _process(proc_data):
|
|||||||
|
|
||||||
List of Dictionaries. Each dictionary represents a YAML document.
|
List of Dictionaries. Each dictionary represents a YAML document.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# No further processing
|
# No further processing
|
||||||
return proc_data
|
return proc_data
|
||||||
|
|
||||||
@ -148,17 +147,20 @@ def parse(data, raw=False, quiet=False):
|
|||||||
# plugin code is incompatible with the pyoxidizer packager
|
# plugin code is incompatible with the pyoxidizer packager
|
||||||
YAML.official_plug_ins = lambda a: []
|
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
|
# modify the timestamp constructor to output datetime objects as
|
||||||
# strings since JSON does not support datetime objects
|
# 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:timestamp'] = \
|
||||||
yaml.constructor.yaml_constructors['tag:yaml.org,2002:str']
|
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):
|
for document in yaml.load_all(data):
|
||||||
raw_output.append(document)
|
raw_output.append(document)
|
||||||
|
|
||||||
if raw:
|
return raw_output if raw else _process(raw_output)
|
||||||
return raw_output
|
|
||||||
else:
|
|
||||||
return _process(raw_output)
|
|
||||||
|
@ -56,6 +56,14 @@ class MyTests(unittest.TestCase):
|
|||||||
expected = [{"deploymentTime":"2022-04-18T11:12:47"}]
|
expected = [{"deploymentTime":"2022-04-18T11:12:47"}]
|
||||||
self.assertEqual(jc.parsers.yaml.parse(data, quiet=True), expected)
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Reference in New Issue
Block a user