Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate factory modules #2687

Open
mhsmith opened this issue Jul 1, 2024 · 1 comment
Open

Eliminate factory modules #2687

mhsmith opened this issue Jul 1, 2024 · 1 comment
Labels
enhancement New features, or improvements to existing features.

Comments

@mhsmith
Copy link
Member

mhsmith commented Jul 1, 2024

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, and also forces all the sub-modules to be imported whether the app uses them or not (#2547).

For example, where we currently write:

self.factory.Camera

We could instead have:

import_backend("hardware.camera.Camera")

Which on macOS would translate into an import of toga_cocoa.hardware.camera, and a getattr of Camera.

For previous discussion, start at #2584 (review).

@mhsmith mhsmith added the enhancement New features, or improvements to existing features. label Jul 1, 2024
@mhsmith mhsmith mentioned this issue Jul 1, 2024
4 tasks
@freakboy3742
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New features, or improvements to existing features.
Projects
None yet
Development

No branches or pull requests

2 participants