From 04d29f1fd64f9dd6e9a9dfd639898bb7d9c2a7d1 Mon Sep 17 00:00:00 2001 From: dece Date: Tue, 8 Jun 2021 14:45:28 +0200 Subject: [PATCH] plugin: add custom command support --- bebop/browser/browser.py | 10 +++++++++ bebop/plugins.py | 47 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/bebop/browser/browser.py b/bebop/browser/browser.py index acfc059..3fc6bd1 100644 --- a/bebop/browser/browser.py +++ b/bebop/browser/browser.py @@ -358,6 +358,14 @@ class Browser: return command = words[0] + + # Check for plugin registered commands first. + for plugin in self.plugins: + if command in map(lambda c: c.name, plugin.commands): + plugin.use_command(self, command, command_text) + return + + # Then built-in commands without args. if num_words == 1: if command == "help": self.open_help() @@ -368,6 +376,8 @@ class Browser: elif command in ("i", "info"): self.show_page_info() return + + # And commands with one or more args. if command in ("o", "open"): self.open_url(words[1]) elif command == "forget-certificate": diff --git a/bebop/plugins.py b/bebop/plugins.py index ebda9e7..4bd7bc2 100644 --- a/bebop/plugins.py +++ b/bebop/plugins.py @@ -18,15 +18,58 @@ There is at least one plugin in this repository in the `plugins` directory. """ from abc import ABC, abstractmethod +from dataclasses import dataclass from typing import Optional from bebop.browser.browser import Browser -class SchemePlugin(ABC): - """Plugin for URL scheme management.""" +@dataclass +class PluginCommand: + """A descriptor for a plugin command. + + Attributes: + - name: the command name. + - description: a very short description of the command; should start lower + case and does not need a period at the end. + """ + name: str + description: str + + +class Plugin(ABC): + """Base class for plugins. + + Attributes: + - commands: list of PluginCommand provided by the plugin. + """ + + def __init__(self) -> None: + super().__init__() + self.commands = [] + + def use_command(self, browser: Browser, name: str, text: str): + """Use a command presented by this plugin. + + Plugins that do not use custom commands can leave this method + unimplemented. + + Attributes: + - name: the command used as it is in the commands list. + - text: the whole command text, including the command name. + """ + pass + + +class SchemePlugin(Plugin): + """Plugin for URL scheme management. + + If you want to create a plugin that can handle new schemes, create a plugin + inheriting this class. + """ def __init__(self, scheme: str) -> None: + super().__init__() self.scheme = scheme @abstractmethod