Skip to content
Myr

Experimental redirect importing for flask.ext to flask_ and flaskext.

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 bd6ce4d · Browse files · 1 file+54 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+54 added−0 removed

  • other:+54 added−0 removed

1 file+54 added−0 removed

added:flask/ext/__init__.py+54 added
1# -*- coding: utf-8 -*-2"""3 flask.ext4 ~~~~~~~~~56 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.910 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``.1314 We're switching from namespace packages because it was just too painful for15 everybody involved.1617 :copyright: (c) 2011 by Armin Ronacher.18 :license: BSD, see LICENSE for more details.19"""20import sys212223class _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.modules3132 def find_module(self, fullname, path=None):33 if fullname.rsplit('.', 1)[0] == __name__:34 return self3536 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)515253sys.meta_path.append(_ExtensionImporter())54del sys, _ExtensionImporter