Explicitly activate compat module
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 9c82840 · Browse files · 2 files·+27 added−37 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+27 added−37 removed
- other:+27 added−37 removed
Provenance
- Commit
7997bae- Tree
ebe23f7- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
9c82840
2 files changed+27 added−37 removed
modified:docs/extensions.rst+5 added-3 removed
31 unmodified lines32 33 We recommend importing from ``flask.ext`` even with older versions of34 Flask however. If you have an application that needs to work with35 versions of Flask older than 0.8 you should activate the36-``flaskext_compat`` module which provides the ``flask.ext`` module. You37-can download it from github: `flaskext_compat.py`_36+``flaskext_compat`` module which provides the ``flask.ext`` module if37+you activate it. You can download it from github: `flaskext_compat.py`_38 39-You can use it like this::39+And here is how you can use it::40 41 import flaskext_compat42+ flaskext_compat.activate()43+44 from flask.ext import foo45 46 Once the ``flaskext_compat`` module is imported the :data:`flask.ext` will47 exist and you can start importing from there.3 unmodified linesmodified:scripts/flaskext_compat.py+22 added-34 removed
12 unmodified lines13 14 :copyright: (c) 2011 by Armin Ronacher.15 :license: BSD, see LICENSE for more details.16 """17+import sys18+import imp19 20 21+ext_module = imp.new_module('flask.ext')22+ext_module.__path__ = []23+ext_module.__package__ = ext_module.__name__24+25+26 class _ExtensionImporter(object):20- """This importer redirects imports from this submodule to other locations.21- This makes it possible to transition from the old flaskext.name to the22- newer flask_name without people having a hard time.27+ """This importer redirects imports from the flask.ext module to other28+ locations.29 """30 _module_choices = ['flask_%s', 'flaskext.%s']31+ prefix = ext_module.__name__ + '.'32+ prefix_cutoff = prefix.count('.')33 26- def __init__(self):27- from sys import meta_path28- self.prefix = __name__ + '.'29- self.prefix_cutoff = __name__.count('.') + 130-31- # since people might reload the flask.ext module (by accident or32- # intentionally) we have to make sure to not add more than one33- # import hook. We can't check class types here either since a new34- # class will be created on reload. As a result of that we check35- # the name of the class and remove stale instances.36- def _name(x):37- cls = type(x)38- return cls.__module__ + '.' + cls.__name__39- this = _name(self)40- meta_path[:] = [x for x in meta_path if _name(x) != this] + [self]41-34 def find_module(self, fullname, path=None):35 if fullname.startswith(self.prefix):36 return self37 38 def load_module(self, fullname):47- from sys import modules48- if fullname in modules:49- return modules[fullname]39+ if fullname in sys.modules:40+ return sys.modules[fullname]41 modname = fullname.split('.', self.prefix_cutoff)[self.prefix_cutoff]42 for path in self._module_choices:43 realname = path % modname44 try:45 __import__(realname)46 except ImportError:47 continue57- module = modules[fullname] = modules[realname]48+ module = sys.modules[fullname] = sys.modules[realname]49 if '.' not in modname:59- setattr(modules[__name__], modname, module)50+ setattr(ext_module, modname, module)51 return module52 raise ImportError(fullname)53 54 64-import sys65-import flask66-try:67- __import__('flask.ext')68-except ImportError:69- sys.modules['flask.ext'] = flask.ext = sys.modules[__name__]70- __name__ = __package__ = 'flask.ext'71- __path__ = []72- _ExtensionImporter()73-del _ExtensionImporter, sys, flask55+def activate():56+ """Activates the compatibility system."""57+ import flask58+ if hasattr(flask, 'ext'):59+ return60+ sys.modules['flask.ext'] = flask.ext = ext_module61+ sys.meta_path.append(_ExtensionImporter())