Created
September 16, 2018 12:29
-
-
Save maruno/258e5d914876850f10833a719c839775 to your computer and use it in GitHub Desktop.
Read USB through ioreg plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import plistlib | |
from typing import Iterable | |
def devices_from_bus(bus_entries: Iterable[dict]): | |
devices = [] | |
for bus_entry in bus_entries: | |
if 'IORegistryEntryChildren' in bus_entry: | |
devices.extend( | |
devices_from_bus(bus_entry['IORegistryEntryChildren'])) | |
else: | |
devices.append(bus_entry) | |
return devices | |
async def probe_ioreg(): | |
process = await asyncio.create_subprocess_exec( | |
'ioreg', '-p', 'IOUSB', '-a', '-l', | |
stdout=asyncio.subprocess.PIPE) | |
ioreg_stdout: bytes = (await process.communicate())[0] | |
return plistlib.loads(ioreg_stdout, fmt=plistlib.FMT_XML) | |
def main(): | |
loop = asyncio.get_event_loop() | |
ioreg = loop.run_until_complete(probe_ioreg()) | |
devices = devices_from_bus(ioreg['IORegistryEntryChildren']) | |
for device in devices: | |
if not device['Built-In']: | |
print(f"Got device: {device['IORegistryEntryName']}") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment