##// 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 The syntax for Python hooks is as follows:
475 The syntax for Python hooks is as follows:
476
476
477 hookname = python:modulename.submodule.callable
477 hookname = python:modulename.submodule.callable
478 hookname = python:/path/to/python/module.py:callable
478
479
479 Python hooks are run within the Mercurial process. Each hook is
480 Python hooks are run within the Mercurial process. Each hook is
480 called with at least three keyword arguments: a ui object (keyword
481 called with at least three keyword arguments: a ui object (keyword
@@ -28,6 +28,17 b' def find(name):'
28 return v
28 return v
29 raise KeyError(name)
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 def load(ui, name, path):
42 def load(ui, name, path):
32 if name.startswith('hgext.') or name.startswith('hgext/'):
43 if name.startswith('hgext.') or name.startswith('hgext/'):
33 shortname = name[6:]
44 shortname = name[6:]
@@ -40,14 +51,7 b' def load(ui, name, path):'
40 # the module will be loaded in sys.modules
51 # the module will be loaded in sys.modules
41 # choose an unique name so that it doesn't
52 # choose an unique name so that it doesn't
42 # conflicts with other modules
53 # conflicts with other modules
43 module_name = "hgext_%s" % name.replace('.', '_')
54 mod = loadpath(path, 'hgext.%s' % name)
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)
51 else:
55 else:
52 def importh(name):
56 def importh(name):
53 mod = __import__(name)
57 mod = __import__(name)
@@ -72,7 +76,6 b' def loadall(ui):'
72 if path:
76 if path:
73 if path[0] == '!':
77 if path[0] == '!':
74 continue
78 continue
75 path = os.path.expanduser(path)
76 try:
79 try:
77 load(ui, name, path)
80 load(ui, name, path)
78 except KeyboardInterrupt:
81 except KeyboardInterrupt:
@@ -7,6 +7,7 b''
7
7
8 from i18n import _
8 from i18n import _
9 import util, os, sys
9 import util, os, sys
10 from mercurial import extensions
10
11
11 def _pythonhook(ui, repo, name, hname, funcname, args, throw):
12 def _pythonhook(ui, repo, name, hname, funcname, args, throw):
12 '''call python hook. hook is callable object, looked up as
13 '''call python hook. hook is callable object, looked up as
@@ -109,8 +110,13 b' def hook(ui, repo, name, throw=False, **'
109 if callable(cmd):
110 if callable(cmd):
110 r = _pythonhook(ui, repo, name, hname, cmd, args, throw) or r
111 r = _pythonhook(ui, repo, name, hname, cmd, args, throw) or r
111 elif cmd.startswith('python:'):
112 elif cmd.startswith('python:'):
112 r = _pythonhook(ui, repo, name, hname, cmd[7:].strip(),
113 if cmd.count(':') == 2:
113 args, throw) or r
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 else:
120 else:
115 r = _exthook(ui, repo, hname, cmd, args, throw) or r
121 r = _exthook(ui, repo, hname, cmd, args, throw) or r
116 finally:
122 finally:
General Comments 0
You need to be logged in to leave comments. Login now