Skip to content

Updated syntax to Python 3 for improved compatibility on MacOS.#126

Open
andrewpking wants to merge 1 commit into
timsutton:mainfrom
andrewpking:main
Open

Updated syntax to Python 3 for improved compatibility on MacOS.#126
andrewpking wants to merge 1 commit into
timsutton:mainfrom
andrewpking:main

Conversation

@andrewpking

Copy link
Copy Markdown

Port brigadier to Python 3

Summary

This PR updates brigadier to be compatible with Python 3. The original script was written for Python 2, which has reached end-of-life and is no longer available via Homebrew or most modern package managers. These changes allow brigadier to run under Python 3 without requiring any legacy Python installation.

The conversion was initiated using the 2to3 migration tool, with additional manual fixes applied to address API removals and type incompatibilities introduced in Python 3.


Changes

Print statements → print() functions

Python 3 requires print to be called as a function. All bare print statements have been updated accordingly.

Before:

print "%s\n" % msg

After:

print("%s\n" % msg)

plistlib.readPlistFromString()plistlib.loads()

readPlistFromString was deprecated in Python 3.4 and removed in Python 3.9. Replaced with the modern plistlib.loads().

Before:

p = plistlib.readPlistFromString(data)

After:

p = plistlib.loads(data)

plistlib.readPlist()plistlib.load()

Similarly, readPlist was removed in Python 3.9. Replaced with plistlib.load(), which requires a binary file object.

Before:

config_plist = plistlib.readPlist(plist_path)

After:

with open(plist_path, 'rb') as f:
    config_plist = plistlib.load(f)

urllib2urllib.request

urllib2 does not exist in Python 3. All urllib2.urlopen() calls have been replaced with urllib.request.urlopen().

Before:

urlfd = urllib2.urlopen(sucatalog_url)

After:

urlfd = urllib.request.urlopen(sucatalog_url)

Bytes vs string type handling for HTTP responses

In Python 3, urllib.request.urlopen().read() returns bytes rather than a str. Passing a bytes object to re.search() with a string pattern raises a TypeError. All HTTP response bodies used with regex are now decoded to UTF-8 strings.

Before:

dist_data = distfd.read()
if re.search(model, dist_data):

After:

dist_data = distfd.read().decode('utf-8')
if re.search(model, dist_data):

Invalid regex escape sequences

2to3 surfaced SyntaxWarning for invalid escape sequences in regex strings that will become errors in future Python versions. These have been updated to use raw strings.

Before:

[HKEY_CURRENT_USER\Software\Apple Inc.\Apple Keyboard Support]
re_model = "([a-zA-Z]{4,12}[1-9]{1,2}\,[1-6])"

After:

[HKEY_CURRENT_USER\\Software\\Apple Inc.\\Apple Keyboard Support]
re_model = r"([a-zA-Z]{4,12}[1-9]{1,2}\,[1-6])"

Testing

Tested on:

  • macOS (Apple Silicon) with Python 3.14
  • Target model: MacBookAir7,2 (mid-2015 MacBook Air, 13-inch)
  • Successfully contacted Apple's SUCatalog, resolved the correct Boot Camp package, and downloaded drivers

Dependencies

The future package is no longer required. All changes use Python 3 stdlib only.


Notes

  • Python 2 compatibility is dropped by this PR. If Python 2 support needs to be maintained in parallel, a separate branch is recommended.
  • The 2to3 tool was used as a starting point but was not sufficient on its own — several runtime errors required manual fixes post-conversion, particularly around plistlib API removals and bytes/string type handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant