Skip to content
Myr

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

2 files+27 added−37 removed

modified:docs/extensions.rst+5 added-3 removed
31 unmodified lines
3233We recommend importing from ``flask.ext`` even with older versions of34Flask however. If you have an application that needs to work with35versions of Flask older than 0.8 you should activate the36``flaskext_compat`` module which provides the ``flask.ext`` module. You37can download it from github: `flaskext_compat.py`_36``flaskext_compat`` module which provides the ``flask.ext`` module if37you activate it. You can download it from github: `flaskext_compat.py`_3839You can use it like this::39And here is how you can use it::4041 import flaskext_compat42 flaskext_compat.activate()4344 from flask.ext import foo4546Once the ``flaskext_compat`` module is imported the :data:`flask.ext` will47exist and you can start importing from there.
3 unmodified lines
modified:scripts/flaskext_compat.py+22 added-34 removed
12 unmodified lines
1314 :copyright: (c) 2011 by Armin Ronacher.15 :license: BSD, see LICENSE for more details.16"""17import sys18import imp192021ext_module = imp.new_module('flask.ext')22ext_module.__path__ = []23ext_module.__package__ = ext_module.__name__242526class _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('.')3326 def __init__(self):27 from sys import meta_path28 self.prefix = __name__ + '.'29 self.prefix_cutoff = __name__.count('.') + 13031 # 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]4134 def find_module(self, fullname, path=None):35 if fullname.startswith(self.prefix):36 return self3738 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)535464import sys65import flask66try:67 __import__('flask.ext')68except ImportError:69 sys.modules['flask.ext'] = flask.ext = sys.modules[__name__]70 __name__ = __package__ = 'flask.ext'71 __path__ = []72 _ExtensionImporter()73del _ExtensionImporter, sys, flask55def 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())