diff hglib/client.py @ 91:0383fc37102b

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).
author Idan Kamara <idankk86@gmail.com>
date Thu, 22 Dec 2011 19:12:47 +0200
parents b894c2222dff
children bd23bc72e662
line wrap: on
line diff
--- a/hglib/client.py	Thu Dec 22 19:12:39 2011 +0200
+++ b/hglib/client.py	Thu Dec 22 19:12:47 2011 +0200
@@ -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