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

[tor-commits] [stem/master] Removing _skip_until_keywords()



commit e1ce38ebd4e49faff993e212e6959f9a46e93e50
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sat Aug 18 19:08:15 2012 -0700

    Removing _skip_until_keywords()
    
    The _skip_until_keywords() is almost entirely a duplicate of
    _read_until_keywords(), its only difference being that it doesn't return the
    content. Adding an argument to _read_until_keywords() to do the same.
---
 stem/descriptor/__init__.py      |   53 ++++++++-----------------------------
 stem/descriptor/networkstatus.py |    4 +-
 2 files changed, 14 insertions(+), 43 deletions(-)

diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index 814fc49..151f13e 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -253,7 +253,7 @@ def _read_keyword_line_str(keyword, lines, validate = True, optional = False):
     raise ValueError("Error parsing network status document: Expected %s, received: %s" % (keyword, lines[0]))
   else: return None
 
-def _read_until_keywords(keywords, descriptor_file, inclusive = False, ignore_first = False):
+def _read_until_keywords(keywords, descriptor_file, inclusive = False, ignore_first = False, skip = False):
   """
   Reads from the descriptor file until we get to one of the given keywords or reach the
   end of the file.
@@ -262,16 +262,19 @@ def _read_until_keywords(keywords, descriptor_file, inclusive = False, ignore_fi
   :param file descriptor_file: file with the descriptor content
   :param bool inclusive: includes the line with the keyword if True
   :param bool ignore_first: doesn't check if the first line read has one of the given keywords
+  :param bool skip: skips buffering content, returning None
   
   :returns: list with the lines until we find one of the keywords
   """
   
-  content = []
+  content = None if skip else []
   if type(keywords) == str: keywords = (keywords,)
   
   if ignore_first:
-    content.append(descriptor_file.readline())
-    if content == [None]: return []
+    first_line = descriptor_file.readline()
+    
+    if content != None and first_line != None:
+      content.append(first_line)
   
   while True:
     last_position = descriptor_file.tell()
@@ -287,49 +290,17 @@ def _read_until_keywords(keywords, descriptor_file, inclusive = False, ignore_fi
       line_keyword = line_match.groups()[0]
     
     if line_keyword in keywords:
-      if inclusive: content.append(line)
-      else: descriptor_file.seek(last_position)
+      if not inclusive:
+        descriptor_file.seek(last_position)
+      elif content != None:
+        content.append(line)
       
       break
-    else:
+    elif content != None:
       content.append(line)
   
   return content
 
-def _skip_until_keywords(keywords, descriptor_file, inclusive = False):
-  """
-  Reads and discards lines of data from the descriptor file until we get to one
-  of the given keywords or reach the end of the file.
-  
-  :param str,list keywords: keyword(s) we want to skip until
-  :param file descriptor_file: file with the descriptor content
-  :param bool inclusive: includes the line with the keyword if True
-  
-  :returns: descriptor_file with the new offset
-  """
-  
-  if type(keywords) == str: keywords = (keywords,)
-  
-  while True:
-    last_position = descriptor_file.tell()
-    line = descriptor_file.readline()
-    if not line: break # EOF
-    
-    line_match = KEYWORD_LINE.match(line)
-    
-    if not line_match:
-      # no spaces or tabs in the line
-      line_keyword = line.strip()
-    else:
-      line_keyword = line_match.groups()[0]
-    
-    if line_keyword in keywords:
-      if not inclusive: descriptor_file.seek(last_position)
-      
-      break
-  
-  return descriptor_file
-
 def _get_pseudo_pgp_block(remaining_contents):
   """
   Checks if given contents begins with a pseudo-Open-PGP-style block and, if
diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py
index f9d89a8..3b592ec 100644
--- a/stem/descriptor/networkstatus.py
+++ b/stem/descriptor/networkstatus.py
@@ -53,7 +53,7 @@ import stem.version
 import stem.exit_policy
 import stem.util.enum
 
-from stem.descriptor import _read_until_keywords, _skip_until_keywords, _peek_keyword, _strptime
+from stem.descriptor import _read_until_keywords, _peek_keyword, _strptime
 from stem.descriptor import _read_keyword_line, _read_keyword_line_str, _get_pseudo_pgp_block, _peek_line
 
 _bandwidth_weights_regex = re.compile(" ".join(["W%s=\d+" % weight for weight in ["bd",
@@ -101,7 +101,7 @@ def parse_file(document_file, validate = True, flavour = Flavour.NONE):
   # store offset
   r_offset = document_file.tell()
   # skip until end of router descriptors
-  _skip_until_keywords(["bandwidth-weights", "directory-footer", "directory-signature"], document_file)
+  _read_until_keywords(["bandwidth-weights", "directory-footer", "directory-signature"], document_file, skip = True)
   # parse until end
   document_data = document_data + document_file.read()
   



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