[PATCH 6 of 7 path-configs] ui.paths: implement getpath in terms of __getitem__
Gregory Szorc
gregory.szorc at gmail.com
Sun Feb 8 01:13:42 UTC 2015
# HG changeset patch
# User Gregory Szorc <gregory.szorc at 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.
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."""
More information about the Mercurial-devel
mailing list