changeset 19919:e48c70451afc

commands: refactor 'serve', extract the http service class
author Mads Kiilerich <madski@unity3d.com>
date Thu, 10 Oct 2013 04:32:36 +0200
parents ae65192fd6b4
children 1a17bb891ce5
files mercurial/commands.py
diffstat 1 files changed, 43 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Thu Oct 10 04:28:44 2013 +0200
+++ b/mercurial/commands.py	Thu Oct 10 04:32:36 2013 +0200
@@ -5183,47 +5183,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]...'))