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