Documented new import system for extensions
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 1e6c5f0 · Browse files · 1 file·+42 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+42 added−38 removed
- other:+42 added−38 removed
Provenance
- Commit
0719ad5- Tree
d08440a- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
1e6c5f0
1 file changed+42 added−38 removed
modified:docs/extensiondev.rst+42 added-38 removed
14 unmodified lines15 16 Anatomy of an Extension17 -----------------------18 19-Extensions are all located in a package called ``flaskext.something``19+Extensions are all located in a package called ``flask_something``20 where "something" is the name of the library you want to bridge. So for21 example if you plan to add support for a library named `simplexml` to22-Flask, you would name your extension's package ``flaskext.simplexml``.22+Flask, you would name your extension's package ``flask_simplexml``.23 24 The name of the actual extension (the human readable name) however would25 be something like "Flask-SimpleXML". Make sure to include the name26 "Flask" somewhere in that name and that you check the capitalization.27 This is how users can then register dependencies to your extension in28 their `setup.py` files.29 30-The magic that makes it possible to have your library in a package called31-``flaskext.something`` is called a "namespace package". Check out the32-guide below how to create something like that.30+Flask sets up a redirect package called :data:`flask.ext` where users31+should import the extensions from. If you for instance have a package32+called ``flask_something`` users would import it as33+``flask.ext.something``. This is done to transition from the old34+namespace packages. See :ref:`ext-import-transition` for more details.35 36 But how do extensions look like themselves? An extension has to ensure37 that it works with multiple Flask application instances at once. This is38 a requirement because many people will use patterns like the16 unmodified lines55 56 So let's get started with creating such a Flask extension. The extension57 we want to create here will provide very basic support for SQLite3.58 57-There is a script on github called `Flask Extension Wizard`_ which helps58-you create the initial folder structure. But for this very basic example59-we want to create all by hand to get a better feeling for it.60-59 First we create the following folder structure::60 61 flask-sqlite3/64- flaskext/65- __init__.py66- sqlite3.py67- setup.py62+ flask_sqlite3.py63 LICENSE64+ README65 66 Here's the contents of the most important files:67 72-flaskext/__init__.py73-````````````````````74-75-The only purpose of this file is to mark the package as namespace package.76-This is required so that multiple modules from different PyPI packages can77-reside in the same Python package::78-79- __import__('pkg_resources').declare_namespace(__name__)80-81-If you want to know exactly what is happening there, checkout the82-distribute or setuptools docs which explain how this works.83-84-Just make sure to not put anything else in there!85-68 setup.py69 ````````70 71 The next file that is absolutely required is the `setup.py` file which is17 unmodified lines89 author='Your Name',90 author_email='your-email@example.com',91 description='Very short description',92 long_description=__doc__,111- packages=['flaskext'],112- namespace_packages=['flaskext'],93+ py_modules=['flask_sqlite3'],94+ # if you would be using a package instead use packages instead95+ # of py_modules:96+ # packages=['flask_sqlite3'],97 zip_safe=False,98+ include_package_data=True,99 platforms='any',100 install_requires=[101 'Flask'102 ],8 unmodified lines111 ]112 )113 114 That's a lot of code but you can really just copy/paste that from existing130-extensions and adapt. This is also what the wizard creates for you if you131-use it.115+extensions and adapt.116 133-flaskext/sqlite3.py134-```````````````````117+flask_sqlite3.py118+````````````````119 120 Now this is where your extension code goes. But how exactly should such121 an extension look like? What are the best practices? Continue reading122 for some insight.30 unmodified lines153 154 The Extension Code155 ------------------156 173-Here's the contents of the `flaskext/sqlite3.py` for copy/paste::157+Here's the contents of the `flask_sqlite3.py` for copy/paste::158 159 from __future__ import absolute_import160 import sqlite3161 44 unmodified lines206 So why did we decide on a class based approach here? Because using our207 extension looks something like this::208 209 from flask import Flask226- from flaskext.sqlite3 import SQLite3210+ from flask_sqlite3 import SQLite3211 212 app = Flask(__name__)213 app.config.from_pyfile('the-config.cfg')214 manager = SQLite3(app)111 unmodified lines326 Extension Registry`_ and marked appropriately. If you want your own327 extension to be approved you have to follow these guidelines:328 329 1. An approved Flask extension must provide exactly one package or module346- inside the `flaskext` namespace package.330+ named ``flask_extensionname``. They might also reside inside a331+ ``flaskext`` namespace packages though this is discouraged now.332 2. It must ship a testing suite that can either be invoked with ``make test``333 or ``python setup.py test``. For test suites invoked with ``make334 test`` the extension has to ensure that all dependencies for the test335 are installed automatically, in case of ``python setup.py test``24 unmodified lines360 10. An extension currently has to support Python 2.5, 2.6 as well as361 Python 2.7362 363 379-.. _Flask Extension Wizard:380- http://github.com/mitsuhiko/flask-extension-wizard364+.. _ext-import-transition:365+366+Extension Import Transition367+---------------------------368+369+For a while we recommended using namespace packages for Flask extensions.370+This turned out to be problematic in practice because many different371+competing namespace package systems exist and pip would automatically372+switch between different systems and this caused a lot of problems for373+users.374+375+Instead we now recommend naming packages ``flask_foo`` instead of the now376+deprecated ``flaskext.foo``. Flask 0.8 introduces a redirect import377+system that lets uses import from ``flask.ext.foo`` and it will try378+``flask_foo`` first and if that fails ``flaskext.foo``.379+380+Flask extensions should urge users to import from ``flask.ext.foo``381+instead of ``flask_foo`` or ``flaskext_foo`` so that extensions can382+transition to the new package name without affecting users.383+384+385 .. _OAuth extension: http://packages.python.org/Flask-OAuth/386 .. _mailinglist: http://flask.pocoo.org/mailinglist/387 .. _IRC channel: http://flask.pocoo.org/community/irc/