Patchwork [4,of,5,RFC] commands: refactor 'serve', extract the http service class

login
register
mail settings
Submitter Mads Kiilerich
Date Oct. 10, 2013, 2:35 a.m.
Message ID <aaa67a78a1a9e1166710.1381372552@mk-desktop>
Download mbox | patch
Permalink /patch/2742/
State Accepted
Commit e48c70451afcba7f2a27fae1c7d827638f1e372b
Headers show

Comments

Mads Kiilerich - Oct. 10, 2013, 2:35 a.m.
# HG changeset patch
# User Mads Kiilerich <madski@unity3d.com>
# Date 1381372356 -7200
#      Thu Oct 10 04:32:36 2013 +0200
# Node ID aaa67a78a1a9e116671082f79342b4f360cd8d2e
# Parent  0143b6956677af12c3bd51f41ff4a26a31c2e367
commands: refactor 'serve', extract the http service class
Matt Mackall - Oct. 19, 2013, 11:04 p.m.
On Thu, 2013-10-10 at 04:35 +0200, Mads Kiilerich wrote:
> # HG changeset patch
> # User Mads Kiilerich <madski@unity3d.com>
> # Date 1381372356 -7200
> #      Thu Oct 10 04:32:36 2013 +0200
> # Node ID aaa67a78a1a9e116671082f79342b4f360cd8d2e
> # Parent  0143b6956677af12c3bd51f41ff4a26a31c2e367
> commands: refactor 'serve', extract the http service class

Queued 1, 2, and 4 for default, as they looks straightforward. Let's
talk about the other 2 post-2.8.

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -5182,47 +5182,51 @@ 
         o = repo
 
     app = hgweb.hgweb(o, baseui=baseui)
-
-    class service(object):
-        def init(self):
-            util.setsignalhandler()
-            self.httpd = hgweb_server.create_server(ui, app)
-
-            if opts['port'] and not ui.verbose:
-                return
-
-            if self.httpd.prefix:
-                prefix = self.httpd.prefix.strip('/') + '/'
-            else:
-                prefix = ''
-
-            port = ':%d' % self.httpd.port
-            if port == ':80':
-                port = ''
-
-            bindaddr = self.httpd.addr
-            if bindaddr == '0.0.0.0':
-                bindaddr = '*'
-            elif ':' in bindaddr: # IPv6
-                bindaddr = '[%s]' % bindaddr
-
-            fqaddr = self.httpd.fqaddr
-            if ':' in fqaddr:
-                fqaddr = '[%s]' % fqaddr
-            if opts['port']:
-                write = ui.status
-            else:
-                write = ui.write
-            write(_('listening at http://%s%s/%s (bound to %s:%d)\n') %
-                  (fqaddr, port, prefix, bindaddr, self.httpd.port))
-
-        def run(self):
-            self.httpd.serve_forever()
-
-    service = service()
-
+    service = httpservice(ui, app, opts)
     cmdutil.service(opts, initfn=service.init, runfn=service.run)
 
+class httpservice(object):
+    def __init__(self, ui, app, opts):
+        self.ui = ui
+        self.app = app
+        self.opts = opts
+
+    def init(self):
+        util.setsignalhandler()
+        self.httpd = hgweb_server.create_server(self.ui, self.app)
+
+        if self.opts['port'] and not self.ui.verbose:
+            return
+
+        if self.httpd.prefix:
+            prefix = self.httpd.prefix.strip('/') + '/'
+        else:
+            prefix = ''
+
+        port = ':%d' % self.httpd.port
+        if port == ':80':
+            port = ''
+
+        bindaddr = self.httpd.addr
+        if bindaddr == '0.0.0.0':
+            bindaddr = '*'
+        elif ':' in bindaddr: # IPv6
+            bindaddr = '[%s]' % bindaddr
+
+        fqaddr = self.httpd.fqaddr
+        if ':' in fqaddr:
+            fqaddr = '[%s]' % fqaddr
+        if self.opts['port']:
+            write = self.ui.status
+        else:
+            write = self.ui.write
+        write(_('listening at http://%s%s/%s (bound to %s:%d)\n') %
+              (fqaddr, port, prefix, bindaddr, self.httpd.port))
+
+    def run(self):
+        self.httpd.serve_forever()
+
+
 @command('showconfig|debugconfig',
     [('u', 'untrusted', None, _('show untrusted configuration options'))],
     _('[-u] [NAME]...'))