##// END OF EJS Templates
namespaces: add __getitem__ property...
Sean Farley -
r23736:d7324c24 default
parent child Browse files
Show More
@@ -1,134 +1,138 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):
46 """returns the namespace object"""
47 return self._names[namespace]
48
45 def addnamespace(self, namespace, order=None):
49 def addnamespace(self, namespace, order=None):
46 """register a namespace
50 """register a namespace
47
51
48 namespace: the name to be registered (in plural form)
52 namespace: the name to be registered (in plural form)
49 order: optional argument to specify the order of namespaces
53 order: optional argument to specify the order of namespaces
50 (e.g. 'branches' should be listed before 'bookmarks')
54 (e.g. 'branches' should be listed before 'bookmarks')
51
55
52 """
56 """
53 if order is not None:
57 if order is not None:
54 self._names.insert(order, namespace.name, namespace)
58 self._names.insert(order, namespace.name, namespace)
55 else:
59 else:
56 self._names[namespace.name] = namespace
60 self._names[namespace.name] = namespace
57
61
58 # 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
59 if namespace.name not in templatekw.keywords:
63 if namespace.name not in templatekw.keywords:
60 def generatekw(**args):
64 def generatekw(**args):
61 return templatekw.shownames(namespace.name, **args)
65 return templatekw.shownames(namespace.name, **args)
62
66
63 templatekw.keywords[namespace.name] = generatekw
67 templatekw.keywords[namespace.name] = generatekw
64
68
65 def singlenode(self, repo, name):
69 def singlenode(self, repo, name):
66 """
70 """
67 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
68 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
69 in the defined precedence order.
73 in the defined precedence order.
70
74
71 Raises a KeyError if there is no such node.
75 Raises a KeyError if there is no such node.
72 """
76 """
73 for ns, v in self._names.iteritems():
77 for ns, v in self._names.iteritems():
74 n = v.namemap(repo, name)
78 n = v.namemap(repo, name)
75 if n:
79 if n:
76 # return max revision number
80 # return max revision number
77 if len(n) > 1:
81 if len(n) > 1:
78 cl = repo.changelog
82 cl = repo.changelog
79 maxrev = max(cl.rev(node) for node in n)
83 maxrev = max(cl.rev(node) for node in n)
80 return cl.node(maxrev)
84 return cl.node(maxrev)
81 return n[0]
85 return n[0]
82 raise KeyError(_('no such name: %s') % name)
86 raise KeyError(_('no such name: %s') % name)
83
87
84 def templatename(self, namespace):
88 def templatename(self, namespace):
85 """method that returns the template name of a namespace"""
89 """method that returns the template name of a namespace"""
86 return self._names[namespace].templatename
90 return self._names[namespace].templatename
87
91
88 def names(self, repo, namespace, node):
92 def names(self, repo, namespace, node):
89 """method that returns a (sorted) list of names in a namespace that
93 """method that returns a (sorted) list of names in a namespace that
90 match a given node"""
94 match a given node"""
91 return sorted(self._names[namespace].nodemap(repo, node))
95 return sorted(self._names[namespace].nodemap(repo, node))
92
96
93 class namespace(object):
97 class namespace(object):
94 """provides an interface to a namespace
98 """provides an interface to a namespace
95
99
96 Namespaces are basically generic many-to-many mapping between some
100 Namespaces are basically generic many-to-many mapping between some
97 (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
98 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
99 internal bits of mercurial: log output, tab completion, etc.
103 internal bits of mercurial: log output, tab completion, etc.
100
104
101 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
102 nodes to names. Each mapping returns a list.
106 nodes to names. Each mapping returns a list.
103
107
104 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
105 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
106 and not raise an error.
110 and not raise an error.
107
111
108 This namespace object will define the properties we need:
112 This namespace object will define the properties we need:
109 'name': the namespace (plural form)
113 'name': the namespace (plural form)
110 'templatename': name to use for templating (usually the singular form
114 'templatename': name to use for templating (usually the singular form
111 of the plural namespace name)
115 of the plural namespace name)
112 'namemap': function that takes a name and returns a list of nodes
116 '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
117 'nodemap': function that takes a node and returns a list of names
114
118
115 """
119 """
116
120
117 def __init__(self, name, templatename, namemap, nodemap):
121 def __init__(self, name, templatename, namemap, nodemap):
118 """create a namespace
122 """create a namespace
119
123
120 name: the namespace to be registered (in plural form)
124 name: the namespace to be registered (in plural form)
121 templatename: the name to use for templating
125 templatename: the name to use for templating
122 namemap: function that inputs a node, output name(s)
126 namemap: function that inputs a node, output name(s)
123 nodemap: function that inputs a name, output node(s)
127 nodemap: function that inputs a name, output node(s)
124
128
125 """
129 """
126 self.name = name
130 self.name = name
127 self.templatename = templatename
131 self.templatename = templatename
128 self.namemap = namemap
132 self.namemap = namemap
129 self.nodemap = nodemap
133 self.nodemap = nodemap
130
134
131 def names(self, repo, node):
135 def names(self, repo, node):
132 """method that returns a (sorted) list of names in a namespace that
136 """method that returns a (sorted) list of names in a namespace that
133 match a given node"""
137 match a given node"""
134 return sorted(self.nodemap(repo, node))
138 return sorted(self.nodemap(repo, node))
General Comments 0
You need to be logged in to leave comments. Login now