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