commit 60868f5f161dc0db59f4c19019b75c29f417b37c Author: Damian Johnson atagar@torproject.org Date: Sun Oct 7 18:53:18 2012 -0700
Dropping unused descriptor helpers
Removing the helper functions for the prior parsers of network status documents. --- stem/descriptor/__init__.py | 73 -------------------------------------- stem/descriptor/networkstatus.py | 13 +++---- 2 files changed, 5 insertions(+), 81 deletions(-)
diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py index ef3d558..28aeed1 100644 --- a/stem/descriptor/__init__.py +++ b/stem/descriptor/__init__.py @@ -169,72 +169,6 @@ class Descriptor(object): def __str__(self): return self._raw_contents
-def _peek_line(descriptor_file): - """ - Returns the line at the current offset of descriptor_file. - - :param file descriptor_file: file with the descriptor content - - :returns: line at the current offset of descriptor_file - """ - - last_position = descriptor_file.tell() - line = descriptor_file.readline() - descriptor_file.seek(last_position) - - return line - -def _peek_keyword(descriptor_file): - """ - Returns the keyword at the current offset of descriptor_file. Respects the - "opt" keyword and returns the next keyword instead. - - :param file descriptor_file: file with the descriptor content - - :returns: keyword at the current offset of descriptor_file - """ - - line = _peek_line(descriptor_file) - - if line.startswith("opt "): - line = line[4:] - if not line: return None - - return line.split(" ", 1)[0].rstrip("\n") - -def _read_keyword_line(keyword, descriptor_file, validate = True, optional = False): - """ - Returns the rest of the line if the first keyword matches the given keyword. If - it doesn't, a ValueError is raised if optional and validate are True, if - not, None is returned. - - Respects the opt keyword and returns the next keyword if the first is "opt". - - :param str keyword: keyword the line must begin with - :param bool descriptor_file: file/file-like object containing descriptor data - :param bool validate: validation is enabled - :param bool optional: if the current line must begin with the given keyword - - :returns: the text after the keyword if the keyword matches the one provided, otherwise returns None or raises an exception - - :raises: ValueError if a non-optional keyword doesn't match when validation is enabled - """ - - line = _peek_line(descriptor_file) - if not line: - if not optional and validate: - raise ValueError("Unexpected end of document") - return None - - if line.startswith("opt "): - line = line[4:] - if re.match("^" + re.escape(keyword) + "($| )", line): - descriptor_file.readline() - return line[len(keyword):].strip() - elif not optional and validate: - raise ValueError("Error parsing network status document: Expected %s, received: %s" % (keyword, line)) - else: return None - def _read_until_keywords(keywords, descriptor_file, inclusive = False, ignore_first = False, skip = False, end_position = None): """ Reads from the descriptor file until we get to one of the given keywords or reach the @@ -399,10 +333,3 @@ def _get_descriptor_components(raw_contents, validate, extra_keywords = ()):
return entries, first_keyword, last_keyword, extra_entries
-def _strptime(string, validate = True, optional = False): - try: - return datetime.datetime.strptime(string, "%Y-%m-%d %H:%M:%S") - except ValueError, exc: - if validate or not optional: raise exc - else: return None - diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py index d9aab4f..dea16f4 100644 --- a/stem/descriptor/networkstatus.py +++ b/stem/descriptor/networkstatus.py @@ -52,9 +52,6 @@ import stem.version import stem.exit_policy import stem.util.tor_tools
-from stem.descriptor import _read_until_keywords, _peek_keyword, _strptime -from stem.descriptor import _read_keyword_line, _get_pseudo_pgp_block, _peek_line - # Network status document are either a 'vote' or 'consensus', with different # mandatory fields for each. Both though require that their fields appear in a # specific order. This is an ordered listing of the following... @@ -146,10 +143,10 @@ def parse_file(document_file, validate = True, is_microdescriptor = False):
# getting the document without the routers section
- header = _read_until_keywords((ROUTERS_START, FOOTER_START), document_file) + header = stem.descriptor._read_until_keywords((ROUTERS_START, FOOTER_START), document_file)
routers_start = document_file.tell() - _read_until_keywords(FOOTER_START, document_file, skip = True) + stem.descriptor._read_until_keywords(FOOTER_START, document_file, skip = True) routers_end = document_file.tell()
footer = document_file.readlines() @@ -203,14 +200,14 @@ def _get_entries(document_file, validate, entry_class, entry_keyword, start_posi
if end_position is None: if section_end_keywords: - _read_until_keywords(section_end_keywords, document_file, skip = True) + stem.descriptor._read_until_keywords(section_end_keywords, document_file, skip = True) end_position = document_file.tell() else: raise ValueError("Either a end_position or section_end_keywords must be provided")
document_file.seek(start_position) while document_file.tell() < end_position: - desc_content = "".join(_read_until_keywords(entry_keyword, document_file, ignore_first = True, end_position = end_position)) + desc_content = "".join(stem.descriptor._read_until_keywords(entry_keyword, document_file, ignore_first = True, end_position = end_position)) yield entry_class(desc_content, validate, *extra_args)
class NetworkStatusDocument(stem.descriptor.Descriptor): @@ -347,7 +344,7 @@ class _DocumentHeader(object):
self._unrecognized_lines = []
- content = "".join(_read_until_keywords((AUTH_START, ROUTERS_START, FOOTER_START), document_file)) + content = "".join(stem.descriptor._read_until_keywords((AUTH_START, ROUTERS_START, FOOTER_START), document_file)) entries = stem.descriptor._get_descriptor_components(content, validate)[0] self._parse(entries, validate)