##// END OF EJS Templates
ability to load hooks from arbitrary python module
Alexander Solovyov -
r7916:f779e199 default
parent child Browse files
Show More
@@ -475,6 +475,7 b' hooks::'
475 475 The syntax for Python hooks is as follows:
476 476
477 477 hookname = python:modulename.submodule.callable
478 hookname = python:/path/to/python/module.py:callable
478 479
479 480 Python hooks are run within the Mercurial process. Each hook is
480 481 called with at least three keyword arguments: a ui object (keyword
@@ -28,6 +28,17 b' def find(name):'
28 28 return v
29 29 raise KeyError(name)
30 30
31 def loadpath(path, module_name):
32 module_name = module_name.replace('.', '_')
33 path = os.path.expanduser(path)
34 if os.path.isdir(path):
35 # module/__init__.py style
36 d, f = os.path.split(path)
37 fd, fpath, desc = imp.find_module(f, [d])
38 return imp.load_module(module_name, fd, fpath, desc)
39 else:
40 return imp.load_source(module_name, path)
41
31 42 def load(ui, name, path):
32 43 if name.startswith('hgext.') or name.startswith('hgext/'):
33 44 shortname = name[6:]
@@ -40,14 +51,7 b' def load(ui, name, path):'
40 51 # the module will be loaded in sys.modules
41 52 # choose an unique name so that it doesn't
42 53 # conflicts with other modules
43 module_name = "hgext_%s" % name.replace('.', '_')
44 if os.path.isdir(path):
45 # module/__init__.py style
46 d, f = os.path.split(path)
47 fd, fpath, desc = imp.find_module(f, [d])
48 mod = imp.load_module(module_name, fd, fpath, desc)
49 else:
50 mod = imp.load_source(module_name, path)
54 mod = loadpath(path, 'hgext.%s' % name)
51 55 else:
52 56 def importh(name):
53 57 mod = __import__(name)
@@ -72,7 +76,6 b' def loadall(ui):'
72 76 if path:
73 77 if path[0] == '!':
74 78 continue
75 path = os.path.expanduser(path)
76 79 try:
77 80 load(ui, name, path)
78 81 except KeyboardInterrupt:
@@ -7,6 +7,7 b''
7 7
8 8 from i18n import _
9 9 import util, os, sys
10 from mercurial import extensions
10 11
11 12 def _pythonhook(ui, repo, name, hname, funcname, args, throw):
12 13 '''call python hook. hook is callable object, looked up as
@@ -109,8 +110,13 b' def hook(ui, repo, name, throw=False, **'
109 110 if callable(cmd):
110 111 r = _pythonhook(ui, repo, name, hname, cmd, args, throw) or r
111 112 elif cmd.startswith('python:'):
112 r = _pythonhook(ui, repo, name, hname, cmd[7:].strip(),
113 args, throw) or r
113 if cmd.count(':') == 2:
114 path, cmd = cmd[7:].split(':')
115 mod = extensions.loadpath(path, 'hgkook.%s' % hname)
116 hookfn = getattr(mod, cmd)
117 else:
118 hookfn = cmd[7:].strip()
119 r = _pythonhook(ui, repo, name, hname, hookfn, args, throw) or r
114 120 else:
115 121 r = _exthook(ui, repo, hname, cmd, args, throw) or r
116 122 finally:
General Comments 0
You need to be logged in to leave comments. Login now