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

[tor-commits] [stem/master] Iterating over routers with parse_file()



commit 7927579282e6b6a3071dc1901c0a725a94fa7563
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sat Aug 18 16:45:43 2012 -0700

    Iterating over routers with parse_file()
    
    Having parse_file() iterate over the RouterDescriptors in a consensus rather
    than providing the document itself. 99.9% of the time our callers want to do
    something with the routers, and providing them a document instead just forces
    them to unwrap it.
    
    I'm thinking about changing the networkstatus module to provide
    RouterDescriptors that reference the NetworkStatusDocument they came from
    rather than the other way around. We'll see how it works.
---
 stem/descriptor/__init__.py            |   14 ++++++++++----
 test/integ/descriptor/networkstatus.py |    8 ++++----
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index 8e2bdc7..e6f3e4c 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -65,9 +65,9 @@ def parse_file(path, descriptor_file):
   elif filename == "cached-extrainfo":
     file_parser = stem.descriptor.extrainfo_descriptor.parse_file
   elif filename == "cached-consensus":
-    file_parser = lambda f: [stem.descriptor.networkstatus.parse_file(f)]
+    file_parser = lambda f: stem.descriptor.networkstatus.parse_file(f).router_descriptors
   elif filename == "cached-microdesc-consensus":
-    file_parser = lambda f: [stem.descriptor.networkstatus.parse_file(f, True, "microdesc")]
+    file_parser = lambda f: stem.descriptor.networkstatus.parse_file(f, True, "microdesc").router_descriptors
   else:
     # Metrics descriptor handling
     first_line, desc = descriptor_file.readline().strip(), None
@@ -107,9 +107,15 @@ def _parse_metrics_file(descriptor_type, major_version, minor_version, descripto
     
     yield stem.descriptor.extrainfo_descriptor.BridgeExtraInfoDescriptor(descriptor_file.read())
   elif descriptor_type in ("network-status-consensus-3", "network-status-vote-3") and major_version == 1:
-    yield stem.descriptor.networkstatus.parse_file(descriptor_file)
+    consensus = stem.descriptor.networkstatus.parse_file(descriptor_file)
+    
+    for desc in consensus.router_descriptors:
+      yield desc
   elif descriptor_type == "network-status-microdesc-consensus-3" and major_version == 1:
-    yield stem.descriptor.networkstatus.parse_file(descriptor_file, flavour = "microdesc")
+    consensus = stem.descriptor.networkstatus.parse_file(descriptor_file, flavour = "microdesc")
+    
+    for desc in consensus.router_descriptors:
+      yield desc
   else:
     raise TypeError("Unrecognized metrics descriptor format. type: '%s', version: '%i.%i'" % (descriptor_type, major_version, minor_version))
 
diff --git a/test/integ/descriptor/networkstatus.py b/test/integ/descriptor/networkstatus.py
index 4cbf42c..11bd3e3 100644
--- a/test/integ/descriptor/networkstatus.py
+++ b/test/integ/descriptor/networkstatus.py
@@ -56,9 +56,9 @@ class TestNetworkStatusDocument(unittest.TestCase):
     descriptor_path = test.integ.descriptor.get_resource("metrics_consensus")
     
     with file(descriptor_path) as descriptor_file:
-      desc = stem.descriptor.parse_file(descriptor_path, descriptor_file)
+      descriptors = stem.descriptor.parse_file(descriptor_path, descriptor_file)
       
-      router = next(next(desc).router_descriptors)
+      router = next(descriptors)
       self.assertEquals("sumkledi", router.nickname)
       self.assertEquals("ABPSI4nNUNC3hKPkBhyzHozozrU", router.identity)
       self.assertEquals("8mCr8Sl7RF4ENU4jb0FZFA/3do8", router.digest)
@@ -148,9 +148,9 @@ I/TJmV928na7RLZe2mGHCAW3VQOvV+QkCfj05VZ8CsY=
     descriptor_path = test.integ.descriptor.get_resource("metrics_vote")
     
     with file(descriptor_path) as descriptor_file:
-      desc = stem.descriptor.parse_file(descriptor_path, descriptor_file)
+      descriptors = stem.descriptor.parse_file(descriptor_path, descriptor_file)
       
-      router = next(next(desc).router_descriptors)
+      router = next(descriptors)
       self.assertEquals("sumkledi", router.nickname)
       self.assertEquals("ABPSI4nNUNC3hKPkBhyzHozozrU", router.identity)
       self.assertEquals("B5n4BiALAF8B5AqafxohyYiuj7E", router.digest)



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