Mercurial > public > mercurial-scm > hg
comparison mercurial/extensions.py @ 30058:8f54f9b8010d
extensions: move the "import" logic out from "load"
The "load" method does too many things: on-demand import and check version.
This patch moves the import logic out from "load" so it could be wrapped to
change the import behavior, for example, chg will use it to pre-import
extensions.
author | Jun Wu <quark@fb.com> |
---|---|
date | Mon, 03 Oct 2016 03:37:10 +0100 |
parents | 3741a8f86e88 |
children | 5581b294f3c6 |
comparison
equal
deleted
inserted
replaced
30057:a8ba9a23c893 | 30058:8f54f9b8010d |
---|---|
78 components = name.split('.') | 78 components = name.split('.') |
79 for comp in components[1:]: | 79 for comp in components[1:]: |
80 mod = getattr(mod, comp) | 80 mod = getattr(mod, comp) |
81 return mod | 81 return mod |
82 | 82 |
83 def _importext(name, path=None, reportfunc=None): | |
84 if path: | |
85 # the module will be loaded in sys.modules | |
86 # choose an unique name so that it doesn't | |
87 # conflicts with other modules | |
88 mod = loadpath(path, 'hgext.%s' % name) | |
89 else: | |
90 try: | |
91 mod = _importh("hgext.%s" % name) | |
92 except ImportError as err: | |
93 if reportfunc: | |
94 reportfunc(err, "hgext.%s" % name, "hgext3rd.%s" % name) | |
95 try: | |
96 mod = _importh("hgext3rd.%s" % name) | |
97 except ImportError as err: | |
98 if reportfunc: | |
99 reportfunc(err, "hgext3rd.%s" % name, name) | |
100 mod = _importh(name) | |
101 return mod | |
102 | |
83 def _reportimporterror(ui, err, failed, next): | 103 def _reportimporterror(ui, err, failed, next): |
84 # note: this ui.debug happens before --debug is processed, | 104 # note: this ui.debug happens before --debug is processed, |
85 # Use --config ui.debug=1 to see them. | 105 # Use --config ui.debug=1 to see them. |
86 ui.debug('could not import %s (%s): trying %s\n' | 106 ui.debug('could not import %s (%s): trying %s\n' |
87 % (failed, err, next)) | 107 % (failed, err, next)) |
96 if shortname in _builtin: | 116 if shortname in _builtin: |
97 return None | 117 return None |
98 if shortname in _extensions: | 118 if shortname in _extensions: |
99 return _extensions[shortname] | 119 return _extensions[shortname] |
100 _extensions[shortname] = None | 120 _extensions[shortname] = None |
101 if path: | 121 mod = _importext(name, path, bind(_reportimporterror, ui)) |
102 # the module will be loaded in sys.modules | |
103 # choose an unique name so that it doesn't | |
104 # conflicts with other modules | |
105 mod = loadpath(path, 'hgext.%s' % name) | |
106 else: | |
107 try: | |
108 mod = _importh("hgext.%s" % name) | |
109 except ImportError as err: | |
110 _reportimporterror(ui, err, "hgext.%s" % name, "hgext3rd.%s" % name) | |
111 try: | |
112 mod = _importh("hgext3rd.%s" % name) | |
113 except ImportError as err: | |
114 _reportimporterror(ui, err, "hgext3rd.%s" % name, name) | |
115 mod = _importh(name) | |
116 | 122 |
117 # Before we do anything with the extension, check against minimum stated | 123 # Before we do anything with the extension, check against minimum stated |
118 # compatibility. This gives extension authors a mechanism to have their | 124 # compatibility. This gives extension authors a mechanism to have their |
119 # extensions short circuit when loaded with a known incompatible version | 125 # extensions short circuit when loaded with a known incompatible version |
120 # of Mercurial. | 126 # of Mercurial. |