[tor-commits] [bridgedb/develop] Fix Filters.filterBridgesByNotBlockedIn to work with new Bridge class.

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


commit 98cf68d778480aa005e5914cac47164f169b87b9
Author: Isis Lovecruft <isis at torproject.org>
Date:   Thu Apr 16 20:37:24 2015 +0000

    Fix Filters.filterBridgesByNotBlockedIn to work with new Bridge class.
---
 lib/bridgedb/Filters.py           |   37 +++++++++++++++++--------------------
 lib/bridgedb/bridgerequest.py     |    4 +---
 lib/bridgedb/test/legacy_Tests.py |    2 +-
 3 files changed, 19 insertions(+), 24 deletions(-)

diff --git a/lib/bridgedb/Filters.py b/lib/bridgedb/Filters.py
index 94d4325..41df297 100644
--- a/lib/bridgedb/Filters.py
+++ b/lib/bridgedb/Filters.py
@@ -92,28 +92,25 @@ def filterBridgesByTransport(methodname, addressClass=None):
         funcs[ruleset] = _filterByTransport
         return _filterByTransport
 
-def filterBridgesByNotBlockedIn(countryCode, addressClass=None, methodname=None):
-    """ if at least one address:port of the selected addressClass and
-    (optional) transport type is not blocked in countryCode, return True
+def filterBridgesByNotBlockedIn(countryCode):
+    """Return ``True`` if at least one of a bridge's (transport) bridgelines isn't
+    known to be blocked in **countryCode**.
+
+    :param str countryCode: A two-letter country code.
+    :rtype: bool
+    :returns: ``True`` if at least one address of the bridge isn't blocked.
+        ``False`` otherwise.
     """
-    # default to IPv4 if not specified
-    if addressClass is None: addressClass = IPv4Address
-    assert (addressClass) in (IPv4Address, IPv6Address)
-    ruleset = frozenset([countryCode, addressClass, methodname])
+    countryCode = countryCode.lower()
+    ruleset = frozenset([countryCode])
     try:
         return funcs[ruleset]
     except KeyError:
-        def f(bridge):
-            if bridge.isBlocked(countryCode, addressClass, methodname):
-                if addressClass is IPv4Address: ac = "IPv4"
-                else: ac = "IPv6"
-                logmsg = "Removing %s from set of results for country"
-                logmsg += " '%s' with address class %s and transport %s"
-                logging.debug(logmsg % ( bridge.fingerprint, countryCode, ac,
-                    methodname))
+        def _filterByNotBlockedIn(bridge):
+            if bridge.isBlockedIn(countryCode):
                 return False
-            return True # not blocked
-        f.__name__ = "filterBridgesNotBlockedIn(%s,%s,%s)" % \
-                (countryCode,methodname,addressClass)
-        funcs[ruleset] = f
-        return f
+            return True
+        _filterByNotBlockedIn.__name__ = "filterBridgesByNotBlockedIn(%s)" % countryCode
+        setattr(_filterByNotBlockedIn, "description", "unblocked=%s" % countryCode)
+        funcs[ruleset] = _filterByNotBlockedIn
+        return _filterByNotBlockedIn
diff --git a/lib/bridgedb/bridgerequest.py b/lib/bridgedb/bridgerequest.py
index f4eaaa4..532558d 100644
--- a/lib/bridgedb/bridgerequest.py
+++ b/lib/bridgedb/bridgerequest.py
@@ -182,6 +182,4 @@ class BridgeRequestBase(object):
             self.addFilter(Filters.filterBridgesByTransport(transport,
                                                             self.addressClass))
         for country in self.notBlockedIn:
-            self.addFilter(Filters.filterBridgesByNotBlockedIn(country.lower(),
-                                                               self.addressClass,
-                                                               transport))
+            self.addFilter(Filters.filterBridgesByNotBlockedIn(country.lower()))
diff --git a/lib/bridgedb/test/legacy_Tests.py b/lib/bridgedb/test/legacy_Tests.py
index cc1ba6d..5621515 100644
--- a/lib/bridgedb/test/legacy_Tests.py
+++ b/lib/bridgedb/test/legacy_Tests.py
@@ -357,7 +357,7 @@ class IPBridgeDistTests(unittest.TestCase):
         for i in xrange(5):
             b = d.getBridges(randomIPString(), "x", 1,
                     bridgeFilterRules=[
-                        filterBridgesByNotBlockedIn("cn", methodname='obfs2'),
+                        filterBridgesByNotBlockedIn("cn"),
                         filterBridgesByTransport('obfs2'),
                         ])
             try: assert len(b) > 0





More information about the tor-commits mailing list