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