Created
May 25, 2017 20:49
-
-
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 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
# 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