Skip to content

Commit

Permalink
Fix parsing of nested capability groups
Browse files Browse the repository at this point in the history
  • Loading branch information
newAM committed May 13, 2023
1 parent 89befa9 commit d2ef4e7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 63 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html),
as of version 2.1.1.

## [Unreleased] - 2023-05-13
### Fixed
- Fixed parsing of nested capability groups.

## [3.0.3] - 2023-04-22
### Fixed
- Fixed dangling file descriptors on linux platforms after calling `get_monitors()`.
Expand Down
27 changes: 15 additions & 12 deletions monitorcontrol/monitorcontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,9 @@ def _convert_to_dict(caps_str: str) -> dict:
Expected string "04 14(05 06) 16" is converted to::
{
0x04: [],
0x14: [0x05, 0x06],
0x16: [],
0x04: {},
0x14: {0x05: {}, 0x06: {}},
0x16: {},
}
"""

Expand All @@ -536,22 +536,25 @@ def _convert_to_dict(caps_str: str) -> dict:
return {}

result_dict = {}
group = None
prev_digit = None
group = []
prev_val = None
for chunk in caps_str.replace("(", " ( ").replace(")", " ) ").split(" "):
if chunk == "":
continue
elif chunk == "(":
group = prev_digit
group.append(prev_val)
elif chunk == ")":
group = None
group.pop(-1)
else:
val = int(chunk, 16)
if group is None:
result_dict[val] = []
if len(group) == 0:
result_dict[val] = {}
else:
result_dict[group].append(val)
prev_digit = val
d = result_dict
for g in group:
d = d[g]
d[val] = {}
prev_val = val

return result_dict

Expand Down Expand Up @@ -598,7 +601,7 @@ def _parse_capabilities(caps_str: str) -> dict:
input_source_cap = vcp.VCPCode("input_select").value
if input_source_cap in caps_dict["vcp"]:
caps_dict["inputs"] = []
input_val_list = caps_dict["vcp"][input_source_cap]
input_val_list = list(caps_dict["vcp"][input_source_cap].keys())
input_val_list.sort()

for val in input_val_list:
Expand Down
109 changes: 58 additions & 51 deletions tests/test_monitorcontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,57 +261,64 @@ def test_convert_to_dict():
"F7(00 01 02 03) F8(00 01) F9 EF FD(00 01) FE(00 01 02) FF"
)
expected = {
0x02: [],
0x04: [],
0x05: [],
0x08: [],
0x10: [],
0x12: [],
0x14: [0x05, 0x08, 0x0B],
0x16: [],
0x18: [],
0x1A: [],
0x52: [],
0x60: [0x11, 0x12, 0x0F, 0x10],
0xAC: [],
0xAE: [],
0xB2: [],
0xB6: [],
0xC0: [],
0xC6: [],
0xC8: [],
0xC9: [],
0xD6: [0x01, 0x04],
0xDF: [],
0x62: [],
0x8D: [],
0xF4: [],
0xF5: [0x00, 0x01, 0x02],
0xF6: [0x00, 0x01, 0x02],
0x4D: [],
0x4E: [],
0x4F: [],
0x15: [
0x01,
0x06,
0x09,
0x10,
0x11,
0x13,
0x14,
0x28,
0x29,
0x32,
0x44,
0x48,
],
0xF7: [0x00, 0x01, 0x02, 0x03],
0xF8: [0x00, 0x01],
0xF9: [],
0xEF: [],
0xFD: [0x00, 0x01],
0xFE: [0x00, 0x01, 0x02],
0xFF: [],
0x02: {},
0x04: {},
0x05: {},
0x08: {},
0x10: {},
0x12: {},
0x14: {0x05: {}, 0x08: {}, 0x0B: {}},
0x16: {},
0x18: {},
0x1A: {},
0x52: {},
0x60: {0x11: {}, 0x12: {}, 0x0F: {}, 0x10: {}},
0xAC: {},
0xAE: {},
0xB2: {},
0xB6: {},
0xC0: {},
0xC6: {},
0xC8: {},
0xC9: {},
0xD6: {0x01: {}, 0x04: {}},
0xDF: {},
0x62: {},
0x8D: {},
0xF4: {},
0xF5: {0x00: {}, 0x01: {}, 0x02: {}},
0xF6: {0x00: {}, 0x01: {}, 0x02: {}},
0x4D: {},
0x4E: {},
0x4F: {},
0x15: {
0x01: {},
0x06: {},
0x09: {},
0x10: {},
0x11: {},
0x13: {},
0x14: {},
0x28: {},
0x29: {},
0x32: {},
0x44: {},
0x48: {},
},
0xF7: {0x00: {}, 0x01: {}, 0x02: {}, 0x03: {}},
0xF8: {0x00: {}, 0x01: {}},
0xF9: {},
0xEF: {},
0xFD: {0x00: {}, 0x01: {}},
0xFE: {0x00: {}, 0x01: {}, 0x02: {}},
0xFF: {},
}

assert _convert_to_dict(caps_str) == expected


def test_convert_to_dict_nested():
# https://github.com/newAM/monitorcontrol/issues/249
caps_str = "DC(00(00 12 13 14))"
expected = {0xDC: {0: {0: {}, 0x12: {}, 0x13: {}, 0x14: {}}}}
assert _convert_to_dict(caps_str) == expected

0 comments on commit d2ef4e7

Please sign in to comment.