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