This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
barkshark-web/barkshark_web/extension_api.py

129 lines
2.9 KiB
Python

from hashlib import sha256
from .functions import get_app
## this will eventually get replaced with a keypair (probably rsa) setup
def generate_ext_hash(ext):
if None in [ext.manifest.author, ext.manifest.shortname]:
return
h = sha256((ext.manifest.author + ext.manifest.shortname).encode('UTF-8'))
return h.hexdigest()
## This is the extension skeleton. Sub-class and re-implement 'handler_' functions you wish to use.
## Be sure to set the metadata (at least name and author) or else the extension won't load'.
class BarksharkWebExtension:
config_defaults = {
'log_level': {
'value': 'INFO',
'type': 'dropdown',
'all_values': ['CRITICAL', 'WARNING', 'INFO', 'VERBOSE', 'DEBUG']
}
}
def __init__(self, path):
self.path = path
self.manifest = self.load_manifest()
self.digest = generate_ext_hash(self)
self.form_fields = DotDict()
self.logging = logging.get_logger(f'WebExt:{self.manifest.name}')
self.config = DotDict()
## Temporary
self.enabled = True
def __getattr__(self, key):
try:
if object.__getattribute__(self, 'manifest'):
return self.manifest[key]
finally:
return object.__getattribute(self, key)
def __getitem__(self, key):
return self.config[key]
def __setitem__(self, key, value):
if key not in self.config_defaults:
raise KeyError(f'Invalid config option: {key}')
self.config[key] = value
def load_manifest(self):
## return a dict with the layout suggested below
## shortname and author are required. Do not change them later.
#name = 'Unnamed Extension'
#shortname = 'UnnamedExtension'
#description = 'It does the thing with the stuff'
#author = 'John Doe'
#website = 'example.com'
#repo = 'https://github.com/johndoe/example-extension'
#license = 'Non-Violent Public License v5'
#license_url = 'https://git.pixie.town/thufie/CNPL/raw/tag/CNPLv6/CNPL.txt'
data = DotDict().new_from_json_file(self.path.join('manifest.json'))
def load_config(self):
self.config.update({k: v['value'] for k,v in self.config_defaults.items()})
with get_app().db.session as s:
if not (ext := s.fetch('extensions', digest=self.digest).one()):
return
self.config.update(ext.config)
def save_config(self):
with get_app().db.session as s:
s.update('extensions', {'config': self.config}, digest=self.digest)
def get_config(self, key):
return self[key]
def set_config(self, key, value):
self[key] = value
self.save_config
def set_log_level(self, level='INFO'):
self.logging.setConfig({'level': level})
def handle_initialize(self):
pass
def handle_page_created(self, pageid: int):
pass
def handle_document_loaded(self, page_url: Url):
pass
def handle_send_request(self, page_url: Url, redirect: Url):
pass
def handle_console_message_sent(self, text: str, level: str, line: int):
pass
def handle_form_created(self, client, tab, page_url: str, forms: dict):
pass
def handle_form_sent(self, stage: str, fields: DotDict):
pass