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

API 的插件框架 #413

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
11 changes: 11 additions & 0 deletions libs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import functools
import hashlib
import ipaddress
import json
import random
import re
import smtplib
Expand Down Expand Up @@ -786,6 +787,13 @@ def _aes_decrypt(word:str, key:str, mode='CBC', iv:str=None, input_format='base6
mode = switch_mode(mode)
return aes_decrypt(word.encode("utf-8"), key.encode("utf-8"), mode=mode, iv=iv.encode("utf-8"), input=input_format, padding=padding, padding_style=padding_style, no_packb=no_packb)


def json_parse(data):
return json.loads(data)

def json_stringify(data):
return json.dumps(data)

jinja_globals = {
# types
'quote_chinese': quote_chinese,
Expand Down Expand Up @@ -829,6 +837,9 @@ def _aes_decrypt(word:str, key:str, mode='CBC', iv:str=None, input_format='base6
# random stuff
'random': random_fliter,
'shuffle': randomize_list,
# json
'json_parse': json_parse,
'json_stringify': json_stringify,
# undefined
'mandatory': mandatory,
# debug
Expand Down
47 changes: 26 additions & 21 deletions web/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,33 @@
# http://binux.me
# Created on 2012-12-15 16:15:50

import importlib
import os
import sys
import pkgutil

sys.path.append(os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))))
from . import base

handlers = []
ui_modules = {}
ui_methods = {}
modules = []
for file in os.listdir(os.path.dirname(__file__)):
if not file.endswith(".py"):
continue
if file == "__init__.py":
continue
modules.append(file[:-3])

for module in modules:
module = __import__('%s.%s' % (__package__, module), fromlist = ["handlers"])
if hasattr(module, "handlers"):
handlers.extend(module.handlers)
if hasattr(module, "ui_modules"):
ui_modules.update(module.ui_modules)
if hasattr(module, "ui_methods"):
ui_methods.update(module.ui_methods)

def load_modules():
handlers: list[tuple[str, base.BaseHandler]] = []
ui_modules: dict[str, base.BaseUIModule] = {}
ui_methods: dict[str, base.BaseWebSocketHandler] = {}

path = os.path.join(os.path.dirname(__file__), "")
for finder, name, ispkg in pkgutil.iter_modules([path]):
module = importlib.import_module("." + name, __name__)
if hasattr(module, "handlers"):
handlers.extend(module.handlers)
if hasattr(module, "ui_modules"):
ui_modules.update(module.ui_modules)
if hasattr(module, "ui_methods"):
ui_methods.update(module.ui_methods)

return handlers, ui_modules, ui_methods


handlers: list[tuple[str, base.BaseHandler]]
ui_modules: dict[str, base.BaseUIModule]
ui_methods: dict[str, base.BaseWebSocketHandler]

handlers, ui_modules, ui_methods = load_modules()
14 changes: 11 additions & 3 deletions web/handlers/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@
# http://binux.me
# Created on 2014-08-08 21:06:02

from . import api
from .api import BodyArgument, MultiArgument
from .base import *


class AboutHandler(BaseHandler):
@tornado.web.addslash
async def get(self):
await self.render('about.html')
await self.render(
"about.html",
apis=api.apis,
ismulti=lambda x: isinstance(x, MultiArgument),
isbody=lambda x: isinstance(x, BodyArgument),
)
return


handlers = [
('/about/?', AboutHandler),
]
("/about/?", AboutHandler),
]
Loading