[PATCH 2 of 6 hglib] client: refactor opening of the cmd server to an open method

Idan Kamara idankk86 at gmail.com
Thu Dec 22 17:18:41 UTC 2011


# HG changeset patch
# User Idan Kamara <idankk86 at gmail.com>
# Date 1324573967 -7200
# Node ID 0383fc37102b54870aac5b6586dec2767986f0f6
# Parent  b894c2222dff30183d26388812a257c93e8f8a5a
client: refactor opening of the cmd server to an open method

so we can create instances of hgclient without automatically opening a command
server (needed for clone/init).

diff --git a/hglib/client.py b/hglib/client.py
--- a/hglib/client.py
+++ b/hglib/client.py
@@ -37,21 +37,23 @@
     outputfmtsize = struct.calcsize(outputfmt)
     retfmt = '>i'
 
-    def __init__(self, path, encoding, configs):
-        args = [hglib.HGPATH, 'serve', '--cmdserver', 'pipe',
+    def __init__(self, path, encoding, configs, connect=True):
+        self._args = [hglib.HGPATH, 'serve', '--cmdserver', 'pipe',
                 '--config', 'ui.interactive=True']
         if path:
-            args += ['-R', path]
+            self._args += ['-R', path]
         if configs:
-            args += ['--config'] + configs
-        env = {}
+            self._args += ['--config'] + configs
+        self._env = {}
         if encoding:
-            env['HGENCODING'] = encoding
+            self._env['HGENCODING'] = encoding
 
-        self.server = util.popen(args, env)
-        self._readhello()
+        self.server = None
         self._version = None
 
+        if connect:
+            self.open()
+
     def __enter__(self):
         return self
 
@@ -164,6 +166,14 @@
                 return eh(ret, out, err)
         return out
 
+    def open(self):
+        if self.server is not None:
+            raise ValueError('server already open')
+
+        self.server = util.popen(self._args, self._env)
+        self._readhello()
+        return self
+
     def close(self):
         """
         Closes the command server instance and waits for it to exit, returns the



More information about the Mercurial-devel mailing list