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

[tor-commits] [stem/master] Allowing stem.descriptor.parse_file() to handle paths



commit fa0fbd195d0531980a6e250ee2e32c99a0708259
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Mar 24 20:52:07 2013 -0700

    Allowing stem.descriptor.parse_file() to handle paths
    
    Several times now I've passed a string path to parse_file() rather than a file
    object and been confused for a sec by the stacktrace. On reflection there's no
    reason *not* to accept paths - that's commonly what we want it for after all
    and eliminating the extra open() lets our callers have much nicer code.
---
 stem/descriptor/__init__.py        |   11 ++++++++++-
 stem/descriptor/microdescriptor.py |   14 ++++++--------
 2 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index 501ac5d..ef438a7 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -125,7 +125,7 @@ def parse_file(descriptor_file, descriptor_type = None, validate = True, documen
 
     my_descriptor_file = open(descriptor_path, 'rb')
 
-  :param file descriptor_file: opened file with the descriptor contents
+  :param str,file descriptor_file: path or opened file with the descriptor contents
   :param str descriptor_type: `descriptor type <https://metrics.torproject.org/formats.html#descriptortypes>`_, this is guessed if not provided
   :param bool validate: checks the validity of the descriptor's content if
     **True**, skips these checks otherwise
@@ -139,6 +139,15 @@ def parse_file(descriptor_file, descriptor_type = None, validate = True, documen
     * **IOError** if unable to read from the descriptor_file
   """
 
+  # if we got a path then open that file for parsing
+
+  if isinstance(descriptor_file, (bytes, unicode)):
+    with open(descriptor_file) as desc_file:
+      for desc in parse_file(desc_file, descriptor_type, validate, document_handler):
+        yield desc
+
+      return
+
   import stem.descriptor.server_descriptor
   import stem.descriptor.extrainfo_descriptor
   import stem.descriptor.networkstatus
diff --git a/stem/descriptor/microdescriptor.py b/stem/descriptor/microdescriptor.py
index 4fc2c31..ac1eb2e 100644
--- a/stem/descriptor/microdescriptor.py
+++ b/stem/descriptor/microdescriptor.py
@@ -41,10 +41,9 @@ the exit relays.
 
     print "Exit Relays:"
 
-    with open(os.path.join(data_dir, 'cached-microdesc-consensus')) as desc_file:
-      for desc in parse_file(desc_file):
-        if desc.digest in exit_digests:
-          print "  %s (%s)" % (desc.nickname, desc.fingerprint)
+    for desc in parse_file(os.path.join(data_dir, 'cached-microdesc-consensus')):
+      if desc.digest in exit_digests:
+        print "  %s (%s)" % (desc.nickname, desc.fingerprint)
 
 Doing the same is trivial with server descriptors...
 
@@ -54,10 +53,9 @@ Doing the same is trivial with server descriptors...
 
   print "Exit Relays:"
 
-  with open("/home/atagar/.tor/cached-descriptors") as desc_file:
-    for desc in parse_file(desc_file):
-      if desc.exit_policy.is_exiting_allowed():
-        print "  %s (%s)" % (desc.nickname, desc.fingerprint)
+  for desc in parse_file("/home/atagar/.tor/cached-descriptors"):
+    if desc.exit_policy.is_exiting_allowed():
+      print "  %s (%s)" % (desc.nickname, desc.fingerprint)
 
 **Module Overview:**
 

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