Skip to content

Commit

Permalink
💥 NEW: plugin support (close #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
aprilahijriyan committed Jun 18, 2021
1 parent 0ca63aa commit 4fd3a50
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 7 deletions.
13 changes: 11 additions & 2 deletions examples/asgi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,36 @@

from falcon.asgi.ws import WebSocket

from falca.responses import HTMLResponse
from falca.responses import HTMLResponse, JSONResponse

envvar = "FALCA_SETTINGS"
os.environ.setdefault(envvar, "settings")

from plugin import Logger
from private.router import private_router
from resources.article import Article
from resources.home import Home
from resources.media import Media

from falca.app import ASGI
from falca.depends import Plugin, Settings

app = ASGI(__name__)
app.settings.from_envvar(envvar)
app.plugins.install("plugin.Logger")


@app.get("/")
async def index():
async def index(logger: Logger = Plugin("logging")):
logger.info("are you ok ?")
return HTMLResponse("index.html", context={"body": "not bad!"})


@app.get("/settings")
async def settings(part_config: dict = Settings("part_config")):
return JSONResponse(part_config)


@app.websocket("/events")
async def events(ws: WebSocket):
await ws.accept()
Expand Down
29 changes: 29 additions & 0 deletions examples/asgi/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import logging
from typing import Union

from falca.app import ASGI, WSGI
from falca.plugins.base import BasePlugin


class Logger(BasePlugin):
name = "logging"

def __init__(self, app: Union[WSGI, ASGI]) -> None:
super().__init__(app)
logging.basicConfig(level=logging.INFO)
self._log = logging.getLogger(app.import_name)

def info(self, *args, **kwds):
self._log.info(*args, **kwds)

def error(self, *args, **kwds):
self._log.error(*args, **kwds)

def warning(self, *args, **kwds):
self._log.warning(*args, **kwds)

def exception(self, *args, **kwds):
self._log.exception(*args, **kwds)

def debug(self, *args, **kwds):
self._log.debug(*args, **kwds)
13 changes: 11 additions & 2 deletions examples/wsgi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,32 @@
envvar = "FALCA_SETTINGS"
os.environ.setdefault(envvar, "settings")

from plugin import Logger
from private.router import private_router
from resources.article import Article
from resources.home import Home
from resources.media import Media

from falca.app import WSGI
from falca.responses import HTMLResponse
from falca.depends import Plugin, Settings
from falca.responses import HTMLResponse, JSONResponse

app = WSGI(__name__)
app.settings.from_envvar(envvar)
app.plugins.install("plugin.Logger")


@app.get("/")
def index():
def index(logger: Logger = Plugin("logging")):
logger.info("are you ok ?")
return HTMLResponse("index.html", context={"body": "not bad!"})


@app.get("/settings")
def settings(part_config: dict = Settings("part_config")):
return JSONResponse(part_config)


app.add_route("/home", Home())
app.add_route("/article", Article())
app.add_route("/form", Article(), suffix="form")
Expand Down
29 changes: 29 additions & 0 deletions examples/wsgi/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import logging
from typing import Union

from falca.app import ASGI, WSGI
from falca.plugins.base import BasePlugin


class Logger(BasePlugin):
name = "logging"

def __init__(self, app: Union[WSGI, ASGI]) -> None:
super().__init__(app)
logging.basicConfig(level=logging.INFO)
self._log = logging.getLogger(app.import_name)

def info(self, *args, **kwds):
self._log.info(*args, **kwds)

def error(self, *args, **kwds):
self._log.error(*args, **kwds)

def warning(self, *args, **kwds):
self._log.warning(*args, **kwds)

def exception(self, *args, **kwds):
self._log.exception(*args, **kwds)

def debug(self, *args, **kwds):
self._log.debug(*args, **kwds)
2 changes: 1 addition & 1 deletion falca/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class FalcaError(Exception):
pass


class PluginNotFound(FalcaError):
class PluginError(FalcaError):
pass


Expand Down
5 changes: 5 additions & 0 deletions falca/plugins/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class BasePlugin:
name: str = None

def __init__(self, app) -> None:
self.app = app
15 changes: 13 additions & 2 deletions falca/plugins/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,33 @@

from falcon.app import App

from ..exceptions import PluginError
from ..helpers import import_attr
from .base import BasePlugin


class PluginManager:
def __init__(self, app: App) -> None:
self.app = app
self.storage: Dict[str, object] = {}
self.storage: Dict[str, BasePlugin] = {}

def has(self, name: str):
return name in self.storage

def get(self, name: str):
if not self.has(name):
raise PluginError(f"plugin {name!r} not found")
return self.storage.get(name)

def install(self, name: str, src: str):
def install(self, src: str, name: str = None):
plugin = import_attr(src)
if not issubclass(plugin, BasePlugin):
raise PluginError(f"invalid plugin object {plugin}")

name = name or plugin.name
if not name:
raise PluginError("plugin name is required")

self.storage[name] = plugin(self.app)

def uninstall(self, name: str):
Expand Down

0 comments on commit 4fd3a50

Please sign in to comment.