Mercurial > hg
diff mercurial/namespaces.py @ 23717:d8663e6153c1
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
api.
author | Sean Farley <sean.michael.farley@gmail.com> |
---|---|
date | Fri, 19 Dec 2014 17:27:20 -0800 |
parents | f4828a8f6ae9 |
children | 42908c3275c6 |
line wrap: on
line diff
--- a/mercurial/namespaces.py Fri Dec 19 17:17:17 2014 -0800 +++ b/mercurial/namespaces.py Fri Dec 19 17:27:20 2014 -0800 @@ -39,24 +39,27 @@ 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 @@ -67,20 +70,17 @@ 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.name, namespace) else: - self._names[namespace] = val + self._names[namespace.name] = namespace # we only generate a template keyword if one does not already exist - if namespace not in templatekw.keywords: + if namespace.name not in templatekw.keywords: def generatekw(**args): - return templatekw.shownames(namespace, **args) + return templatekw.shownames(namespace.name, **args) - templatekw.keywords[namespace] = generatekw + templatekw.keywords[namespace.name] = generatekw def singlenode(self, repo, name): """ @@ -91,7 +91,7 @@ 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: @@ -103,12 +103,12 @@ 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