|
|
@ -14,39 +14,30 @@ |
|
|
|
:copyright: (c) 2011 by Armin Ronacher. |
|
|
|
:copyright: (c) 2011 by Armin Ronacher. |
|
|
|
:license: BSD, see LICENSE for more details. |
|
|
|
:license: BSD, see LICENSE for more details. |
|
|
|
""" |
|
|
|
""" |
|
|
|
|
|
|
|
import sys |
|
|
|
|
|
|
|
import imp |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ext_module = imp.new_module('flask.ext') |
|
|
|
|
|
|
|
ext_module.__path__ = [] |
|
|
|
|
|
|
|
ext_module.__package__ = ext_module.__name__ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _ExtensionImporter(object): |
|
|
|
class _ExtensionImporter(object): |
|
|
|
"""This importer redirects imports from this submodule to other locations. |
|
|
|
"""This importer redirects imports from the flask.ext module to other |
|
|
|
This makes it possible to transition from the old flaskext.name to the |
|
|
|
locations. |
|
|
|
newer flask_name without people having a hard time. |
|
|
|
|
|
|
|
""" |
|
|
|
""" |
|
|
|
_module_choices = ['flask_%s', 'flaskext.%s'] |
|
|
|
_module_choices = ['flask_%s', 'flaskext.%s'] |
|
|
|
|
|
|
|
prefix = ext_module.__name__ + '.' |
|
|
|
def __init__(self): |
|
|
|
prefix_cutoff = prefix.count('.') |
|
|
|
from sys import meta_path |
|
|
|
|
|
|
|
self.prefix = __name__ + '.' |
|
|
|
|
|
|
|
self.prefix_cutoff = __name__.count('.') + 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# since people might reload the flask.ext module (by accident or |
|
|
|
|
|
|
|
# intentionally) we have to make sure to not add more than one |
|
|
|
|
|
|
|
# import hook. We can't check class types here either since a new |
|
|
|
|
|
|
|
# class will be created on reload. As a result of that we check |
|
|
|
|
|
|
|
# the name of the class and remove stale instances. |
|
|
|
|
|
|
|
def _name(x): |
|
|
|
|
|
|
|
cls = type(x) |
|
|
|
|
|
|
|
return cls.__module__ + '.' + cls.__name__ |
|
|
|
|
|
|
|
this = _name(self) |
|
|
|
|
|
|
|
meta_path[:] = [x for x in meta_path if _name(x) != this] + [self] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_module(self, fullname, path=None): |
|
|
|
def find_module(self, fullname, path=None): |
|
|
|
if fullname.startswith(self.prefix): |
|
|
|
if fullname.startswith(self.prefix): |
|
|
|
return self |
|
|
|
return self |
|
|
|
|
|
|
|
|
|
|
|
def load_module(self, fullname): |
|
|
|
def load_module(self, fullname): |
|
|
|
from sys import modules |
|
|
|
if fullname in sys.modules: |
|
|
|
if fullname in modules: |
|
|
|
return sys.modules[fullname] |
|
|
|
return modules[fullname] |
|
|
|
|
|
|
|
modname = fullname.split('.', self.prefix_cutoff)[self.prefix_cutoff] |
|
|
|
modname = fullname.split('.', self.prefix_cutoff)[self.prefix_cutoff] |
|
|
|
for path in self._module_choices: |
|
|
|
for path in self._module_choices: |
|
|
|
realname = path % modname |
|
|
|
realname = path % modname |
|
|
@ -54,20 +45,17 @@ class _ExtensionImporter(object): |
|
|
|
__import__(realname) |
|
|
|
__import__(realname) |
|
|
|
except ImportError: |
|
|
|
except ImportError: |
|
|
|
continue |
|
|
|
continue |
|
|
|
module = modules[fullname] = modules[realname] |
|
|
|
module = sys.modules[fullname] = sys.modules[realname] |
|
|
|
if '.' not in modname: |
|
|
|
if '.' not in modname: |
|
|
|
setattr(modules[__name__], modname, module) |
|
|
|
setattr(ext_module, modname, module) |
|
|
|
return module |
|
|
|
return module |
|
|
|
raise ImportError(fullname) |
|
|
|
raise ImportError(fullname) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import sys |
|
|
|
def activate(): |
|
|
|
import flask |
|
|
|
"""Activates the compatibility system.""" |
|
|
|
try: |
|
|
|
import flask |
|
|
|
__import__('flask.ext') |
|
|
|
if hasattr(flask, 'ext'): |
|
|
|
except ImportError: |
|
|
|
return |
|
|
|
sys.modules['flask.ext'] = flask.ext = sys.modules[__name__] |
|
|
|
sys.modules['flask.ext'] = flask.ext = ext_module |
|
|
|
__name__ = __package__ = 'flask.ext' |
|
|
|
sys.meta_path.append(_ExtensionImporter()) |
|
|
|
__path__ = [] |
|
|
|
|
|
|
|
_ExtensionImporter() |
|
|
|
|
|
|
|
del _ExtensionImporter, sys, flask |
|
|
|
|
|
|
|