[Pkg-privacy-commits] [pyptlib] 50/136: Improve the code and documentation some more.

Ximin Luo infinity0 at moszumanska.debian.org
Sat Aug 22 13:25:06 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 d5605be2ad35c42cf5b1105282b6ea1395f06950
Author: George Kadianakis <desnacked at riseup.net>
Date:   Thu Oct 4 22:58:02 2012 -0400

    Improve the code and documentation some more.
    
    - Update README.
    - Reset .gitignore.
    - Fix documentation.
    - Rename EnvException to EnvError.
---
 .gitignore               | 10 ------
 README                   | 27 ++++++++++++++--
 pyptlib/client.py        |  6 ++--
 pyptlib/client_config.py | 24 ++++++--------
 pyptlib/config.py        |  8 ++---
 pyptlib/server.py        |  6 ++--
 pyptlib/server_config.py | 84 +++++++++++++++++++++++++-----------------------
 pyptlib/util.py          |  4 +--
 setup.py                 |  2 --
 9 files changed, 90 insertions(+), 81 deletions(-)

diff --git a/.gitignore b/.gitignore
index d800abb..e69de29 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,10 +0,0 @@
-pytidy.py
-tidy.py
-tidyall
-.DS_Store
-pyptlib.xcodeproj/*
-build/*
-dist/*
-pyptlib.egg-info/*
-*.pyc
-src/pyptlib/*.pyc
diff --git a/README b/README
index 3b3f311..097bb0c 100644
--- a/README
+++ b/README
@@ -1,2 +1,25 @@
-This is a python implementation of the Pluggable Transports for Circumvention specification for Tor:
-https://gitweb.torproject.org/torspec.git/blob_plain/HEAD:/proposals/180-pluggable-transport.txt
+- What is pyptlib?
+
+pyptlib is a little Python library which understands the 'pluggable
+transport managed-proxy protocol' [0].
+
+- Who is interested in pyptlib?
+
+You might be interested in pyptlib if you have an application that
+obfuscates TCP traffic and you want to integrate it easily with Tor.
+
+- What does pyptlib do?
+
+When pyptlib is initialized by your application, pyptlib communicates
+with Tor and learns which pluggable transports Tor needs, in which
+ports you should listen for connections, in which filesystem directory
+you should keep state, etc.
+Pyptlib then reports that information to your application and your
+application has to follow Tor's wishes.
+
+- How do I use pyptlib?
+
+Read the API file and see the examples/ directory. The source code
+might also be useful.
+
+[0]: https://gitweb.torproject.org/torspec.git/blob_plain/HEAD:/proposals/180-pluggable-transport.txt
diff --git a/pyptlib/client.py b/pyptlib/client.py
index eccea49..a0d5b9d 100644
--- a/pyptlib/client.py
+++ b/pyptlib/client.py
@@ -5,7 +5,7 @@
 This module provides a convenient API for writing pluggable transport clients.
 """
 
-from pyptlib.config import EnvException
+from pyptlib.config import EnvError
 from pyptlib.client_config import ClientConfig
 
 
@@ -25,7 +25,7 @@ def init(supported_transports):
     'transports' : The names of the transports that must be
     launched. The list can be empty.
 
-    Throws EnvException.
+    Throws EnvError.
     """
 
     supportedTransportVersion = '1'
@@ -36,7 +36,7 @@ def init(supported_transports):
         config.writeVersion(supportedTransportVersion)
     else:
         config.writeVersionError()
-        raise EnvException("Unsupported managed proxy protocol version (%s)" %
+        raise EnvError("Unsupported managed proxy protocol version (%s)" %
                            str(config.getManagedTransportVersions()))
 
     retval = {}
diff --git a/pyptlib/client_config.py b/pyptlib/client_config.py
index 4acc6f7..f2a5039 100644
--- a/pyptlib/client_config.py
+++ b/pyptlib/client_config.py
@@ -2,31 +2,25 @@
 # -*- coding: utf-8 -*-
 
 """
-This module inherits from pyptlib.config and contains just the parts
-of the API which are specific to the client implementations of the
-protocol.
+This module contains parts of the API that are only useful to clients.
 """
 
 from pyptlib.config import Config
 
-__docformat__ = 'restructuredtext'
-
-
 class ClientConfig(Config):
     """
-    This class inherits from pyptlib.config.Config and contains just
-    the parts of the API which are specific to the client
-    implementations of the protocol.
-    """
+    Attributes:
 
-  # Public methods
+    self.transports: List with strings of pluggable transport names
+    that Tor wants us to handle.
 
+    self.allTransportsEnabled: True if Tor wants us to spawn all the
+    transports.
+    """
     def __init__(self):
         """
-        Initialize the ClientConfig object.
-        This causes the state location, managed transport, and transports version to be set.
-
-        Throws EnvException.
+        Initializer.
+        Throws EnvError.
         """
 
         Config.__init__(self)
diff --git a/pyptlib/config.py b/pyptlib/config.py
index b6d4e77..aa859dd 100644
--- a/pyptlib/config.py
+++ b/pyptlib/config.py
@@ -29,7 +29,7 @@ class Config:
         Initialize the Config object. this causes the state location
         and managed transport version to be set.
 
-        Throws EnvException.
+        Throws EnvError.
         """
 
         self.stateLocation = self.get('TOR_PT_STATE_LOCATION')
@@ -102,7 +102,7 @@ class Config:
         """
         Attempts to fetch the given key from the environment
         variables. If it is present, it is returned, otherwise an
-        EnvException is thrown.
+        EnvError is thrown.
         """
 
         if key in os.environ:
@@ -110,7 +110,7 @@ class Config:
         else:
             message = 'Missing environment variable %s' % key
             self.writeEnvError(message)
-            raise EnvException(message)
+            raise EnvError(message)
 
     def emit(self, msg):
         print msg
@@ -121,4 +121,4 @@ class Config:
 Exception thrown when there is an error parsing managed proxy
 environment variables. Also sends an ENV-ERROR to Tor.
 """
-class EnvException(Exception): pass
+class EnvError(Exception): pass
diff --git a/pyptlib/server.py b/pyptlib/server.py
index bcc2ae8..900667e 100644
--- a/pyptlib/server.py
+++ b/pyptlib/server.py
@@ -3,7 +3,7 @@
 
 """ The pyptlib.easy.server module includes a convenient API for writing pluggable transport servers. """
 
-from pyptlib.config import EnvException
+from pyptlib.config import EnvError
 from pyptlib.server_config import ServerConfig
 
 
@@ -32,7 +32,7 @@ def init(supported_transports):
     spawned, and [<addr>, <port>] is a list containing the location
     where that transport should bind. The dictionary can be empty.
 
-    Throws EnvException.
+    Throws EnvError.
     """
 
     supportedTransportVersion = '1'
@@ -43,7 +43,7 @@ def init(supported_transports):
         config.writeVersion(supportedTransportVersion)
     else:
         config.writeVersionError()
-        raise EnvException("Unsupported managed proxy protocol version (%s)" %
+        raise EnvError("Unsupported managed proxy protocol version (%s)" %
                            str(config.getManagedTransportVersions()))
 
     retval = {}
diff --git a/pyptlib/server_config.py b/pyptlib/server_config.py
index 181c071..57ee02e 100644
--- a/pyptlib/server_config.py
+++ b/pyptlib/server_config.py
@@ -2,80 +2,75 @@
 # -*- coding: utf-8 -*-
 
 """
-This module inherits from pyptlib.config and contains just the parts
-of the API which are specific to the server implementations of the
-protocol.
+This module contains parts of the API that are only useful to servers.
 """
 
-import os
-
 import pyptlib.config as config
 
-__docformat__ = 'restructuredtext'
-
 class ServerConfig(config.Config):
     """
-    This class inherits from pyptlib.config.Config and contains just
-    the parts of the API which are specific to the client
-    implementations of the protocol.
-    """
-  # Public methods
+    Attributes:
+
+    self.transports: List with strings of pluggable transport names
+    that Tor wants us to handle.
+
+    self.allTransportsEnabled: True if Tor wants us to spawn all the
+    transports.
 
+    self.extendedORPort: '(<ip>,<port>)' tuple pointing to Tor's
+    Extended ORPort. 'None' if Extended ORPort is not supported.
+
+    self.serverBindAddr: A dictionary {<transport> : [<addr>, <port>]},
+    where <transport> is the name of the transport that must be
+    spawned, and [<addr>, <port>] is a list containing the location
+    where that transport should bind. The dictionary can be empty.
+    """
     def __init__(self):
         """
-        Initialize the ClientConfig object.
-        This causes the state location, managed transport, and transports version to be set.
-
-        Throws EnvException.
+        Initializer.
+        Throws EnvError.
         """
 
         config.Config.__init__(self)
 
-        # TOR_PT_EXTENDED_SERVER_PORT is optional; tor uses the empty
-        # string as its value if it does not support the Extended
-        # ORPort.
+        """
+        TOR_PT_EXTENDED_SERVER_PORT is optional; tor uses the empty
+        string as its value if it does not support the Extended
+        ORPort.
+        """
         ext_orport_tmp = self.get('TOR_PT_EXTENDED_SERVER_PORT')
         if ext_orport_tmp == '':
             self.extendedORPort = None
         else:
             self.extendedORPort = self.get_addrport('TOR_PT_EXTENDED_SERVER_PORT')
 
+        # Get ORPort.
         self.ORPort = self.get_addrport('TOR_PT_ORPORT')
 
+        # Get bind addresses.
         self.serverBindAddr = {}
         bindaddrs = self.get('TOR_PT_SERVER_BINDADDR').split(',')
-        for bind in bindaddrs:
-            (key, value) = bind.split('-')
-            self.serverBindAddr[key] = value.split(":") # XXX ugly code
-            self.serverBindAddr[key][1] = int(self.serverBindAddr[key][1]) # XXX ugly code
+        for bindaddr in bindaddrs:
+            (transport_name, addrport) = bindaddr.split('-')
+            (addr, port) = get_addrport_from_string(addrport)
+            self.serverBindAddr[key] = (addr, port)
 
+        # Get transports.
         self.transports = self.get('TOR_PT_SERVER_TRANSPORTS').split(',')
         if '*' in self.transports:
             self.allTransportsEnabled = True
             self.transports.remove('*')
 
     def getExtendedORPort(self):
-        """ Returns a tuple (str,int) representing the address of the Tor server port as reported by Tor """
-
         return self.extendedORPort
 
     def getORPort(self):
-        """ Returns a tuple (str,int) representing the address of the Tor OR port as reported by Tor """
-
         return self.ORPort
 
     def getServerBindAddresses(self):
-        """ Returns a dict {str: (str,int)} representing the addresses for each transport as reported by Tor """
-
         return self.serverBindAddr
 
     def getServerTransports(self):
-        """
-        Returns a list of strings representing the server
-        transports reported by Tor. If present, '*' is stripped from
-        this list and used to set allTransportsEnabled to True.
-        """
-
         return self.transports
 
     def writeMethod(self, name, address, options):
@@ -109,21 +104,30 @@ class ServerConfig(config.Config):
         Given an environment variable name in 'key' with an
         '<addr>:<port>' value, return [<addr>,<port>].
 
-        Throws EnvException.
+        Throws EnvError.
         """
         string = self.get(key)
+        return self.get_addrport_from_string(string)
+
+    def get_addrport_from_string(self, string):
+        """
+        Given a string in 'string' with an '<addr>:<port>' value,
+        return [<addr>,<port>].
+
+        Throws EnvError.
+        """
 
         addrport = string.split(':')
 
         if (len(addrport) != 2) or (not addrport[1].isdigit()):
-            message = '%s: Parsing error (%s).' % (key, string)
+            message = 'Parsing error (%s).' % (string)
             self.writeEnvError(message)
-            raise config.EnvException(message)
+            raise config.EnvError(message)
 
         if (not 0 <= int(addrport[1]) < 65536):
-            message = '%s: Port out of range (%s).' % (key, string)
+            message = 'Port out of range (%s).' % (string)
             self.writeEnvError(message)
-            raise config.EnvException(message)
+            raise config.EnvError(message)
 
         return addrport
 
diff --git a/pyptlib/util.py b/pyptlib/util.py
index 6c3412b..2d4c064 100644
--- a/pyptlib/util.py
+++ b/pyptlib/util.py
@@ -3,7 +3,7 @@
 
 """ The pyptlib.util module contains useful functions that don't fit in anywhere else. """
 
-from pyptlib.config import Config, EnvException
+from pyptlib.config import Config, EnvError
 
 
 def checkClientMode():
@@ -12,7 +12,7 @@ def checkClientMode():
     try:
         c = Config()
         return c.checkClientMode()
-    except EnvException:
+    except EnvError:
         return False
 
 
diff --git a/setup.py b/setup.py
index 319a60e..e296c6a 100644
--- a/setup.py
+++ b/setup.py
@@ -2,8 +2,6 @@
 
 import sys
 
-sys.path.insert(0, 'src')
-
 from setuptools import setup
 
 setup(name='pyptlib',

-- 
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