Patchwork [6,of,7,path-configs] ui.paths: implement getpath in terms of __getitem__

login
register
mail settings
Submitter Gregory Szorc
Date Feb. 8, 2015, 1:13 a.m.
Message ID <327b209f4ef203fd5d9b.1423358022@vm-ubuntu-main.gateway.sonic.net>
Download mbox | patch
Permalink /patch/7767/
State Changes Requested
Headers show

Comments

Gregory Szorc - Feb. 8, 2015, 1:13 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1423346259 28800
#      Sat Feb 07 13:57:39 2015 -0800
# Node ID 327b209f4ef203fd5d9bedb2527b8bfdc9edad3f
# Parent  8070ddf4999636a5bb52ffa6bfaca69299d86006
ui.paths: implement getpath in terms of __getitem__

paths.__iter__ is now the canonical method for obtaining all known
paths. Implement paths.__getitem__ and change paths.getpath
to be implemented in terms of it.

Patch

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -1001,22 +1001,30 @@  class paths(object):
 
         for name, p in sorted(r.items()):
             yield p
 
+    def __getitem__(self, key):
+        for path in self:
+            if path.name == key:
+                return path
+        raise KeyError('path not known: %s' % key)
+
     def getpath(self, name, default=None):
         """Return a ``path`` for the specified name.
 
         Returns None if the specified path or the default path was not
         found.
         """
-        p = self.ui.config('paths', name)
-        if not p and default is not None:
-            p = self.ui.config('paths', default)
+        try:
+            return self[name]
+        except KeyError:
+            if default is not None:
+                try:
+                    return self[default]
+                except KeyError:
+                    pass
 
-        if p:
-            return path(name, url=p)
-        else:
-            return None
+        return None
 
 class path(object):
     """Represents an individual path and its configuration."""