Skip to content

Instantly share code, notes, and snippets.

@pudquick
Created May 25, 2017 20:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pudquick/0f575a0891cfef5f1b0024c89dcbaeea to your computer and use it in GitHub Desktop.
Save pudquick/0f575a0891cfef5f1b0024c89dcbaeea to your computer and use it in GitHub Desktop.
Load arbitrary non-symbol Framework constants from macOS pyobjc .bridgesupport files in python
# This code leverages the pre-existing XML parser in objc but takes just the constant values
# without any deeper parsing of classes, structs, etc.
# Useful for when objc.loadBundleVariables as used here https://gist.github.com/pudquick/ac8f22326f095ed2690e
# is not possible because the constant isn't defined as a framework symbol, only in a header
# (which the System .bridgesupport files helpfully included entries for)
import objc
def constant_loader(framework_bridgesupport_path, target_dict, values = None):
with open(framework_bridgesupport_path, 'r') as f:
xml = f.read()
prs = objc._bridgesupport._BridgeSupportParser(xml, 'no path necessary for constants')
if values is None:
target_dict.update(prs.values)
else:
for k in values:
if prs.values.has_key(k):
target_dict[k] = prs.values[k]
# Example usage:
# constant_loader('/System/Library/Frameworks/IOKit.framework/Resources/BridgeSupport/IOKit.bridgesupport', globals(), ['kIOMediaClass'])
# >>> kIOMediaClass
# 'IOMedia'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment