[PATCH hglib] Modified package level init and clone functions to returned an UnopenedClient

Jeff Laughlin jmlaughlin at integrated-informatics.com
Mon Nov 28 10:09:08 CST 2011


# HG changeset patch
# User Jeff Laughlin <jmlaughlin at integrated-informatics.com>
# Date 1322062528 18000
# Node ID 90a6c77789f844165a5215d6c0904286f89f26d5
# Parent  8d491607b3eeb83be001137fd9406f68f935b2dc
Modified package level init and clone functions to returned an UnopenedClient
object with a single open method. This permits the user to decide if they want
to open the repository following an init or clone by simply chaining the open
call, e.g. "hglib.init()" or "client = hglib.init().open()".

diff -r 8d491607b3ee -r 90a6c77789f8 hglib/__init__.py
--- a/hglib/__init__.py	Wed Nov 23 10:27:57 2011 -0500
+++ b/hglib/__init__.py	Wed Nov 23 10:35:28 2011 -0500
@@ -8,6 +8,20 @@
     similar to those passed to hg --config. '''
     return client.hgclient(path, encoding, configs)
 
+_open = open
+
+class UnopenedClient(object):
+    def __init__(self, path=None, encoding=None, configs=None):
+        self.path = path
+        self.encoding = encoding
+        self.configs = configs
+
+    def open(self, path=None, encoding=None, configs=None):
+        if path is None: path = self.path
+        if encoding is None: encoding = self.encoding
+        if configs is None: configs = self.configs
+        return _open(path=path, encoding=encoding, configs=configs)
+
 def init(dest=None, ssh=None, remotecmd=None, insecure=False,
          encoding=None, configs=None):
     args = util.cmdbuilder('init', dest, e=ssh, remotecmd=remotecmd,
@@ -19,7 +33,7 @@
     if proc.returncode:
         raise error.CommandError(args, proc.returncode, out, err)
 
-    return open(dest, encoding, configs)
+    return UnopenedClient(path=dest, encoding=encoding, configs=configs)
 
 def clone(source=None, dest=None, noupdate=False, updaterev=None, rev=None,
           branch=None, pull=False, uncompressed=False, ssh=None,
@@ -34,5 +48,5 @@
     if proc.returncode:
         raise error.CommandError(args, proc.returncode, out, err)
 
-    return open(dest, encoding, configs)
+    return UnopenedClient(path=dest, encoding=encoding, configs=configs)
 
diff -r 8d491607b3ee -r 90a6c77789f8 tests/test-init.py
--- a/tests/test-init.py	Wed Nov 23 10:27:57 2011 -0500
+++ b/tests/test-init.py	Wed Nov 23 10:35:28 2011 -0500
@@ -9,5 +9,12 @@
         self.client = None
         shutil.rmtree('.hg')
 
-        self.client = hglib.init()
-        self.assertTrue(self.client.root().endswith('test_init'))
+        try:
+            self.client = hglib.init().open()
+            self.assertTrue(self.client.root().endswith('test_init'))
+        finally:
+            try:
+                self.client.close()
+            except Exception:
+                pass
+


More information about the Mercurial-devel mailing list