##// END OF EJS Templates
namespaces: generate template keyword when registering a namespace...
Sean Farley -
r23610:9266d1dd default
parent child Browse files
Show More
@@ -1,103 +1,111 b''
1 1 from i18n import _
2 2 from mercurial import util
3 import templatekw
3 4
4 5 def tolist(val):
5 6 """
6 7 a convenience method to return an empty list instead of None
7 8 """
8 9 if val is None:
9 10 return []
10 11 else:
11 12 return [val]
12 13
13 14 class namespaces(object):
14 15 """
15 16 provides an interface to register a generic many-to-many mapping between
16 17 some (namespaced) names and nodes. The goal here is to control the
17 18 pollution of jamming things into tags or bookmarks (in extension-land) and
18 19 to simplify internal bits of mercurial: log output, tab completion, etc.
19 20
20 21 More precisely, we define a list of names (the namespace), a mapping of
21 22 names to nodes, and a mapping from nodes to names. Each mapping
22 23 returns a list of nodes.
23 24
24 25 Furthermore, each name mapping will be passed a name to lookup which might
25 26 not be in its domain. In this case, each method should return an empty list
26 27 and not raise an error.
27 28
28 29 We'll have a dictionary '_names' where each key is a namespace and
29 30 its value is a dictionary of functions:
30 31 'templatename': name to use for templating (usually the singular form
31 32 of the plural namespace name)
32 33 'namemap': function that takes a name and returns a list of nodes
33 34 'nodemap': function that takes a node and returns a list of names
34 35 """
35 36
36 37 _names_version = 0
37 38
38 39 def __init__(self):
39 40 self._names = util.sortdict()
40 41
41 42 addns = self.addnamespace
42 43
43 44 # we need current mercurial named objects (bookmarks, tags, and
44 45 # branches) to be initialized somewhere, so that place is here
45 46 addns("bookmarks", "bookmark",
46 47 lambda repo, name: tolist(repo._bookmarks.get(name)),
47 48 lambda repo, name: repo.nodebookmarks(name))
48 49
49 50 addns("tags", "tag",
50 51 lambda repo, name: tolist(repo._tagscache.tags.get(name)),
51 52 lambda repo, name: repo.nodetags(name))
52 53
53 54 addns("branches", "branch",
54 55 lambda repo, name: tolist(repo.branchtip(name)),
55 56 lambda repo, node: [repo[node].branch()])
56 57
57 58 def addnamespace(self, namespace, templatename, namemap, nodemap,
58 59 order=None):
59 60 """
60 61 register a namespace
61 62
62 63 namespace: the name to be registered (in plural form)
63 64 templatename: the name to use for templating
64 65 namemap: function that inputs a node, output name(s)
65 66 nodemap: function that inputs a name, output node(s)
66 67 order: optional argument to specify the order of namespaces
67 68 (e.g. 'branches' should be listed before 'bookmarks')
68 69 """
69 70 val = {'templatename': templatename,
70 71 'namemap': namemap,
71 72 'nodemap': nodemap}
72 73 if order is not None:
73 74 self._names.insert(order, namespace, val)
74 75 else:
75 76 self._names[namespace] = val
76 77
78 # we only generate a template keyword if one does not already exist
79 if namespace not in templatekw.keywords:
80 def generatekw(**args):
81 return templatekw.shownames(namespace, **args)
82
83 templatekw.keywords[namespace] = generatekw
84
77 85 def singlenode(self, repo, name):
78 86 """
79 87 Return the 'best' node for the given name. Best means the first node
80 88 in the first nonempty list returned by a name-to-nodes mapping function
81 89 in the defined precedence order.
82 90
83 91 Raises a KeyError if there is no such node.
84 92 """
85 93 for ns, v in self._names.iteritems():
86 94 n = v['namemap'](repo, name)
87 95 if n:
88 96 # return max revision number
89 97 if len(n) > 1:
90 98 cl = repo.changelog
91 99 maxrev = max(cl.rev(node) for node in n)
92 100 return cl.node(maxrev)
93 101 return n[0]
94 102 raise KeyError(_('no such name: %s') % name)
95 103
96 104 def templatename(self, namespace):
97 105 """method that returns the template name of a namespace"""
98 106 return self._names[namespace]['templatename']
99 107
100 108 def names(self, repo, namespace, node):
101 109 """method that returns a (sorted) list of names in a namespace that
102 110 match a given node"""
103 111 return sorted(self._names[namespace]['nodemap'](repo, node))
General Comments 0
You need to be logged in to leave comments. Login now