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
Provenance
Commit
e944167
Tree
65a0e38
Author
Armin Ronacher <armin.ronacher@active-4.com>
Committer
Armin Ronacher <armin.ronacher@active-4.com>
Parent
0719ad5
1 file changed+73 added−0 removed
added:scripts/flaskext_compat.py+73 added
1+# -*- coding: utf-8 -*-2+"""3+ flaskext_compat4+ ~~~~~~~~~~~~~~~5+6+ 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.8+9+ Usage::10+11+ import flaskext_compat12+ from flask.ext import foo13+14+ :copyright: (c) 2011 by Armin Ronacher.15+ :license: BSD, see LICENSE for more details.16+"""17+18+19+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.23+ """24+ _module_choices = ['flask_%s', 'flaskext.%s']25+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+42+ def find_module(self, fullname, path=None):43+ if fullname.startswith(self.prefix):44+ return self45+46+ 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)62+63+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, flask