Skip to content
Myr

Added the flaskext_compat module to support flask.ext in 0.7 and earlier.

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 0719ad5 · Browse files · 1 file+73 added−0 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+73 added−0 removed

  • other:+73 added−0 removed

1 file+73 added−0 removed

added:scripts/flaskext_compat.py+73 added
1# -*- coding: utf-8 -*-2"""3 flaskext_compat4 ~~~~~~~~~~~~~~~56 Implements the ``flask.ext`` virtual package for versions of Flask7 older than 0.7. This module is a noop if Flask 0.8 was detected.89 Usage::1011 import flaskext_compat12 from flask.ext import foo1314 :copyright: (c) 2011 by Armin Ronacher.15 :license: BSD, see LICENSE for more details.16"""171819class _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.23 """24 _module_choices = ['flask_%s', 'flaskext.%s']2526 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]4142 def find_module(self, fullname, path=None):43 if fullname.startswith(self.prefix):44 return self4546 def load_module(self, fullname):47 from sys import modules48 if fullname in modules:49 return modules[fullname]50 modname = fullname.split('.', self.prefix_cutoff)[self.prefix_cutoff]51 for path in self._module_choices:52 realname = path % modname53 try:54 __import__(realname)55 except ImportError:56 continue57 module = modules[fullname] = modules[realname]58 if '.' not in modname:59 setattr(modules[__name__], modname, module)60 return module61 raise ImportError(fullname)626364import 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, flask