[tor-commits] [bridgedb/develop] Change PluggableTransport.methodname to be a @property.

isis at torproject.org isis at torproject.org
Thu Jun 25 07:10:55 UTC 2015


commit 70019ff8944cd29de272d34f432247b055045006
Author: Isis Lovecruft <isis at torproject.org>
Date:   Tue Apr 21 03:02:57 2015 +0000

    Change PluggableTransport.methodname to be a @property.
    
    This ensures that it's always lowercased when set, removing the need for
    so many lower() calls.
---
 lib/bridgedb/Filters.py           |   12 ++++++------
 lib/bridgedb/bridges.py           |   29 ++++++++++++++++++++++++++---
 lib/bridgedb/test/test_bridges.py |   10 +++++-----
 3 files changed, 37 insertions(+), 14 deletions(-)

diff --git a/lib/bridgedb/Filters.py b/lib/bridgedb/Filters.py
index cb5e782..eb7db4e 100644
--- a/lib/bridgedb/Filters.py
+++ b/lib/bridgedb/Filters.py
@@ -71,19 +71,19 @@ def filterBridgesByTransport(methodname, addressClass=None):
     if not ((addressClass is IPv4Address) or (addressClass is IPv6Address)):
         addressClass = IPv4Address
 
+    # Ignore case
+    methodname = methodname.lower()
+
     ruleset = frozenset([methodname, addressClass])
     try:
         return funcs[ruleset]
     except KeyError:
-
         def _filterByTransport(bridge):
             for transport in bridge.transports:
-                if isinstance(transport.address, addressClass):
-                    # ignore method name case
-                    if transport.methodname.lower() == methodname.lower():
-                        return True
+                if (transport.methodname == methodname and
+                    isinstance(transport.address, addressClass)):
+                    return True
             return False
-
         _filterByTransport.__name__ = ("filterBridgesByTransport(%s,%s)"
                                        % (methodname, addressClass))
         setattr(_filterByTransport, "description", "transport=%s" % methodname)
diff --git a/lib/bridgedb/bridges.py b/lib/bridgedb/bridges.py
index dc5b78d..c98d54c 100644
--- a/lib/bridgedb/bridges.py
+++ b/lib/bridgedb/bridges.py
@@ -316,14 +316,13 @@ class PluggableTransport(BridgeAddressBase):
         super(PluggableTransport, self).__init__()
         self._port = None
         self._methodname = None
-        self._arguments = None
+        self._blockedIn = {}
 
         self.fingerprint = fingerprint
         self.address = address
         self.port = port
         self.methodname = methodname
         self.arguments = arguments
-        self._blockedIn = {}
 
         # Because we can intitialise this class with the __init__()
         # parameters, or use the ``updateFromStemTransport()`` method, we'll
@@ -464,6 +463,30 @@ class PluggableTransport(BridgeAddressBase):
         """Reset this ``PluggableTransport``'s port to ``None``."""
         self._port = None
 
+    @property
+    def methodname(self):
+        """Get this :class:`PluggableTransport`'s methodname.
+
+        :rtype: str
+        :returns: The (lowercased) methodname of this ``PluggableTransport``,
+            i.e. ``"obfs3"``, ``"scramblesuit"``, etc.
+        """
+        return self._methodname
+
+    @methodname.setter
+    def methodname(self, value):
+        """Set this ``PluggableTransport``'s methodname.
+
+        .. hint:: The **value** will be automatically lowercased.
+
+        :param str value: The new methodname.
+        """
+        if value:
+            try:
+                self._methodname = value.lower()
+            except (AttributeError, TypeError):
+                raise TypeError("methodname must be a str or unicode")
+
     def getTransportLine(self, includeFingerprint=True, bridgePrefix=False):
         """Get a Bridge Line for this :class:`PluggableTransport`.
 
@@ -1317,7 +1340,7 @@ class Bridge(BridgeBackwardsCompatibility):
             **countryCode**, ``False`` otherwise.
         """
         for pt in self.transports:
-            if pt.methodname.lower() == methodname.lower():
+            if pt.methodname == methodname.lower():
                 if self.addressIsBlockedIn(countryCode, pt.address, pt.port):
                     logging.info("Transport %s of bridge %s is blocked in %s."
                                  % (pt.methodname, self, countryCode))
diff --git a/lib/bridgedb/test/test_bridges.py b/lib/bridgedb/test/test_bridges.py
index 499a8c2..db662ae 100644
--- a/lib/bridgedb/test/test_bridges.py
+++ b/lib/bridgedb/test/test_bridges.py
@@ -659,11 +659,11 @@ class PluggableTransportTests(unittest.TestCase):
         bridgeLine = pt.getTransportLine()
 
         # We have to check for substrings because we don't know which order
-        # the PT arguments will end up in the bridge line.  Fortunately, the
-        # following three are the only ones which are important to have in
-        # order:
-        self.assertTrue(bridgeLine.startswith("voltronPT"))
-        self.assertSubstring("voltronPT 1.2.3.4:443 " + self.fingerprint,
+        # the PT arguments will end up in the bridge line.  We also have to
+        # check for the lowercased transport name.  Fortunately, the following
+        # three are the only ones which are important to have in order:
+        self.assertTrue(bridgeLine.startswith("voltronpt"))
+        self.assertSubstring("voltronpt 1.2.3.4:443 " + self.fingerprint,
                              bridgeLine)
         # These ones can be in any order, but they should be at the end of the
         # bridge line:





More information about the tor-commits mailing list