modularize strport parsers Re: [Twisted-Python] clean up app.getApplication

Paul Boehm paul at soniq.net
Sat Sep 27 18:58:04 EDT 2003


On Sat, Sep 27, 2003 at 06:37:23PM -0400, Jp Calderone wrote:
>   Thangs for posting this here (though someone may tell you it belongs on
> the bug tracker at http://www.twistedmatrix.com/bugs/, which it probably
> does)

ugh.. using webinterfaces without a mouse is horror.. so i'll do it
again and submit another patch here:

Index: strports.py
===================================================================
RCS file: /cvs/Twisted/twisted/application/strports.py,v
retrieving revision 1.1
diff -u -r1.1 strports.py
--- strports.py 26 Sep 2003 22:44:48 -0000  1.1
+++ strports.py 27 Sep 2003 22:56:19 -0000
@@ -46,12 +46,21 @@
 """
 from __future__ import generators
 
+_types = {}
+
+def registerType(name, helper):
+    global _types
+    _types[name] = helper
+    print _types
+
 def _parseTCP(factory, port, interface="", backlog=5):
     return (int(port), factory), {'interface': interface,
                                   'backlog': int(backlog)}
+registerType("tcp", _parseTCP)
 
 def _parseUNIX(factory, address, mode='666', backlog=5):
     return (address, factory), {'mode': int(mode, 8), 'backlog': int(backlog)}
+registerType("unix", _parseUNIX)
 
 def _parseSSL(factory, port, privateKey="server.pem", certKey=None,
               sslmethod=None, interface='', backlog=5):
@@ -64,10 +73,7 @@
     cf = ssl.DefaultOpenSSLContextFactory(privateKey, certKey, **kw)
     return ((int(port), factory, cf),
             {'interface': interface, 'backlog': int(backlog)})
-
-_funcs = {"tcp": _parseTCP,
-          "unix": _parseUNIX,
-          "ssl": _parseSSL}
+registerType("ssl", _parseSSL)
 
 _OP, _STRING = range(2)
 def _tokenize(description):
@@ -149,7 +155,7 @@
     args, kw = _parse(description)
     if not args or (len(args)==1 and not kw):
         args[0:0] = [default or 'tcp']
-    return (args[0].upper(),)+_funcs[args[0]](factory, *args[1:], **kw)
+    return (args[0].upper(),)+_types[args[0]](factory, *args[1:], **kw)
 
 def service(description, factory, default=None):
     """Return the service corresponding to a description





More information about the Twisted-Python mailing list