[PATCH 2 of 2] url: move [auth] parsing out into a utility function

Steve Borho steve at borho.org
Sat Feb 12 22:16:40 CST 2011


# HG changeset patch
# User Steve Borho <steve at borho.org>
# Date 1297569583 21600
# Node ID c691cfdc6b4d92e26c9e7ab5316fff0984114e65
# Parent  d13a533a0b11ee4380a7b0e8f7f461f5b165db78
url: move [auth] parsing out into a utility function

No functionality change, but it makes the [auth] section parsing and
best match detection usable by third party tools

diff -r d13a533a0b11 -r c691cfdc6b4d mercurial/url.py
--- a/mercurial/url.py	Sat Feb 12 21:53:27 2011 -0600
+++ b/mercurial/url.py	Sat Feb 12 21:59:43 2011 -0600
@@ -71,6 +71,38 @@
         return userpass + '@' + hostport
     return hostport
 
+def readauthforuri(ui, uri):
+    # Read configuration
+    config = dict()
+    for key, val in ui.configitems('auth'):
+        if '.' not in key:
+            ui.warn(_("ignoring invalid [auth] key '%s'\n") % key)
+            continue
+        group, setting = key.rsplit('.', 1)
+        gdict = config.setdefault(group, dict())
+        if setting in ('username', 'cert', 'key'):
+            val = util.expandpath(val)
+        gdict[setting] = val
+
+    # Find the best match
+    scheme, hostpath = uri.split('://', 1)
+    bestlen = 0
+    bestauth = None
+    for auth in config.itervalues():
+        prefix = auth.get('prefix')
+        if not prefix:
+            continue
+        p = prefix.split('://', 1)
+        if len(p) > 1:
+            schemes, prefix = [p[0]], p[1]
+        else:
+            schemes = (auth.get('schemes') or 'https').split()
+        if (prefix == '*' or hostpath.startswith(prefix)) and \
+            len(prefix) > bestlen and scheme in schemes:
+            bestlen = len(prefix)
+            bestauth = auth
+    return bestauth
+
 _safe = ('abcdefghijklmnopqrstuvwxyz'
          'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
          '0123456789' '_.-/')
@@ -149,36 +181,7 @@
         self.ui.debug(msg % (user, passwd and '*' * len(passwd) or 'not set'))
 
     def readauthtoken(self, uri):
-        # Read configuration
-        config = dict()
-        for key, val in self.ui.configitems('auth'):
-            if '.' not in key:
-                self.ui.warn(_("ignoring invalid [auth] key '%s'\n") % key)
-                continue
-            group, setting = key.rsplit('.', 1)
-            gdict = config.setdefault(group, dict())
-            if setting in ('username', 'cert', 'key'):
-                val = util.expandpath(val)
-            gdict[setting] = val
-
-        # Find the best match
-        scheme, hostpath = uri.split('://', 1)
-        bestlen = 0
-        bestauth = None
-        for auth in config.itervalues():
-            prefix = auth.get('prefix')
-            if not prefix:
-                continue
-            p = prefix.split('://', 1)
-            if len(p) > 1:
-                schemes, prefix = [p[0]], p[1]
-            else:
-                schemes = (auth.get('schemes') or 'https').split()
-            if (prefix == '*' or hostpath.startswith(prefix)) and \
-                len(prefix) > bestlen and scheme in schemes:
-                bestlen = len(prefix)
-                bestauth = auth
-        return bestauth
+        return readauthforuri(self.ui, uri)
 
 class proxyhandler(urllib2.ProxyHandler):
     def __init__(self, ui):


More information about the Mercurial-devel mailing list