Просмотр исходного кода

Started working on the Plugin API

I’m still experimenting..
sh4nks 11 лет назад
Родитель
Сommit
ce6d621c5c

+ 84 - 0
flaskbb/plugins/__init__.py

@@ -0,0 +1,84 @@
+# -*- coding: utf-8 -*-
+"""
+    flaskbb.plugins
+    ~~~~~~~~~~~~~~~
+
+    The Plugin class that every plugin should implement
+
+    :copyright: (c) 2014 by the FlaskBB Team.
+    :license: BSD, see LICENSE for more details.
+"""
+
+
+class PluginError(Exception):
+    """Error class for all plugin errors."""
+    pass
+
+
+class Plugin(object):
+    """Every plugin should implement this class. It handles the registration
+    for the plugin hooks, creates or modifies additional relations or
+    registers plugin specific thinks"""
+
+    #: If you want create your tables with one command, put your models
+    #: in here.
+    models = []
+
+    #: The name of the plugin
+    #: E.g. "Example Plugin"
+    name = None
+
+    #: A small description of the plugin
+    description = None
+
+    def install(self, app=None):
+        """The plugin should specify here what needs to be installed.
+        For example, create the database and register the hooks."""
+        raise NotImplementedError
+
+    def uninstall(self, app=None):
+        """Uninstalls the plugin and deletes the things that
+        the plugin has installed."""
+        raise NotImplementedError
+
+    def create_table(self, model, db):
+        """Creates the table for the model
+
+        :param model: The Model which should be created
+        :param db: The database instance.
+        """
+        model.__table__.create(bind=db.engine)
+
+    def drop_table(self, model, db):
+        """Drops the table for the bounded model.
+
+        :param model: The model on which the table is bound.
+        :param db: The database instance.
+        """
+        model.__table__.drop(bind=db.engine)
+
+    def create_all_tables(self, db):
+        """A interface for creating all models specified in ``models``.
+        If no models are specified in that variable it will abort
+        with a exception.
+
+        :param db: The database instance
+        """
+        if len(self.models) > 0:
+            for model in self.models:
+                self.create_table(model, db)
+        else:
+            raise PluginError("No models found in 'models'.")
+
+    def drop_all_tables(self, db):
+        """A interface for dropping all models specified in the
+        variable ``models``. If no models are specified in that variable,
+        it will abort with an exception.
+
+        :param db: The database instance.
+        """
+        if len(self.models) > 0:
+            for model in self.models:
+                self.drop_table(model, db)
+        else:
+            raise PluginError("No models found in 'models'.")

+ 60 - 0
flaskbb/plugins/loader.py

@@ -0,0 +1,60 @@
+# -*- coding: utf-8 -*-
+"""
+    flaskbb.plugins.loader
+    ~~~~~~~~~~~~~~~~~~~~~~
+
+    The Plugin loader.
+
+    :copyright: (c) 2014 by the FlaskBB Team.
+    :license: BSD, see LICENSE for more details.
+"""
+import os
+
+
+class PluginLoader(object):
+
+    #: Stores all founded plugins in the plugins folder
+    plugins = list()
+
+    def __init__(self, app):
+        self.app = app
+        self.plugin_folder = os.path.join(self.app.root_path, "plugins")
+        self.base_plugin_package = ".".join([self.app.name, "plugins"])
+
+    def load_plugins(self):
+        """Loads all plugins"""
+        #from flaskbb.plugins.pluginname import PluginName
+        #__import__(plugin, globals(), locals(), [tmp.__plugin__], -1)
+        pass
+
+    def check_plugin(self, plugin):
+        """Checks if a plugin is appropriate.
+
+        :param plugin:
+        """
+        pass
+
+    def find_plugins(self):
+        for item in os.listdir(self.plugin_folder):
+
+            if os.path.isdir(os.path.join(self.plugin_folder, item)) and \
+                    os.path.exists(
+                        os.path.join(self.plugin_folder, item, "__init__.py")):
+
+                plugin = ".".join([self.base_plugin_package, item])
+
+                # Same like from flaskbb.plugins.pluginname import __plugin__
+                tmp = __import__(
+                    plugin, globals(), locals(), ['__plugin__'], -1
+                )
+
+                self.plugins.append(tmp.__plugin__)
+
+"""
+for ipython:
+from flask import current_app
+from flaskbb.plugins.loader import PluginLoader
+loader = PluginLoader(current_app)
+loader.find_plugins()
+loader.plugins
+"""

+ 23 - 0
flaskbb/plugins/portal/__init__.py

@@ -0,0 +1,23 @@
+from flaskbb.extensions import db
+from flaskbb.plugins import Plugin
+
+from .portal import PortalModel
+
+
+#: The name of your plugin class
+__plugin__ = "PortalPlugin"
+
+
+class PortalPlugin(Plugin):
+    models = [PortalModel]
+
+    name = "Portal Plugin"
+    description = "A simple Portal"
+
+    def install(self):
+        self.create_all_tables(db)
+        #
+        # register hooks and blueprints/routes here
+
+    def uninstall(self):
+        self.drop_all_tables(db)

+ 17 - 0
flaskbb/plugins/portal/portal.py

@@ -0,0 +1,17 @@
+# -*- coding: utf-8 -*-
+"""
+    flaskbb.plugins.portal
+    ~~~~~~~~~~~~~~~~~~~~~~
+
+    This plugin implements a portal. You can choose which topics and posts
+    from forums are displayed.
+
+    :copyright: (c) 2014 by the FlaskBB Team.
+    :license: BSD, see LICENSE for more details.
+"""
+from flaskbb.extensions import db
+
+
+class PortalModel(db.Model):
+    id = db.Column(db.Integer, primary_key=True)
+    forum_id = db.Column(db.Integer, db.ForeignKey("forums.id"))