PyObjC MetricKit Framework
This package provides Python bindings for the MetricKit framework on macOS, allowing Python applications to access Apple's system for collecting power and performance metrics. It is part of the larger PyObjC project, which provides comprehensive Objective-C bridge functionality. The library is actively maintained, with new versions often aligning with macOS SDK updates and Python releases.
Warnings
- breaking PyObjC 12.0 dropped support for Python 3.9. Projects targeting older Python versions must use an older PyObjC release (e.g., PyObjC 11.x for Python 3.9).
- breaking PyObjC 11.0 dropped support for Python 3.8. Users on Python 3.8 need to use PyObjC 10.x or earlier.
- breaking PyObjC 11.1 introduced significant changes to how initializer methods are handled, aligning the core bridge with clang's Automatic Reference Counting (ARC) documentation. Methods in the 'init' family now correctly steal a reference to self and return a new reference, which can affect custom memory management or interactions with `alloc`.
- gotcha In PyObjC 10.3, calling `__init__` was initially restricted for classes implementing `__new__`, causing breaks in some projects. While partially re-introduced in 10.3.1, users defining custom `__new__` methods should be cautious, as the behavior around `__init__` might still differ from older PyObjC versions or standard Python class initialization.
- gotcha MetricKit is designed for deep integration into macOS applications or extensions to report metrics to Apple. While its APIs are exposed, successfully collecting and submitting metrics usually requires running within an appropriate application context with an `NSApplication` runloop, rather than a simple command-line Python script. Payloads are delivered asynchronously.
Install
-
pip install pyobjc-framework-metrickit
Imports
- MXMetricManager
from MetricKit import MXMetricManager
- MXDiagnosticPayload
from MetricKit import MXDiagnosticPayload
- MXMetricPayload
from MetricKit import MXMetricPayload
Quickstart
import objc
from MetricKit import MXMetricManager
from Foundation import NSLog # Used for Objective-C style logging
def get_metrickit_manager():
"""
Retrieves the shared MetricKit manager instance.
MetricKit is used for collecting power and performance metrics from an app.
Note: This is primarily designed for macOS apps/extensions to submit data to Apple,
not typically for standalone console scripts to actively collect and process.
"""
manager = MXMetricManager.sharedManager()
NSLog(f"MetricKit Shared Manager: {manager}")
# In a full application, you would typically add a subscriber (an NSObject subclass)
# manager.addSubscriber_(my_subscriber_instance)
return manager
if __name__ == "__main__":
print("Attempting to get MetricKit manager...")
manager = get_metrickit_manager()
if manager:
print(f"Successfully retrieved MetricKit manager: {manager}")
else:
print("Failed to retrieve MetricKit manager.")