Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+54 added−0 removed
other:+54 added−0 removed
Provenance
Commit
8f85a3b
Tree
1d98bb8
Author
Armin Ronacher <armin.ronacher@active-4.com>
Committer
Armin Ronacher <armin.ronacher@active-4.com>
Parent
bd6ce4d
1 file changed+54 added−0 removed
added:flask/ext/__init__.py+54 added
1+# -*- coding: utf-8 -*-2+"""3+ flask.ext4+ ~~~~~~~~~5+6+ Redirect imports for extensions. This module basically makes it possible7+ for us to transition from flaskext.foo to flask_foo without having to8+ force all extensions to upgrade at the same time.9+10+ When a user does ``from flask.ext.foo import bar`` it will attempt to11+ imprt ``from flask_foo import bar`` first and when that fails it will12+ try to import ``from flaskext.foo import bar``.13+14+ We're switching from namespace packages because it was just too painful for15+ everybody involved.16+17+ :copyright: (c) 2011 by Armin Ronacher.18+ :license: BSD, see LICENSE for more details.19+"""20+import sys21+22+23+class _ExtensionImporter(object):24+ """This importer redirects imports from this submodule to other25+ locations. This makes it possible to transition from the old26+ flaskext.name to the newer flask_name without people having a27+ hard time.28+ """29+ _module_choices = ['flask_%s', 'flaskext.%s']30+ _modules = sys.modules31+32+ def find_module(self, fullname, path=None):33+ if fullname.rsplit('.', 1)[0] == __name__:34+ return self35+36+ def load_module(self, fullname):37+ if fullname in self._modules:38+ return self._modules[fullname]39+ packname, modname = fullname.rsplit('.', 1)40+ for path in self._module_choices:41+ realname = path % modname42+ try:43+ __import__(realname)44+ except ImportError:45+ continue46+ module = self._modules[fullname] = self._modules[realname]47+ setattr(self._modules[__name__], modname, module)48+ module.__loader__ = self49+ return module50+ raise ImportError(fullname)51+52+53+sys.meta_path.append(_ExtensionImporter())54+del sys, _ExtensionImporter