[tor-commits] [stem/master] Merging descriptor.py with module init

atagar at torproject.org atagar at torproject.org
Mon Mar 26 00:10:01 UTC 2012


commit 55db7157d30e62af2fa610f1bb962e8fc8a29817
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Mar 23 09:39:50 2012 -0700

    Merging descriptor.py with module init
    
    The syntax for using the descriptor module was stupid ("import
    stem.descriptor.descriptor"). Using the json module as an example for using
    __init__.py for base functions. Common ones will live in 'stem.descritor' but
    the contents, like 'stem.descriptor.reader', are also accessable.
    
    This is similar to how the os module works, though in that case they're using a
    lot more python magic to also support multiple operating systems.
---
 stem/descriptor/__init__.py          |   86 ++++++++++++++++++++++++++++++++
 stem/descriptor/descriptor.py        |   89 ----------------------------------
 stem/descriptor/server_descriptor.py |    6 +-
 3 files changed, 89 insertions(+), 92 deletions(-)

diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index 0a5ba81..96f1bf5 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -4,3 +4,89 @@ Utilities for parsing and processing descriptor data.
 
 __all__ = ["descriptor", "reader", "server_descriptor"]
 
+import os
+
+import stem.descriptor.server_descriptor
+
+def parse_descriptors(path, descriptor_file):
+  """
+  Provides an iterator for the descriptors within a given file.
+  
+  Arguments:
+    path (str)             - absolute path to the file's location on disk
+    descriptor_file (file) - opened file with the descriptor contents
+  
+  Returns:
+    iterator that parses the file's contents into descriptors
+  
+  Raises:
+    TypeError if we can't match the contents of the file to a descriptor type
+    IOError if unable to read from the descriptor_file
+  """
+  
+  # The tor descriptor specifications do not provide a reliable method for
+  # identifying a descriptor file's type and version so we need to guess
+  # based on its filename for resources from the data directory and contents
+  # for files provided by metrics.
+  
+  filename = os.path.basename(path)
+  
+  if filename == "cached-descriptors":
+    # server descriptors from tor's data directory
+    while descriptor_file:
+      yield stem.descriptor.server_descriptor.parse_server_descriptors_v2(path, descriptor_file)
+    
+    return
+  
+  first_line = descriptor_file.readline()
+  descriptor_file.seek(0)
+  
+  if first_line.startswith("router "):
+    # server descriptor
+    while descriptor_file:
+      yield stem.descriptor.server_descriptor.parse_server_descriptors_v2(path, descriptor_file)
+    
+    return
+  
+  # TODO: implement actual descriptor type recognition and parsing
+  # TODO: add integ test for non-descriptor text content
+  yield Descriptor(path, descriptor_file.read())
+
+class Descriptor:
+  """
+  Common parent for all types of descriptors.
+  """
+  
+  def __init__(self, contents):
+    self._path = None
+    self._raw_contents = contents
+  
+  def get_path(self):
+    """
+    Provides the absolute path that we loaded this descriptor from.
+    
+    Returns:
+      str with the absolute path of the descriptor source
+    """
+    
+    return self._path
+  
+  def get_unrecognized_lines(self):
+    """
+    Provides a list of lines that were either ignored or had data that we did
+    not know how to process. This is most common due to new descriptor fields
+    that this library does not yet know how to process. Patches welcome!
+    
+    Returns:
+      list of lines of unrecognized content
+    """
+    
+    return []
+  
+  def _set_path(self, path):
+    self._path = path
+  
+  def __str__(self):
+    return self._raw_contents
+
+
diff --git a/stem/descriptor/descriptor.py b/stem/descriptor/descriptor.py
deleted file mode 100644
index f535afc..0000000
--- a/stem/descriptor/descriptor.py
+++ /dev/null
@@ -1,89 +0,0 @@
-"""
-Common functionality for descriptors.
-"""
-
-import os
-
-import stem.descriptor.server_descriptor
-
-def parse_descriptors(path, descriptor_file):
-  """
-  Provides an iterator for the descriptors within a given file.
-  
-  Arguments:
-    path (str)             - absolute path to the file's location on disk
-    descriptor_file (file) - opened file with the descriptor contents
-  
-  Returns:
-    iterator that parses the file's contents into descriptors
-  
-  Raises:
-    TypeError if we can't match the contents of the file to a descriptor type
-    IOError if unable to read from the descriptor_file
-  """
-  
-  # The tor descriptor specifications do not provide a reliable method for
-  # identifying a descriptor file's type and version so we need to guess
-  # based on its filename for resources from the data directory and contents
-  # for files provided by metrics.
-  
-  filename = os.path.basename(path)
-  
-  if filename == "cached-descriptors":
-    # server descriptors from tor's data directory
-    while descriptor_file:
-      yield stem.descriptor.server_descriptor.parse_server_descriptors_v2(path, descriptor_file)
-    
-    return
-  
-  first_line = descriptor_file.readline()
-  descriptor_file.seek(0)
-  
-  if first_line.startswith("router "):
-    # server descriptor
-    while descriptor_file:
-      yield stem.descriptor.server_descriptor.parse_server_descriptors_v2(path, descriptor_file)
-    
-    return
-  
-  # TODO: implement actual descriptor type recognition and parsing
-  # TODO: add integ test for non-descriptor text content
-  yield Descriptor(path, descriptor_file.read())
-
-class Descriptor:
-  """
-  Common parent for all types of descriptors.
-  """
-  
-  def __init__(self, contents):
-    self._path = None
-    self._raw_contents = contents
-  
-  def get_path(self):
-    """
-    Provides the absolute path that we loaded this descriptor from.
-    
-    Returns:
-      str with the absolute path of the descriptor source
-    """
-    
-    return self._path
-  
-  def get_unrecognized_lines(self):
-    """
-    Provides a list of lines that were either ignored or had data that we did
-    not know how to process. This is most common due to new descriptor fields
-    that this library does not yet know how to process. Patches welcome!
-    
-    Returns:
-      list of lines of unrecognized content
-    """
-    
-    return []
-  
-  def _set_path(self, path):
-    self._path = path
-  
-  def __str__(self):
-    return self._raw_contents
-
diff --git a/stem/descriptor/server_descriptor.py b/stem/descriptor/server_descriptor.py
index 40124f5..bd68f91 100644
--- a/stem/descriptor/server_descriptor.py
+++ b/stem/descriptor/server_descriptor.py
@@ -14,7 +14,7 @@ import datetime
 import stem.version
 import stem.util.connection
 import stem.util.tor_tools
-import stem.descriptor.descriptor
+import stem.descriptor
 
 ENTRY_START = "router"
 ENTRY_END   = "router-signature"
@@ -164,7 +164,7 @@ def _get_psudo_pgp_block(remaining_contents):
   else:
     return (None, None)
 
-class ServerDescriptorV2(stem.descriptor.descriptor.Descriptor):
+class ServerDescriptorV2(stem.descriptor.Descriptor):
   """
   Version 2 server descriptor, as specified in...
   https://gitweb.torproject.org/torspec.git/blob/HEAD:/dir-spec-v2.txt
@@ -230,7 +230,7 @@ class ServerDescriptorV2(stem.descriptor.descriptor.Descriptor):
       ValueError if the contents is malformed and validate is True
     """
     
-    stem.descriptor.descriptor.Descriptor.__init__(self, contents)
+    stem.descriptor.Descriptor.__init__(self, contents)
     
     self._annotation_lines = annotations
     self._annotation_dict = {}





More information about the tor-commits mailing list