Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/exthelper.py @ 41074:c81bb97b0cac
exthelper: add some examples for using registrar aliases
Maybe it's my general lack of python knowledge, but how to use these would be
way too obscure for me otherwise.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Fri, 28 Dec 2018 00:51:02 -0500 |
parents | 8f40e21ca842 |
children | 4d40f6bb4cef |
comparison
equal
deleted
inserted
replaced
41073:8f40e21ca842 | 41074:c81bb97b0cac |
---|---|
19 ) | 19 ) |
20 | 20 |
21 class exthelper(object): | 21 class exthelper(object): |
22 """Helper for modular extension setup | 22 """Helper for modular extension setup |
23 | 23 |
24 A single helper should be instantiated for each extension. Helper | 24 A single helper should be instantiated for each module of an |
25 methods are then used as decorators for various purpose. | 25 extension, where a command or function needs to be wrapped, or a |
26 command, extension hook, fileset, revset or template needs to be | |
27 registered. Helper methods are then used as decorators for | |
28 these various purposes. If an extension spans multiple modules, | |
29 all helper instances should be merged in the main module. | |
26 | 30 |
27 All decorators return the original function and may be chained. | 31 All decorators return the original function and may be chained. |
32 | |
33 Aside from the helper functions with examples below, several | |
34 registrar method aliases are available for adding commands, | |
35 configitems, filesets, revsets, and templates. Simply decorate | |
36 the appropriate methods, and assign the corresponding exthelper | |
37 variable to a module level variable of the extension. The | |
38 extension loading mechanism will handle the rest. | |
39 | |
40 example:: | |
41 | |
42 # ext.py | |
43 eh = exthelper.exthelper() | |
44 | |
45 # As needed: | |
46 cmdtable = eh.cmdtable | |
47 configtable = eh.configtable | |
48 filesetpredicate = eh.filesetpredicate | |
49 revsetpredicate = eh.revsetpredicate | |
50 templatekeyword = eh.templatekeyword | |
51 | |
52 @eh.command('mynewcommand', | |
53 [('r', 'rev', [], _('operate on these revisions'))], | |
54 _('-r REV...'), | |
55 helpcategory=command.CATEGORY_XXX) | |
56 def newcommand(ui, repo, *revs, **opts): | |
57 # implementation goes here | |
58 | |
59 eh.configitem('experimental', 'foo', | |
60 default=False, | |
61 ) | |
62 | |
63 @eh.filesetpredicate('lfs()') | |
64 def filesetbabar(mctx, x): | |
65 return mctx.predicate(...) | |
66 | |
67 @eh.revsetpredicate('hidden') | |
68 def revsetbabar(repo, subset, x): | |
69 args = revset.getargs(x, 0, 0, 'babar accept no argument') | |
70 return [r for r in subset if 'babar' in repo[r].description()] | |
71 | |
72 @eh.templatekeyword('babar') | |
73 def kwbabar(ctx): | |
74 return 'babar' | |
28 """ | 75 """ |
29 | 76 |
30 def __init__(self): | 77 def __init__(self): |
31 self._uipopulatecallables = [] | 78 self._uipopulatecallables = [] |
32 self._uicallables = [] | 79 self._uicallables = [] |