-
-
Notifications
You must be signed in to change notification settings - Fork 236
Refactor inter-plugin callback handling #1332
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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 file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,6 @@ | |
| from blueman.Service import Service | ||
| from blueman.bluez.errors import BluezDBusException | ||
| from blueman.main.NetworkManager import NMConnectionError | ||
| from blueman.main.PluginManager import StopException | ||
| from blueman.plugins.AppletPlugin import AppletPlugin | ||
| from blueman.bluez.Device import Device | ||
| from blueman.services.Functions import get_service | ||
|
|
@@ -17,20 +16,37 @@ | |
| from blueman.services.meta import SerialService, NetworkService | ||
|
|
||
|
|
||
| class RFCOMMConnectedListener: | ||
| def on_rfcomm_connected(self, service: Service, port: str) -> None: | ||
| ... | ||
|
|
||
| def on_rfcomm_disconnect(self, port: int) -> None: | ||
| ... | ||
|
|
||
|
|
||
| class RFCOMMConnectHandler: | ||
| def rfcomm_connect_handler(self, service: Service, reply: Callable[[str], None], | ||
| err: Callable[[Exception], None]) -> bool: | ||
| ... | ||
|
|
||
|
|
||
| class ServiceConnectHandler: | ||
| def service_connect_handler(self, service: Service, ok: Callable[[], None], | ||
| err: Callable[[Union[NMConnectionError, GLib.Error]], None]) -> bool: | ||
|
infirit marked this conversation as resolved.
|
||
| ... | ||
|
|
||
| def service_disconnect_handler(self, service: Service, ok: Callable[[], None], | ||
| err: Callable[[Union[NMConnectionError, GLib.Error]], None]) -> bool: | ||
| ... | ||
|
|
||
|
|
||
| class DBusService(AppletPlugin): | ||
| __depends__ = ["StatusIcon"] | ||
| __unloadable__ = False | ||
| __description__ = _("Provides DBus API for other Blueman components") | ||
| __author__ = "Walmis" | ||
|
|
||
| def on_load(self): | ||
|
|
||
| AppletPlugin.add_method(self.on_rfcomm_connected) | ||
| AppletPlugin.add_method(self.on_rfcomm_disconnect) | ||
| AppletPlugin.add_method(self.rfcomm_connect_handler) | ||
| AppletPlugin.add_method(self.service_connect_handler) | ||
| AppletPlugin.add_method(self.service_disconnect_handler) | ||
|
|
||
| self._add_dbus_method("QueryPlugins", (), "as", self.parent.Plugins.get_loaded) | ||
| self._add_dbus_method("QueryAvailablePlugins", (), "as", lambda: list(self.parent.Plugins.get_classes())) | ||
| self._add_dbus_method("SetPluginConfig", ("s", "b"), "", self.parent.Plugins.set_config) | ||
|
|
@@ -55,30 +71,26 @@ def connect_service(self, object_path: str, uuid: str, ok: Callable[[], None], | |
| device = Device(obj_path=object_path) | ||
| device.connect(reply_handler=ok, error_handler=err) | ||
| else: | ||
| def cb(_inst, ret): | ||
| if ret: | ||
| raise StopException | ||
|
|
||
| service = get_service(Device(obj_path=object_path), uuid) | ||
| assert service is not None | ||
|
|
||
| if isinstance(service, SerialService) and 'NMDUNSupport' in self.parent.Plugins.get_loaded(): | ||
| self.parent.Plugins.run_ex("service_connect_handler", cb, service, ok, err) | ||
| elif isinstance(service, SerialService) and 'PPPSupport' in self.parent.Plugins.get_loaded(): | ||
| if any(plugin.service_connect_handler(service, ok, err) | ||
| for plugin in self.parent.Plugins.get_loaded_plugins(ServiceConnectHandler)): | ||
| pass | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For alls these any() I find this very hard to read because the if continues on a new line. |
||
| elif isinstance(service, SerialService): | ||
| def reply(rfcomm): | ||
| self.parent.Plugins.run("on_rfcomm_connected", service, rfcomm) | ||
| for plugin in self.parent.Plugins.get_loaded_plugins(RFCOMMConnectedListener): | ||
| plugin.on_rfcomm_connected(service, rfcomm) | ||
| ok() | ||
|
|
||
| rets = self.parent.Plugins.run("rfcomm_connect_handler", service, reply, err) | ||
| if True in rets: | ||
| pass | ||
| else: | ||
| logging.info("No handler registered") | ||
| err("Service not supported\nPossibly the plugin that handles this service is not loaded") | ||
| if not any(plugin.rfcomm_connect_handler(service, reply, err) | ||
| for plugin in self.parent.Plugins.get_loaded_plugins(RFCOMMConnectHandler)): | ||
| service.connect(reply_handler=lambda port: ok(), error_handler=err) | ||
| elif isinstance(service, NetworkService): | ||
| service.connect(reply_handler=lambda interface: ok(), error_handler=err) | ||
| else: | ||
| if not self.parent.Plugins.run_ex("service_connect_handler", cb, service, ok, err) \ | ||
| and isinstance(service, (SerialService, NetworkService)): | ||
| service.connect(reply_handler=lambda *args: ok(), error_handler=err) | ||
| logging.info("No handler registered") | ||
| err("Service not supported\nPossibly the plugin that handles this service is not loaded") | ||
|
|
||
| def _disconnect_service(self, object_path: str, uuid: str, port: int, ok: Callable[[], None], | ||
| err: Callable[[Union[BluezDBusException, NMConnectionError, | ||
|
|
@@ -87,40 +99,21 @@ def _disconnect_service(self, object_path: str, uuid: str, port: int, ok: Callab | |
| device = Device(obj_path=object_path) | ||
| device.disconnect(reply_handler=ok, error_handler=err) | ||
| else: | ||
| def cb(_inst, ret): | ||
| if ret: | ||
| raise StopException | ||
|
|
||
| service = get_service(Device(obj_path=object_path), uuid) | ||
| assert service is not None | ||
|
|
||
| if isinstance(service, SerialService) and 'NMDUNSupport' in self.parent.Plugins.get_loaded(): | ||
| self.parent.Plugins.run_ex("service_disconnect_handler", cb, service, ok, err) | ||
| elif isinstance(service, SerialService) and 'PPPSupport' in self.parent.Plugins.get_loaded(): | ||
| if any(plugin.service_disconnect_handler(service, ok, err) | ||
| for plugin in self.parent.Plugins.get_loaded_plugins(ServiceConnectHandler)): | ||
| pass | ||
| elif isinstance(service, SerialService): | ||
| service.disconnect(port, reply_handler=ok, error_handler=err) | ||
|
|
||
| self.parent.Plugins.run("on_rfcomm_disconnect", port) | ||
| for plugin in self.parent.Plugins.get_loaded_plugins(RFCOMMConnectedListener): | ||
| plugin.on_rfcomm_disconnect(port) | ||
|
|
||
| logging.info("Disconnecting rfcomm device") | ||
| else: | ||
| if not self.parent.Plugins.run_ex("service_disconnect_handler", cb, service, ok, err) \ | ||
| and isinstance(service, NetworkService): | ||
| service.disconnect(reply_handler=ok, error_handler=err) | ||
|
|
||
| def service_connect_handler(self, service: Service, ok: Callable[..., None], err: Callable[..., None]) -> bool: | ||
| return False | ||
|
|
||
| def service_disconnect_handler(self, service: Service, ok: Callable[..., None], err: Callable[..., None]) -> bool: | ||
| return False | ||
| elif isinstance(service, NetworkService): | ||
| service.disconnect(reply_handler=ok, error_handler=err) | ||
|
|
||
| def _open_plugin_dialog(self): | ||
| self.parent.Plugins.StandardItems.on_plugins() | ||
|
|
||
| def rfcomm_connect_handler(self, service: Service, reply: Callable[..., None], err: Callable[..., None]) -> bool: | ||
| return False | ||
|
|
||
| def on_rfcomm_connected(self, service, port): | ||
| pass | ||
|
|
||
| def on_rfcomm_disconnect(self, port): | ||
| pass | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like the name because it does a bit more than just get loaded plugins, it get loaded plugins with a certain class. But I can't think of a better one...