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