Open mhsmith opened 4 days ago
As noted in this comment, it may be preferable to expose this API as import_backend("hardware").camera.Camera
or import_backend("hardware.camera").Camera
; that makes import_backend a relatively lightweight wrapper around import_module
, but injecting the platform name, and yielding a module.
A related thought: this mechanism could also be used to provide a plugin interface for third-party widgets to provide platform-specific implementations. Consider the case of a third party widget that has native platform implementations - the widget could register as having providing a macOS implementation, then the interface use a standard import_backend
API to access the platform backend. That avoids third-party widget needing to replicate the "if sys.platform ..." logic that would be the core of import_backend
, and should be able to do this as a fallback if an AttributeError
is raised on a first-pass lookup on the built-in backend.
The sub-module structure of each backend is identical, so that can already be considered a well-defined API between the interface and backend layers. Putting the "factory" namespace in between them adds complexity for no benefit.
For example, where we currently write:
We could instead have:
Which on macOS would translate into an import of
toga_cocoa.hardware.camera
, and a getattr ofCamera
.For previous discussion, start at https://github.com/beeware/toga/pull/2584#pullrequestreview-2092686462.