plugin: add custom command support

This commit is contained in:
dece 2021-06-08 14:45:28 +02:00
parent 3501316565
commit 04d29f1fd6
2 changed files with 55 additions and 2 deletions

View file

@ -358,6 +358,14 @@ class Browser:
return return
command = words[0] 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 num_words == 1:
if command == "help": if command == "help":
self.open_help() self.open_help()
@ -368,6 +376,8 @@ class Browser:
elif command in ("i", "info"): elif command in ("i", "info"):
self.show_page_info() self.show_page_info()
return return
# And commands with one or more args.
if command in ("o", "open"): if command in ("o", "open"):
self.open_url(words[1]) self.open_url(words[1])
elif command == "forget-certificate": elif command == "forget-certificate":

View file

@ -18,15 +18,58 @@ There is at least one plugin in this repository in the `plugins` directory.
""" """
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Optional from typing import Optional
from bebop.browser.browser import Browser from bebop.browser.browser import Browser
class SchemePlugin(ABC): @dataclass
"""Plugin for URL scheme management.""" 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: def __init__(self, scheme: str) -> None:
super().__init__()
self.scheme = scheme self.scheme = scheme
@abstractmethod @abstractmethod