rophako/rophako/plugin.py
Noah Petherbridge 8a2d6a7c04 Refactor modules into a plugin-based system
All modules are now plugins. The config.py calls load_plugin for each
plugin it needs (some plugins may load others automatically). Also each
plugin keeps its own template folder which gets added to the template
search path, so i.e. if the photo plugin is unloaded completely, the URL
endpoints won't work either (with the old system, since the HTML
templates still existed in the default root the endpoints would still
serve pages, just without any Python logic behind them).
2014-07-23 14:21:53 -07:00

29 lines
1.1 KiB
Python

#!/usr/bin/env python
"""Dynamic CMS plugin loader."""
import os
from importlib import import_module
from rophako.app import app, BLUEPRINT_PATHS
def load_plugin(name, as_blueprint=True, template_path=None):
"""Load a Rophako CMS plugin.
* `name` is a Python module name, i.e. `rophako.modules.blog`
* `as_blueprint` is True if the module exports a blueprint object called
`mod` that can be attached to the Flask app. Set this value to False if
you simply need to include a Python module that isn't a blueprint.
* `template_path` is a filesystem path where the blueprint's templates
can be found. If not provided, the path is automatically determined
based on the module name, which is suitable for the built-in plugins."""
module = import_module(name)
if as_blueprint:
mod = getattr(module, "mod")
app.register_blueprint(mod)
# Get the template path to add to the BLUEPRINT_PATHS.
if template_path is None:
module_path = name.replace(".", "/")
template_path = os.path.join(module_path, "templates")
BLUEPRINT_PATHS.append(template_path)