[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Allow plaintext enum value



commit 45b98cac45f3f80f6bb319d898ad12775b30823a
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Jun 23 14:29:14 2019 -0700

    Allow plaintext enum value
    
    Our CollecTor module used None to specify plaintext but we should accept the
    Compression.PLAINTEXT value as well.
---
 stem/descriptor/collector.py      | 22 +++++++++++-----------
 test/unit/descriptor/collector.py |  5 +++--
 2 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/stem/descriptor/collector.py b/stem/descriptor/collector.py
index 7a3d04ff..21c9d91d 100644
--- a/stem/descriptor/collector.py
+++ b/stem/descriptor/collector.py
@@ -65,7 +65,7 @@ COLLECTOR_URL = 'https://collector.torproject.org/'
 REFRESH_INDEX_RATE = 3600  # get new index if cached copy is an hour old
 
 
-def url(resource, compression = None):
+def url(resource, compression = Compression.PLAINTEXT):
   """
   Provides CollecTor url for the given resource.
 
@@ -84,8 +84,8 @@ def url(resource, compression = None):
   else:
     raise ValueError("'%s' isn't a recognized resource type" % resource)
 
-  suffix = compression.extension if compression else ''
-  return COLLECTOR_URL + '/'.join(path) + suffix
+  extension = compression.extension if compression not in (None, Compression.PLAINTEXT) else ''
+  return COLLECTOR_URL + '/'.join(path) + extension
 
 
 class CollecTor(object):
@@ -102,22 +102,22 @@ class CollecTor(object):
   """
 
   def __init__(self, compression = 'best', retries = 2, timeout = None):
+    self.compression = Compression.PLAINTEXT
+    self.retries = retries
+    self.timeout = timeout
+
+    self._cached_index = None
+    self._cached_index_at = 0
+
     if compression == 'best':
-      self.compression = None
 
       for option in (Compression.LZMA, Compression.BZ2, Compression.GZIP):
         if option.available:
           self.compression = option
           break
-    else:
+    elif compression is not None:
       self.compression = compression
 
-    self.retries = retries
-    self.timeout = timeout
-
-    self._cached_index = None
-    self._cached_index_at = 0
-
   def index(self):
     """
     Provides the archives available in CollecTor.
diff --git a/test/unit/descriptor/collector.py b/test/unit/descriptor/collector.py
index b2b464ce..6aeda8f0 100644
--- a/test/unit/descriptor/collector.py
+++ b/test/unit/descriptor/collector.py
@@ -23,6 +23,7 @@ class TestCollector(unittest.TestCase):
   def test_url(self):
     self.assertEqual('https://collector.torproject.org/index/index.json', url('index'))
     self.assertEqual('https://collector.torproject.org/index/index.json', url('index', compression = None))
+    self.assertEqual('https://collector.torproject.org/index/index.json', url('index', compression = Compression.PLAINTEXT))
     self.assertEqual('https://collector.torproject.org/index/index.json.gz', url('index', compression = Compression.GZIP))
     self.assertEqual('https://collector.torproject.org/index/index.json.bz2', url('index', compression = Compression.BZ2))
     self.assertEqual('https://collector.torproject.org/index/index.json.xz', url('index', compression = Compression.LZMA))
@@ -35,12 +36,12 @@ class TestCollector(unittest.TestCase):
       'path': 'https://collector.torproject.org'
     }
 
-    collector = CollecTor(compression = None)
+    collector = CollecTor(compression = Compression.PLAINTEXT)
     self.assertEqual(expected, collector.index())
 
   @patch(URL_OPEN, Mock(return_value = io.BytesIO(b'not json')))
   def test_index_malformed_json(self):
-    collector = CollecTor(compression = None)
+    collector = CollecTor(compression = Compression.PLAINTEXT)
 
     if stem.prereq.is_python_3():
       self.assertRaisesRegexp(ValueError, 'Expecting value: line 1 column 1', collector.index)



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits