Patchwork [3,of,4] namespaces: use namespace object instead of dictionary

mail settings
Submitter Sean Farley
Date Jan. 1, 2015, 1:17 a.m.
Message ID <5e57375149bd835b6368.1420067857@laptop.local>
Download mbox | patch
Permalink /patch/7294/
State Accepted
Commit d8663e6153c17c7a037fdf65b6c4d57f65939ac9
Headers show


Sean Farley - Jan. 1, 2015, 1:17 a.m.
# HG changeset patch
# User Sean Farley <>
# Date 1419038840 28800
#      Fri Dec 19 17:27:20 2014 -0800
# Node ID 5e57375149bd835b636859bcfbfecdea5433c7b8
# Parent  e0bdcc1fa7619e440128ef6b3ad4742d3f370f5d
namespaces: use namespace object instead of dictionary

This isn't as bad as the diff seems, it only looks like scary. In this patch,
we use the 'namespace' object instead of accessing keys in a dictionary.

This required the 'templatename' and 'names' method to change their
implementation. Later, we will remove these functions entirely due to a better


diff --git a/mercurial/ b/mercurial/
--- a/mercurial/
+++ b/mercurial/
@@ -37,63 +37,63 @@  class namespaces(object):
     _names_version = 0
     def __init__(self):
         self._names = util.sortdict()
-        addns = self.addnamespace
+        # shorten the class name for less indentation
+        ns = namespace
         # we need current mercurial named objects (bookmarks, tags, and
         # branches) to be initialized somewhere, so that place is here
-        addns("bookmarks", "bookmark",
+        n = ns("bookmarks", "bookmark",
               lambda repo, name: tolist(repo._bookmarks.get(name)),
               lambda repo, name: repo.nodebookmarks(name))
+        self.addnamespace(n)
-        addns("tags", "tag",
+        n = ns("tags", "tag",
               lambda repo, name: tolist(repo._tagscache.tags.get(name)),
               lambda repo, name: repo.nodetags(name))
+        self.addnamespace(n)
-        addns("branches", "branch",
+        n = ns("branches", "branch",
               lambda repo, name: tolist(repo.branchtip(name)),
               lambda repo, node: [repo[node].branch()])
+        self.addnamespace(n)
-    def addnamespace(self, namespace, templatename, namemap, nodemap,
-                     order=None):
+    def addnamespace(self, namespace, order=None):
         register a namespace
         namespace: the name to be registered (in plural form)
         templatename: the name to use for templating
         namemap: function that inputs a node, output name(s)
         nodemap: function that inputs a name, output node(s)
         order: optional argument to specify the order of namespaces
                (e.g. 'branches' should be listed before 'bookmarks')
-        val = {'templatename': templatename,
-               'namemap': namemap,
-               'nodemap': nodemap}
         if order is not None:
-            self._names.insert(order, namespace, val)
+            self._names.insert(order,, namespace)
-            self._names[namespace] = val
+            self._names[] = namespace
         # we only generate a template keyword if one does not already exist
-        if namespace not in templatekw.keywords:
+        if not in templatekw.keywords:
             def generatekw(**args):
-                return templatekw.shownames(namespace, **args)
+                return templatekw.shownames(, **args)
-            templatekw.keywords[namespace] = generatekw
+            templatekw.keywords[] = generatekw
     def singlenode(self, repo, name):
         Return the 'best' node for the given name. Best means the first node
         in the first nonempty list returned by a name-to-nodes mapping function
         in the defined precedence order.
         Raises a KeyError if there is no such node.
         for ns, v in self._names.iteritems():
-            n = v['namemap'](repo, name)
+            n = v.namemap(repo, name)
             if n:
                 # return max revision number
                 if len(n) > 1:
                     cl = repo.changelog
                     maxrev = max(cl.rev(node) for node in n)
@@ -101,16 +101,16 @@  class namespaces(object):
                 return n[0]
         raise KeyError(_('no such name: %s') % name)
     def templatename(self, namespace):
         """method that returns the template name of a namespace"""
-        return self._names[namespace]['templatename']
+        return self._names[namespace].templatename
     def names(self, repo, namespace, node):
         """method that returns a (sorted) list of names in a namespace that
         match a given node"""
-        return sorted(self._names[namespace]['nodemap'](repo, node))
+        return sorted(self._names[namespace].nodemap(repo, node))
 class namespace(object):
     """provides an interface to a namespace
     Namespaces are basically generic many-to-many mapping between some