[tor-commits] [stem/master] Dropping the 'path' argument from stem.descriptor.parse_file()

atagar at torproject.org atagar at torproject.org
Sun Feb 17 06:07:15 UTC 2013


commit 8cdcb0883f0da8bb492da31cfea7155df5e9efed
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Feb 15 09:38:02 2013 -0800

    Dropping the 'path' argument from stem.descriptor.parse_file()
    
    File objects have a 'name' attribute that we can use to guess the path. This
    isn't entirely reliable, but nothing is...
    
    http://stackoverflow.com/questions/2458676/absolute-path-of-a-file-object
    
    The path argument was only there to support the descriptor reader. Now that
    parse_file() is something for our users it's nice to get rid of arguments they
    can't use.
---
 stem/descriptor/__init__.py            |   10 +++++-----
 stem/descriptor/reader.py              |    4 ++--
 test/integ/descriptor/networkstatus.py |   14 +++++++-------
 3 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index 669485c..6e5d68f 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -68,7 +68,7 @@ DocumentHandler = stem.util.enum.UppercaseEnum(
 )
 
 
-def parse_file(descriptor_file, descriptor_type = None, path = None, validate = True, document_handler = DocumentHandler.ENTRIES):
+def parse_file(descriptor_file, descriptor_type = None, validate = True, document_handler = DocumentHandler.ENTRIES):
   """
   Simple function to read the descriptor contents from a file, providing an
   iterator for its :class:`~stem.descriptor.__init__.Descriptor` contents.
@@ -120,7 +120,6 @@ def parse_file(descriptor_file, descriptor_type = None, path = None, validate =
 
   :param file descriptor_file: 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 str path: absolute path to the file's location on disk
   :param bool validate: checks the validity of the descriptor's content if
     **True**, skips these checks otherwise
   :param stem.descriptor.__init__.DocumentHandler document_handler: method in
@@ -154,7 +153,7 @@ def parse_file(descriptor_file, descriptor_type = None, path = None, validate =
   if not metrics_header_match:
     descriptor_file.seek(initial_position)
 
-  filename = '<undefined>' if path is None else os.path.basename(path)
+  filename = '<undefined>' if descriptor_file.name is None else os.path.basename(descriptor_file.name)
   file_parser = None
 
   if descriptor_type is not None:
@@ -184,8 +183,8 @@ def parse_file(descriptor_file, descriptor_type = None, path = None, validate =
 
   if file_parser:
     for desc in file_parser(descriptor_file):
-      if path is not None:
-        desc._set_path(path)
+      if descriptor_file.name is not None:
+        desc._set_path(os.path.abspath(descriptor_file.name))
 
       yield desc
 
@@ -288,6 +287,7 @@ class _UnicodeReader(object):
 
   def __init__(self, wrapped_file):
     self.wrapped_file = wrapped_file
+    self.name = getattr(wrapped_file, 'name', None)
 
   def close(self):
     return self.wrapped_file.close()
diff --git a/stem/descriptor/reader.py b/stem/descriptor/reader.py
index 21ab049..92adeaf 100644
--- a/stem/descriptor/reader.py
+++ b/stem/descriptor/reader.py
@@ -517,7 +517,7 @@ class DescriptorReader(object):
       self._notify_read_listeners(target)
 
       with open(target, 'rb') as target_file:
-        for desc in stem.descriptor.parse_file(target_file, validate = self._validate, path = target, document_handler = self._document_handler):
+        for desc in stem.descriptor.parse_file(target_file, validate = self._validate, document_handler = self._document_handler):
           if self._is_stopped.isSet():
             return
 
@@ -545,7 +545,7 @@ class DescriptorReader(object):
         if tar_entry.isfile():
           entry = tar_file.extractfile(tar_entry)
 
-          for desc in stem.descriptor.parse_file(entry, validate = self._validate, path = target, document_handler = self._document_handler):
+          for desc in stem.descriptor.parse_file(entry, validate = self._validate, document_handler = self._document_handler):
             if self._is_stopped.isSet():
               return
 
diff --git a/test/integ/descriptor/networkstatus.py b/test/integ/descriptor/networkstatus.py
index 3f758b4..9f68eba 100644
--- a/test/integ/descriptor/networkstatus.py
+++ b/test/integ/descriptor/networkstatus.py
@@ -43,7 +43,7 @@ class TestNetworkStatus(unittest.TestCase):
 
     count = 0
     with open(consensus_path, 'rb') as descriptor_file:
-      for router in stem.descriptor.parse_file(descriptor_file, "network-status-consensus-3 1.0", path = consensus_path):
+      for router in stem.descriptor.parse_file(descriptor_file, "network-status-consensus-3 1.0"):
         count += 1
 
         # We should have constant memory usage. Fail if we're using over 200 MB.
@@ -88,7 +88,7 @@ class TestNetworkStatus(unittest.TestCase):
 
     count = 0
     with open(consensus_path, 'rb') as descriptor_file:
-      for router in stem.descriptor.parse_file(descriptor_file, "network-status-microdesc-consensus-3 1.0", path = consensus_path):
+      for router in stem.descriptor.parse_file(descriptor_file, "network-status-microdesc-consensus-3 1.0"):
         count += 1
 
         if resource.getrusage(resource.RUSAGE_SELF).ru_maxrss > 200000:
@@ -118,9 +118,9 @@ class TestNetworkStatus(unittest.TestCase):
     for specify_type in (True, False):
       with open(consensus_path, 'rb') as descriptor_file:
         if specify_type:
-          descriptors = stem.descriptor.parse_file(descriptor_file, "network-status-consensus-3 1.0", path = consensus_path)
+          descriptors = stem.descriptor.parse_file(descriptor_file, "network-status-consensus-3 1.0")
         else:
-          descriptors = stem.descriptor.parse_file(descriptor_file, path = consensus_path)
+          descriptors = stem.descriptor.parse_file(descriptor_file)
 
         router = next(descriptors)
         self.assertEquals("sumkledi", router.nickname)
@@ -139,7 +139,7 @@ class TestNetworkStatus(unittest.TestCase):
     consensus_path = get_resource("bridge_network_status")
 
     with open(consensus_path, 'rb') as descriptor_file:
-      router = next(stem.descriptor.parse_file(descriptor_file, path = consensus_path))
+      router = next(stem.descriptor.parse_file(descriptor_file))
       self.assertEquals("Unnamed", router.nickname)
       self.assertEquals("0014A2055278DB3EB0E59EA701741416AF185558", router.fingerprint)
       self.assertEquals("FI74aFuNJZZQrgln0f+OaocMd0M", router.digest)
@@ -185,7 +185,7 @@ GM9hAsAMRX9Ogqhq5UjDNqEsvDKuyVeyh7unSZEOip9Zr6K/+7VsVPNb8vfBRBjo
     cert_path = get_resource("metrics_cert")
 
     with open(cert_path) as cert_file:
-      cert = next(stem.descriptor.parse_file(cert_file, path = cert_path))
+      cert = next(stem.descriptor.parse_file(cert_file))
       self.assertEquals(3, cert.version)
       self.assertEquals(None, cert.address)
       self.assertEquals(None, cert.dir_port)
@@ -374,7 +374,7 @@ TpQQk3nNQF8z6UIvdlvP+DnJV4izWVkQEZgUZgIVM0E=
     vote_path = get_resource("metrics_vote")
 
     with open(vote_path, 'rb') as descriptor_file:
-      descriptors = stem.descriptor.parse_file(descriptor_file, path = vote_path)
+      descriptors = stem.descriptor.parse_file(descriptor_file)
 
       router = next(descriptors)
       self.assertEquals("sumkledi", router.nickname)





More information about the tor-commits mailing list