Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Fix Python 3.6.0 #5

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions usbinfo/darwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import copy
import platform
import re
import subprocess

from .posix import get_mounts
Expand All @@ -30,6 +31,38 @@
OSX_VERSION_MICRO_INT = [int(part) for part in OSX_VERSION_STR.split('.')]


def _sanitize_xml(data):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth investigating doing all of this in an XML library. This function is assuming that ioreg will always return data in a specific format.

pattern = re.compile(r'^(\s*?\<string\>)(.*?)(\<\/string\>.*?)$')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be compiled once instead of re-compiling on every function call?

output = []

for i, line in enumerate(data.split('\n')):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to enumerate if you're not using the index.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use splitlines().

chunk = line.decode('utf-8')
match = re.match(pattern, chunk)
if match:
start = match.group(1)
middle = match.group(2)
end = match.group(3)
needs_patch = False
# Inspect the line and see if it has invalid characters.
byte_list = bytearray([ord(byte) for byte in middle])
for byte in byte_list:
if byte < 32:
needs_patch = True
if needs_patch:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this all be reduced to just

if any([ord(byte) not in range(32, 128) for byte in middle]):
    ...

middle = ''.join(['%02X' % byte for byte in byte_list])
new_line = '{start}{middle}{end}'.format(start=start,
middle=middle,
end=end)
output.append(new_line)
else:
output.append(line)
else:
output.append(line)
output = '\n'.join(output)

return output.encode('utf-8')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this redundant?



def _ioreg_usb_devices(nodename=None):
"""Returns a list of USB device tree from ioreg"""
import plistlib
Expand All @@ -38,6 +71,10 @@ def _ioreg(nodename):
"""Run ioreg command on specific node name"""
cmd = ['ioreg', '-a', '-l', '-r', '-n', nodename]
output = subprocess.check_output(cmd)
# ST-Link devices (and possibly others?) erroneously store binary data
# in the <string> serial number, which causes plistlib to blow up.
# This will convert that to hex and preserve contents otherwise.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this comment to the function definition.

output = _sanitize_xml(output)
plist_data = []
if output:
try:
Expand Down