Fix gvar for dbus if binary is not a string (#1984)

This commit is contained in:
Pascal Vizeli 2020-08-27 17:04:14 +02:00 committed by GitHub
parent 8d552ae15c
commit a2821a98ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 1 deletions

View File

@ -58,6 +58,17 @@ MONITOR: str = "gdbus monitor --system --dest {bus}"
DBUS_METHOD_GETALL: str = "org.freedesktop.DBus.Properties.GetAll"
def _convert_bytes(value: str) -> str:
"""Convert bytes to string or byte-array."""
data: bytes = bytes(int(char, 0) for char in value.split(", "))
try:
text = data.decode()
except UnicodeDecodeError:
return f"<[{', '.join(str(char) for char in data)}]>"
return f"<'{text}'>"
class DBus:
"""DBus handler."""
@ -113,7 +124,7 @@ class DBus:
"""Parse GVariant input to python."""
# Process first string
json_raw = RE_GVARIANT_BINARY.sub(
lambda x: f"<'{bytes([int(char, 0) for char in x.group(1).split(', ')]).decode()}'>",
lambda x: _convert_bytes(x.group(1)),
raw,
)
json_raw = RE_GVARIANT_STRING_ESC.sub(

View File

@ -357,3 +357,48 @@ def test_networkmanager_binary_data():
"proxy": {},
}
]
raw = "({'802-11-wireless': {'mac-address-blacklist': <@as []>, 'mac-address': <[byte 0xca, 0x0b, 0x61, 0x00, 0xd8, 0xbd]>, 'mode': <'infrastructure'>, 'security': <'802-11-wireless-security'>, 'seen-bssids': <['7C:2E:BD:98:1B:06']>, 'ssid': <[byte 0x4e, 0x45, 0x54, 0x54]>}, 'connection': {'id': <'NETT'>, 'interface-name': <'wlan0'>, 'permissions': <@as []>, 'timestamp': <uint64 1598526799>, 'type': <'802-11-wireless'>, 'uuid': <'13f9af79-a6e9-4e07-9353-165ad57bf1a8'>}, 'ipv6': {'address-data': <@aa{sv} []>, 'addresses': <@a(ayuay) []>, 'dns': <@aay []>, 'dns-search': <@as []>, 'method': <'auto'>, 'route-data': <@aa{sv} []>, 'routes': <@a(ayuayu) []>}, '802-11-wireless-security': {'auth-alg': <'open'>, 'key-mgmt': <'wpa-psk'>}, 'ipv4': {'address-data': <@aa{sv} []>, 'addresses': <@aau []>, 'dns': <@au []>, 'dns-search': <@as []>, 'method': <'auto'>, 'route-data': <@aa{sv} []>, 'routes': <@aau []>}, 'proxy': {}},)"
data = DBus.parse_gvariant(raw)
assert data == [
{
"802-11-wireless": {
"mac-address": [202, 11, 97, 0, 216, 189],
"mac-address-blacklist": [],
"mode": "infrastructure",
"security": "802-11-wireless-security",
"seen-bssids": ["7C:2E:BD:98:1B:06"],
"ssid": "NETT",
},
"802-11-wireless-security": {"auth-alg": "open", "key-mgmt": "wpa-psk"},
"connection": {
"id": "NETT",
"interface-name": "wlan0",
"permissions": [],
"timestamp": 1598526799,
"type": "802-11-wireless",
"uuid": "13f9af79-a6e9-4e07-9353-165ad57bf1a8",
},
"ipv4": {
"address-data": [],
"addresses": [],
"dns": [],
"dns-search": [],
"method": "auto",
"route-data": [],
"routes": [],
},
"ipv6": {
"address-data": [],
"addresses": [],
"dns": [],
"dns-search": [],
"method": "auto",
"route-data": [],
"routes": [],
},
"proxy": {},
}
]