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