[PATCH] statichttprepo: add _statichttpopener class

Adrian Buehlmann adrian at cadifra.com
Thu Mar 31 13:09:37 CDT 2011


# HG changeset patch
# User Adrian Buehlmann <adrian at cadifra.com>
# Date 1301588569 -7200
# Node ID 9ea980086958d50a62a435a1c6c68a713bc25d8e
# Parent  800a10c2d82edc847405bbd404196ff013e32de6
statichttprepo: add _statichttpopener class

diff --git a/mercurial/statichttprepo.py b/mercurial/statichttprepo.py
--- a/mercurial/statichttprepo.py
+++ b/mercurial/statichttprepo.py
@@ -62,22 +62,18 @@
     def close(self):
         pass
 
-def build_opener(ui, authinfo):
-    # urllib cannot handle URLs with embedded user or passwd
-    urlopener = url.opener(ui, authinfo)
-    urlopener.add_handler(byterange.HTTPRangeHandler())
+class _statichttpopener(object):
+    def __init__(self, ui, authinfo, base):
+        # urllib cannot handle URLs with embedded user or passwd
+        self.urlopener = url.opener(ui, authinfo)
+        self.urlopener.add_handler(byterange.HTTPRangeHandler())
+        self.base = base
 
-    def opener(base):
-        """return a function that opens files over http"""
-        p = base
-        def o(path, mode="r", atomictemp=None):
-            if mode not in ('r', 'rb'):
-                raise IOError('Permission denied')
-            f = "/".join((p, urllib.quote(path)))
-            return httprangereader(f, urlopener)
-        return o
-
-    return opener
+    def __call__(self, path, mode='r', *args, **kw):
+        if mode not in ('r', 'rb'):
+            raise IOError('Permission denied')
+        f = self.base + '/' + urllib.quote(path)
+        return httprangereader(f, self.urlopener)
 
 class statichttprepository(localrepo.localrepository):
     def __init__(self, ui, path):
@@ -88,8 +84,10 @@
         u = url.url(path.rstrip('/') + "/.hg")
         self.path, authinfo = u.authinfo()
 
-        opener = build_opener(ui, authinfo)
-        self.opener = opener(self.path)
+        def createopener(base):
+            return _statichttpopener(self.ui, authinfo, base)
+
+        self.opener = createopener(self.path)
 
         # find requirements
         try:
@@ -117,7 +115,7 @@
                         _("requirement '%s' not supported") % r)
 
         # setup store
-        self.store = store.store(requirements, self.path, opener)
+        self.store = store.store(requirements, self.path, createopener)
         self.spath = self.store.path
         self.sopener = self.store.opener
         self.sjoin = self.store.join


More information about the Mercurial-devel mailing list