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

[tor-commits] [stem/master] Correct caching example



commit 39c82a7c88480f2da22a7b766c58998d112c08a4
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Thu Aug 15 15:01:48 2019 -0700

    Correct caching example
    
    I whipped this up prior to writing the module. While developing it I paid
    special care to this use case so it's actually more elegant than the demo I
    anticipated. Running this example now works.
---
 stem/descriptor/collector.py | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/stem/descriptor/collector.py b/stem/descriptor/collector.py
index e8e798b3..20eb6872 100644
--- a/stem/descriptor/collector.py
+++ b/stem/descriptor/collector.py
@@ -29,22 +29,23 @@ With this you can either download and read directly from CollecTor...
 ::
 
   import datetime
-  import os
   import stem.descriptor
   import stem.descriptor.collector
 
   yesterday = datetime.datetime.utcnow() - datetime.timedelta(days = 1)
-  path = os.path.expanduser('~/descriptor_cache/server_desc_today')
+  cache_dir = '~/descriptor_cache/server_desc_today'
 
-  with open(path, 'wb') as cache_file:
-    for desc in stem.descriptor.collector.get_server_descriptors(start = yesterday):
-      cache_file.write(desc.get_bytes())
+  collector = stem.descriptor.collector.CollecTor()
+
+  for f in collector.files('server-descriptor', start = yesterday):
+    f.download(cache_dir)
 
   # then later...
 
-  for desc in stem.descriptor.parse_file(path, descriptor_type = 'server-descriptor 1.0'):
-    if desc.exit_policy.is_exiting_allowed():
-      print('  %s (%s)' % (desc.nickname, desc.fingerprint))
+  for f in collector.files('server-descriptor', start = yesterday):
+    for desc in f.read(cache_dir):
+      if desc.exit_policy.is_exiting_allowed():
+        print('  %s (%s)' % (desc.nickname, desc.fingerprint))
 
 ::
 
@@ -355,6 +356,8 @@ class File(object):
     if self.compression != Compression.PLAINTEXT and decompress:
       filename = filename.rsplit('.', 1)[0]
 
+    directory = os.path.expanduser(directory)
+
     path = os.path.join(directory, filename)
 
     if not os.path.exists(directory):



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