[Pkg-privacy-commits] [pyptlib] 83/136: move envvar parsing out into a @classmethod, fromEnv()

Ximin Luo infinity0 at moszumanska.debian.org
Sat Aug 22 13:25:13 UTC 2015


This is an automated email from the git hooks/post-receive script.

infinity0 pushed a commit to branch master
in repository pyptlib.

commit a6199eb817d7546262641373fa59c57f54a703d4
Author: Ximin Luo <infinity0 at gmx.com>
Date:   Wed Aug 14 15:22:07 2013 +0100

    move envvar parsing out into a @classmethod, fromEnv()
---
 pyptlib/client.py        |  8 +++----
 pyptlib/client_config.py | 12 ++++++----
 pyptlib/config.py        | 29 +++++++++++++-----------
 pyptlib/server.py        |  8 +++----
 pyptlib/server_config.py | 59 ++++++++++++++++++++++++------------------------
 5 files changed, 62 insertions(+), 54 deletions(-)

diff --git a/pyptlib/client.py b/pyptlib/client.py
index f8dc6d5..1e57f34 100644
--- a/pyptlib/client.py
+++ b/pyptlib/client.py
@@ -28,7 +28,7 @@ def init(supported_transports):
 
     :raises: :class:`pyptlib.config.EnvError` if environment was incomplete or corrupted.
     """
-    config = ClientConfig()
+    config = ClientConfig.fromEnv()
 
     wanted = config.declareSupports(supported_transports)
     retval = {}
@@ -50,7 +50,7 @@ def reportSuccess(name, socksVersion, addrport, args=None, optArgs=None):
     :param str args: OPT-ARGS field for this transport.
     """
 
-    config = ClientConfig()
+    config = ClientConfig.fromEnv()
     config.writeMethod(name, socksVersion, addrport, args, optArgs)
 
 
@@ -64,7 +64,7 @@ def reportFailure(name, message):
     :param str message: Error message.
     """
 
-    config = ClientConfig()
+    config = ClientConfig.fromEnv()
     config.writeMethodError(name, message)
 
 
@@ -75,5 +75,5 @@ def reportEnd():
     *Call after you have launched all the transports you could launch.*
     """
 
-    config = ClientConfig()
+    config = ClientConfig.fromEnv()
     config.writeMethodEnd()
diff --git a/pyptlib/client_config.py b/pyptlib/client_config.py
index d5536cc..a84ccc6 100644
--- a/pyptlib/client_config.py
+++ b/pyptlib/client_config.py
@@ -15,10 +15,14 @@ class ClientConfig(Config):
 
     :raises: :class:`pyptlib.config.EnvError` if environment was incomplete or corrupted.
     """
-    def __init__(self, stdout=sys.stdout):
-        Config.__init__(self,
-            transports = self.getEnv('TOR_PT_CLIENT_TRANSPORTS').split(','),
-            stdout = stdout)
+    @classmethod
+    def fromEnv(cls, stdout=sys.stdout):
+        return cls(
+            stateLocation = cls.getEnv('TOR_PT_STATE_LOCATION'),
+            managedTransportVer = cls.getEnv('TOR_PT_MANAGED_TRANSPORT_VER').split(','),
+            transports = cls.getEnv('TOR_PT_CLIENT_TRANSPORTS').split(','),
+            stdout = stdout
+            )
 
     def writeMethod(self, name, socksVersion, addrport, args=None, optArgs=None):
         """
diff --git a/pyptlib/config.py b/pyptlib/config.py
index f62991d..17c9f49 100644
--- a/pyptlib/config.py
+++ b/pyptlib/config.py
@@ -36,9 +36,10 @@ class Config(object):
     :raises: :class:`pyptlib.config.EnvError` if environment was incomplete or corrupted.
     """
 
-    def __init__(self, transports, stdout=sys.stdout):
-        self.stateLocation = self.getEnv('TOR_PT_STATE_LOCATION')
-        self.managedTransportVer = self.getEnv('TOR_PT_MANAGED_TRANSPORT_VER').split(',')
+    def __init__(self, stateLocation, managedTransportVer, transports,
+                 stdout=sys.stdout):
+        self.stateLocation = stateLocation
+        self.managedTransportVer = managedTransportVer
         self.allTransportsEnabled = False
         if '*' in transports:
             self.allTransportsEnabled = True
@@ -107,7 +108,18 @@ class Config(object):
     def writeMethodError(self, transportName, message):
         raise NotImplementedError
 
-    def getEnv(self, key, validate=env_has_k):
+    def emit(self, msg):
+        """
+        Announce a message.
+
+        :param str msg: A message.
+        """
+
+        print >>self.stdout, msg
+        self.stdout.flush()
+
+    @classmethod
+    def getEnv(cls, key, validate=env_has_k):
         """
         Get the value of an environment variable.
 
@@ -131,15 +143,6 @@ class Config(object):
             sys.stdout.flush()
             raise EnvError(message)
 
-    def emit(self, msg):
-        """
-        Announce a message.
-
-        :param str msg: A message.
-        """
-
-        print >>self.stdout, msg
-        self.stdout.flush()
 
 class EnvError(Exception):
     """
diff --git a/pyptlib/server.py b/pyptlib/server.py
index 15e5df8..3972a10 100644
--- a/pyptlib/server.py
+++ b/pyptlib/server.py
@@ -31,7 +31,7 @@ def init(supported_transports):
 
     :raises: :class:`pyptlib.config.EnvError` if environment was incomplete or corrupted.
     """
-    config = ServerConfig()
+    config = ServerConfig.fromEnv()
     wanted = config.declareSupports(supported_transports)
     transports = dict(((k, v) for k, v in config.getServerBindAddresses().items()
                               if k in wanted['transports']))
@@ -55,7 +55,7 @@ def reportSuccess(name, addrport, options):
     :param str options: Transport options.
     """
 
-    config = ServerConfig()
+    config = ServerConfig.fromEnv()
     config.writeMethod(name, addrport, options)
 
 
@@ -69,7 +69,7 @@ def reportFailure(name, message):
     :param str message: Error message.
     """
 
-    config = ServerConfig()
+    config = ServerConfig.fromEnv()
     config.writeMethodError(name, message)
 
 
@@ -80,5 +80,5 @@ def reportEnd():
     *Call after you have launched all the transports you could launch.*
     """
 
-    config = ServerConfig()
+    config = ServerConfig.fromEnv()
     config.writeMethodEnd()
diff --git a/pyptlib/server_config.py b/pyptlib/server_config.py
index d6099c0..9fd8130 100644
--- a/pyptlib/server_config.py
+++ b/pyptlib/server_config.py
@@ -22,7 +22,8 @@ class ServerConfig(config.Config):
 
     :raises: :class:`pyptlib.config.EnvError` if environment was incomplete or corrupted.
     """
-    def __init__(self, stdout=sys.stdout):
+    @classmethod
+    def fromEnv(cls, stdout=sys.stdout):
         """
         TOR_PT_EXTENDED_SERVER_PORT is optional; tor uses the empty
         string as its value if it does not support the Extended
@@ -33,11 +34,11 @@ class ServerConfig(config.Config):
             if v == '': return None
             return util.parse_addr_spec(v)
 
-        self.extendedORPort = self.getEnv('TOR_PT_EXTENDED_SERVER_PORT', empty_or_valid_addr)
+        extendedORPort = cls.getEnv('TOR_PT_EXTENDED_SERVER_PORT', empty_or_valid_addr)
 
         # Check that either both Extended ORPort and the Extended
         # ORPort Authentication Cookie are present, or neither.
-        if self.extendedORPort:
+        if extendedORPort:
             def validate_authcookie(k, v):
                 if v is None: raise ValueError("Extended ORPort address provided, but no cookie file.")
                 return v
@@ -45,10 +46,10 @@ class ServerConfig(config.Config):
             def validate_authcookie(k, v):
                 if v is not None: raise ValueError("Extended ORPort Authentication cookie file provided, but no Extended ORPort address.")
                 return v
-        self.authCookieFile = self.getEnv('TOR_PT_AUTH_COOKIE_FILE', validate_authcookie)
+        authCookieFile = cls.getEnv('TOR_PT_AUTH_COOKIE_FILE', validate_authcookie)
 
         # Get ORPort.
-        self.ORPort = self.getEnv('TOR_PT_ORPORT', empty_or_valid_addr)
+        ORPort = cls.getEnv('TOR_PT_ORPORT', empty_or_valid_addr)
 
         # Get bind addresses.
         def validate_sever_bindaddr(k, bindaddrs):
@@ -59,19 +60,38 @@ class ServerConfig(config.Config):
                 (addr, port) = util.parse_addr_spec(addrport)
                 serverBindAddr[transport_name] = (addr, port)
             return serverBindAddr
-        self.serverBindAddr = self.getEnv('TOR_PT_SERVER_BINDADDR', validate_sever_bindaddr)
+        serverBindAddr = cls.getEnv('TOR_PT_SERVER_BINDADDR', validate_sever_bindaddr)
 
         # Get transports.
         def validate_transports(k, transports):
             transports = env_has_k(k, transports).split(',')
             t = sorted(transports)
-            b = sorted(self.serverBindAddr.keys())
+            b = sorted(serverBindAddr.keys())
             if t != b:
                 raise ValueError("Can't match transports with bind addresses (%s, %s)" % (t, b))
             return transports
-        transports = self.getEnv('TOR_PT_SERVER_TRANSPORTS', validate_transports)
-
-        config.Config.__init__(self, transports, stdout)
+        transports = cls.getEnv('TOR_PT_SERVER_TRANSPORTS', validate_transports)
+
+        return cls(
+            stateLocation = cls.getEnv('TOR_PT_STATE_LOCATION'),
+            managedTransportVer = cls.getEnv('TOR_PT_MANAGED_TRANSPORT_VER').split(','),
+            transports = transports,
+            serverBindAddr = serverBindAddr,
+            ORPort = ORPort,
+            extendedORPort = extendedORPort,
+            authCookieFile = authCookieFile,
+            stdout = stdout
+            )
+
+    def __init__(self, stateLocation, managedTransportVer, transports,
+                 serverBindAddr, ORPort, extendedORPort, authCookieFile,
+                 stdout=sys.stdout):
+        config.Config.__init__(self,
+            stateLocation, managedTransportVer, transports, stdout)
+        self.serverBindAddr = serverBindAddr
+        self.ORPort = ORPort
+        self.extendedORPort = extendedORPort
+        self.authCookieFile = authCookieFile
 
     def getExtendedORPort(self):
         """
@@ -138,22 +158,3 @@ class ServerConfig(config.Config):
         """
 
         self.emit('SMETHODS DONE')
-
-    def get_addrport(self, key):
-        """
-        Parse an environment variable holding an address:port value.
-
-        :param str key: Environment variable key.
-
-        :returns: tuple -- (address,port)
-
-        :raises: :class:`pyptlib.config.EnvError` if string was not in address:port format.
-        """
-
-        string = self.get(key)
-        try:
-            return util.parse_addr_spec(string)
-        except ValueError, err:
-            self.writeEnvError(err)
-            raise config.EnvError(err)
-

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/pyptlib.git



More information about the Pkg-privacy-commits mailing list