|
14 | 14 | :copyright: (c) 2011 by Armin Ronacher.
|
15 | 15 | :license: BSD, see LICENSE for more details.
|
16 | 16 | """
|
| 17 | +import sys |
| 18 | +import imp |
| 19 | + |
| 20 | + |
| 21 | +ext_module = imp.new_module('flask.ext') |
| 22 | +ext_module.__path__ = [] |
| 23 | +ext_module.__package__ = ext_module.__name__ |
17 | 24 |
|
18 | 25 |
|
19 | 26 | 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 the |
22 |
| - newer flask_name without people having a hard time. |
| 27 | + """This importer redirects imports from the flask.ext module to other |
| 28 | + locations. |
23 | 29 | """
|
24 | 30 | _module_choices = ['flask_%s', 'flaskext.%s']
|
25 |
| - |
26 |
| - def __init__(self): |
27 |
| - from sys import meta_path |
28 |
| - self.prefix = __name__ + '.' |
29 |
| - self.prefix_cutoff = __name__.count('.') + 1 |
30 |
| - |
31 |
| - # since people might reload the flask.ext module (by accident or |
32 |
| - # intentionally) we have to make sure to not add more than one |
33 |
| - # import hook. We can't check class types here either since a new |
34 |
| - # class will be created on reload. As a result of that we check |
35 |
| - # 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] |
| 31 | + prefix = ext_module.__name__ + '.' |
| 32 | + prefix_cutoff = prefix.count('.') |
41 | 33 |
|
42 | 34 | def find_module(self, fullname, path=None):
|
43 | 35 | if fullname.startswith(self.prefix):
|
44 | 36 | return self
|
45 | 37 |
|
46 | 38 | def load_module(self, fullname):
|
47 |
| - from sys import modules |
48 |
| - if fullname in modules: |
49 |
| - return modules[fullname] |
| 39 | + if fullname in sys.modules: |
| 40 | + return sys.modules[fullname] |
50 | 41 | modname = fullname.split('.', self.prefix_cutoff)[self.prefix_cutoff]
|
51 | 42 | for path in self._module_choices:
|
52 | 43 | realname = path % modname
|
53 | 44 | try:
|
54 | 45 | __import__(realname)
|
55 | 46 | except ImportError:
|
56 | 47 | continue
|
57 |
| - module = modules[fullname] = modules[realname] |
| 48 | + module = sys.modules[fullname] = sys.modules[realname] |
58 | 49 | if '.' not in modname:
|
59 |
| - setattr(modules[__name__], modname, module) |
| 50 | + setattr(ext_module, modname, module) |
60 | 51 | return module
|
61 | 52 | raise ImportError(fullname)
|
62 | 53 |
|
63 | 54 |
|
64 |
| -import sys |
65 |
| -import flask |
66 |
| -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 |
| 55 | +def activate(): |
| 56 | + """Activates the compatibility system.""" |
| 57 | + import flask |
| 58 | + if hasattr(flask, 'ext'): |
| 59 | + return |
| 60 | + sys.modules['flask.ext'] = flask.ext = ext_module |
| 61 | + sys.meta_path.append(_ExtensionImporter()) |
0 commit comments