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

[tor-commits] [stem/master] Stub for identifying descriptor types



commit af1cdfcedf961b506b63c923fdd9b5a6a3edd1e5
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sat Mar 17 15:54:15 2012 -0700

    Stub for identifying descriptor types
    
    The various tor descriptor types were not made to be readily identifyable, so
    we'll probably need to guess their type based on their first line. Ideally that
    first line would explicitly say the type and version, but oh well...
---
 stem/descriptor/descriptor.py |   27 ++++++++++++++++++++++++---
 1 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/stem/descriptor/descriptor.py b/stem/descriptor/descriptor.py
index 90d3bed..a89e5e5 100644
--- a/stem/descriptor/descriptor.py
+++ b/stem/descriptor/descriptor.py
@@ -2,6 +2,8 @@
 Common functionality for descriptors.
 """
 
+import os
+
 def parse_descriptors(path, descriptor_file):
   """
   Provides an iterator for the descriptors within a given file.
@@ -18,6 +20,22 @@ def parse_descriptors(path, descriptor_file):
     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":
+    pass # server descriptors from tor's data directory
+  
+  first_line = descriptor_file.readline()
+  descriptor_file.seek(0)
+  
+  if first_line.startswith("router "):
+    pass # server descriptor
+  
   # TODO: implement actual descriptor type recognition and parsing
   # TODO: add integ test for non-descriptor text content
   yield Descriptor(path, descriptor_file.read())
@@ -27,9 +45,9 @@ class Descriptor:
   Common parent for all types of descriptors.
   """
   
-  def __init__(self, path, raw_contents):
-    self._path = path
-    self._raw_contents = raw_contents
+  def __init__(self, contents):
+    self._path = None
+    self._raw_contents = contents
   
   def get_path(self):
     """
@@ -53,6 +71,9 @@ class Descriptor:
     
     return []
   
+  def _set_path(self, path):
+    self._path = path
+  
   def __str__(self):
     return self._raw_contents
 



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