Skip to content

Commit

Permalink
Merge branch 'dev' into georgettica/feat/systemextensionsctl
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjonbrazil authored Nov 20, 2024
2 parents 203762e + 7887789 commit 23ef46c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
jc changelog

20241119 v1.25.4
20241120 v1.25.4
- Add `ipconfig` command parser (`ipconfig` for Windows)
- Enhance `ethtool` parser to support `link_partner_advertised_link_modes`
- Enhance `ifconfig` parser to support `utun` interfaces with assigned IPv4 addresses on macOS
Expand All @@ -14,7 +14,9 @@ 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
- Enhance cli to coerce any non-JSON-serializable objects to a string

20240609 v1.25.3
- Enhance `bluetoothctl` parser with added `battery_percentage` field
Expand Down
7 changes: 6 additions & 1 deletion jc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,16 @@ def json_out(self) -> str:
self.json_indent = 2
self.json_separators = None

# Convert any non-serializable object to a string
def string_serializer(data):
return str(data)

j_string = json.dumps(
self.data_out,
indent=self.json_indent,
separators=self.json_separators,
ensure_ascii=self.ascii_only
ensure_ascii=self.ascii_only,
default=string_serializer
)

if not self.mono and PYGMENTS_INSTALLED:
Expand Down
9 changes: 1 addition & 8 deletions jc/jc_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
from typing import Any, Dict, List, Tuple, Iterator, Optional, Union

CustomColorType = Dict[Any, str]
JSONDictType = Dict[str, Any]
StreamingOutputType = Iterator[Union[JSONDictType, Tuple[BaseException, str]]]

Expand Down Expand Up @@ -42,11 +43,3 @@
else:
ParserInfoType = Dict
TimeStampFormatType = Dict


try:
from pygments.token import (Name, Number, String, Keyword)
CustomColorType = Dict[Union[Name.Tag, Number, String, Keyword], str]

except Exception:
CustomColorType = Dict # type: ignore
16 changes: 9 additions & 7 deletions jc/parsers/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@

class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.7'
version = '1.8'
description = 'YAML file parser'
author = 'Kelly Brazil'
author_email = '[email protected]'
Expand All @@ -111,7 +111,6 @@ def _process(proc_data):
List of Dictionaries. Each dictionary represents a YAML document.
"""

# No further processing
return proc_data

Expand Down Expand Up @@ -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)
8 changes: 8 additions & 0 deletions tests/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def test_yaml_datetime(self):
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()

0 comments on commit 23ef46c

Please sign in to comment.