cmdserver: switch service objects by mode
authorYuya Nishihara <yuya@tcha.org>
Sat, 27 Sep 2014 15:04:46 +0900
changeset 22989 dc8803ce3dfe
parent 22988 32b77aba2772
child 22990 a0e81aa94125
cmdserver: switch service objects by mode server class will be changed to accept fin/fout pair instead of mode string so that it can interact with socket files.
mercurial/commands.py
mercurial/commandserver.py
--- a/mercurial/commands.py	Sat Sep 27 14:52:09 2014 +0900
+++ b/mercurial/commands.py	Sat Sep 27 15:04:46 2014 +0900
@@ -5536,7 +5536,7 @@
         s.serve_forever()
 
     if opts["cmdserver"]:
-        service = commandserver.pipeservice(ui, repo, opts)
+        service = commandserver.createservice(ui, repo, opts)
         return cmdutil.service(opts, initfn=service.init, runfn=service.run)
 
     # this way we can check if something was given in the command-line
--- a/mercurial/commandserver.py	Sat Sep 27 14:52:09 2014 +0900
+++ b/mercurial/commandserver.py	Sat Sep 27 15:04:46 2014 +0900
@@ -129,7 +129,7 @@
     Listens for commands on stdin, runs them and writes the output on a channel
     based stream to stdout.
     """
-    def __init__(self, ui, repo, mode):
+    def __init__(self, ui, repo):
         self.cwd = os.getcwd()
 
         logpath = ui.config("cmdserver", "log", None)
@@ -151,15 +151,12 @@
             self.ui = ui
             self.repo = self.repoui = None
 
-        if mode == 'pipe':
-            self.cerr = channeledoutput(sys.stdout, 'e')
-            self.cout = channeledoutput(sys.stdout, 'o')
-            self.cin = channeledinput(sys.stdin, sys.stdout, 'I')
-            self.cresult = channeledoutput(sys.stdout, 'r')
+        self.cerr = channeledoutput(sys.stdout, 'e')
+        self.cout = channeledoutput(sys.stdout, 'o')
+        self.cin = channeledinput(sys.stdin, sys.stdout, 'I')
+        self.cresult = channeledoutput(sys.stdout, 'r')
 
-            self.client = sys.stdin
-        else:
-            raise util.Abort(_('unknown mode %s') % mode)
+        self.client = sys.stdin
 
     def _read(self, size):
         if not size:
@@ -251,10 +248,21 @@
 
 class pipeservice(object):
     def __init__(self, ui, repo, opts):
-        self.server = server(ui, repo, opts['cmdserver'])
+        self.server = server(ui, repo)
 
     def init(self):
         pass
 
     def run(self):
         return self.server.serve()
+
+_servicemap = {
+    'pipe': pipeservice,
+    }
+
+def createservice(ui, repo, opts):
+    mode = opts['cmdserver']
+    try:
+        return _servicemap[mode](ui, repo, opts)
+    except KeyError:
+        raise util.Abort(_('unknown mode %s') % mode)