From: Christopher Allan Webber Date: Thu, 16 May 2013 15:38:45 +0000 (-0500) Subject: Start of ability to have plugins provide static resources! X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=d6d2c771bdc111cd26186b1bc42b44f2b3197e05;p=mediagoblin.git Start of ability to have plugins provide static resources! Note I have not tested any of this yet ;) But we're already on our way: - We've got docs - The hook is there Lots to do still though. But, progress! :) This commit sponsored by Laura Arjona Reina. Thanks larjona! --- diff --git a/docs/source/pluginwriter/api.rst b/docs/source/pluginwriter/api.rst index 5e0568fd..cd06cbc5 100644 --- a/docs/source/pluginwriter/api.rst +++ b/docs/source/pluginwriter/api.rst @@ -125,3 +125,22 @@ context... it is added to the actual context of each individual template render right before it is run! Because of this you also can do some powerful and crazy things, such as checking the request object or other parts of the context before passing them on. + + +Adding static resources +----------------------- + +It's possible to add static resources for your plugin. Say your +plugin needs some special javascript and images... how to provide +them? Then how to access them? MediaGoblin has a way! + + +Attaching to the hook ++++++++++++++++++++++ + +First, you need to register your plugin's resources with the hook. +This is pretty easy actually: you just need to provide a function that +passes back a PluginStatic object. + +.. automodule:: mediagoblin.tools.staticdirect + :members: PluginStatic diff --git a/mediagoblin/init/__init__.py b/mediagoblin/init/__init__.py index d16027db..1eb9f1ba 100644 --- a/mediagoblin/init/__init__.py +++ b/mediagoblin/init/__init__.py @@ -24,6 +24,7 @@ from mediagoblin import mg_globals from mediagoblin.mg_globals import setup_globals from mediagoblin.db.open import setup_connection_and_db_from_config, \ check_db_migrations_current, load_models +from mediagoblin.pluginapi import hook_runall from mediagoblin.tools.workbench import WorkbenchManager from mediagoblin.storage import storage_system_from_config @@ -116,6 +117,12 @@ def get_staticdirector(app_config): direct_domains = {None: app_config['direct_remote_path'].strip()} direct_domains['theme'] = app_config['theme_web_path'].strip() + # Let plugins load additional paths + for plugin_static in hook_runall("static_setup"): + direct_domains[plugin_static['name']] = "%s/%s" % ( + app_config['plugin_web_path'].rstrip('/'), + plugin_static['name']) + return staticdirect.StaticDirect( direct_domains) diff --git a/mediagoblin/tools/staticdirect.py b/mediagoblin/tools/staticdirect.py index 31abc566..ef8b20d0 100644 --- a/mediagoblin/tools/staticdirect.py +++ b/mediagoblin/tools/staticdirect.py @@ -61,3 +61,41 @@ class StaticDirect(object): def get(self, filepath, domain=None): return '%s/%s' % ( self.domains[domain], filepath.lstrip('/')) + + +class PluginStatic(object): + """Pass this into the ``'static_setup'`` hook to register your + plugin's static directory. + + This has two mandatory attributes that you must pass in on class + init: + - name: this name will be both used for lookup in "urlgen" for + your plugin's static resources and for the subdirectory that + it'll be "mounted" to for serving via your web browser. It + *MUST* be unique. If writing a plugin bundled with MediaGoblin + please use the pattern 'coreplugin__foo' where 'foo' is your + plugin name. All external plugins should use their modulename, + so if your plugin is 'mg_bettertags' you should also call this + name 'mg_bettertags'. + - file_path: the directory your plugin's static resources are + located in. It's recommended that you use + pkg_resources.resource_filename() for this. + + An example of using this:: + + from pkg_resources import resource_filename + from mediagoblin.tools.staticdirect import PluginStatic + + hooks = { + 'static_setup': lambda: PluginStatic( + 'mg_bettertags', + resource_filename('mg_bettertags', 'static')) + } + + """ + def __init__(self, name, file_path): + self.name = name + self.file_path = file_path + + def __call__(self): + return self