[tor-commits] [stem/master] Simplifying _get_descriptor_components() usage
atagar at torproject.org
atagar at torproject.org
Sat Oct 13 18:35:45 UTC 2012
commit 27356d41908c70fdec23d26d4f9933f8fefb71f9
Author: Damian Johnson <atagar at torproject.org>
Date: Mon Oct 8 08:50:13 2012 -0700
Simplifying _get_descriptor_components() usage
The _get_descriptor_components() provided the first and last keywords since
those are often needed for validation. However, this is pointless now that
we're using an ordered dictionary (we can simply check the key listing).
---
stem/descriptor/__init__.py | 19 +++++++------------
stem/descriptor/extrainfo_descriptor.py | 7 +++----
stem/descriptor/networkstatus.py | 14 +++++++-------
stem/descriptor/router_status_entry.py | 9 ++++-----
stem/descriptor/server_descriptor.py | 12 +++++-------
5 files changed, 26 insertions(+), 35 deletions(-)
diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index 28aeed1..f0ab82d 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -275,17 +275,12 @@ def _get_descriptor_components(raw_contents, validate, extra_keywords = ()):
:param list extra_keywords: entity keywords to put into a separate listing with ordering intact
:returns:
- tuple with the following attributes...
-
- * **entries (collections.OrderedDict)** - keyword => (value, pgp key) entries
- * **first_keyword (str)** - keyword of the first line
- * **last_keyword (str)** - keyword of the last line
- * **extra_entries (list)** - lines containing entries matching extra_keywords
+ collections.OrderedDict with the 'keyword => (value, pgp key) entries'
+ mappings. If a extra_keywords was provided then this instead provides a two
+ value tuple, the second being a list of those entries.
"""
entries = collections.OrderedDict()
- first_keyword = None
- last_keyword = None
extra_entries = [] # entries with a keyword in extra_keywords
remaining_lines = raw_contents.split("\n")
@@ -317,9 +312,6 @@ def _get_descriptor_components(raw_contents, validate, extra_keywords = ()):
keyword, value = line_match.groups()
if value is None: value = ''
- if not first_keyword: first_keyword = keyword
- last_keyword = keyword
-
try:
block_contents = _get_pseudo_pgp_block(remaining_lines)
except ValueError, exc:
@@ -331,5 +323,8 @@ def _get_descriptor_components(raw_contents, validate, extra_keywords = ()):
else:
entries.setdefault(keyword, []).append((value, block_contents))
- return entries, first_keyword, last_keyword, extra_entries
+ if extra_keywords:
+ return entries, extra_entries
+ else:
+ return entries
diff --git a/stem/descriptor/extrainfo_descriptor.py b/stem/descriptor/extrainfo_descriptor.py
index 8d181ae..d66ea87 100644
--- a/stem/descriptor/extrainfo_descriptor.py
+++ b/stem/descriptor/extrainfo_descriptor.py
@@ -359,8 +359,7 @@ class ExtraInfoDescriptor(stem.descriptor.Descriptor):
self._unrecognized_lines = []
- entries, first_keyword, last_keyword, _ = \
- stem.descriptor._get_descriptor_components(raw_contents, validate, ())
+ entries = stem.descriptor._get_descriptor_components(raw_contents, validate)
if validate:
for keyword in self._required_fields():
@@ -372,11 +371,11 @@ class ExtraInfoDescriptor(stem.descriptor.Descriptor):
raise ValueError("The '%s' entry can only appear once in an extra-info descriptor" % keyword)
expected_first_keyword = self._first_keyword()
- if expected_first_keyword and not first_keyword == expected_first_keyword:
+ if expected_first_keyword and expected_first_keyword != entries.keys()[0]:
raise ValueError("Extra-info descriptor must start with a '%s' entry" % expected_first_keyword)
expected_last_keyword = self._last_keyword()
- if expected_last_keyword and not last_keyword == expected_last_keyword:
+ if expected_last_keyword and expected_last_keyword != entries.keys()[-1]:
raise ValueError("Descriptor must end with a '%s' entry" % expected_last_keyword)
self._parse(entries, validate)
diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py
index a2c13a2..91c50e6 100644
--- a/stem/descriptor/networkstatus.py
+++ b/stem/descriptor/networkstatus.py
@@ -345,7 +345,7 @@ class _DocumentHeader(object):
self._unrecognized_lines = []
content = "".join(stem.descriptor._read_until_keywords((AUTH_START, ROUTERS_START, FOOTER_START), document_file))
- entries = stem.descriptor._get_descriptor_components(content, validate)[0]
+ entries = stem.descriptor._get_descriptor_components(content, validate)
self._parse(entries, validate)
# doing this validation afterward so we know our 'is_consensus' and
@@ -538,7 +538,7 @@ class _DocumentFooter(object):
elif not content and not header.meets_consensus_method(9):
return # footer is optional and there's nothing to parse
- entries = stem.descriptor._get_descriptor_components(content, validate)[0]
+ entries = stem.descriptor._get_descriptor_components(content, validate)
self._parse(entries, validate, header)
if validate:
@@ -748,9 +748,9 @@ class DirectoryAuthority(stem.descriptor.Descriptor):
else:
key_cert_content = None
- entries, first_keyword, _, _ = stem.descriptor._get_descriptor_components(content, validate)
+ entries = stem.descriptor._get_descriptor_components(content, validate)
- if validate and first_keyword != 'dir-source':
+ if validate and 'dir-source' != entries.keys()[0]:
raise ValueError("Authority entries are expected to start with a 'dir-source' line:\n%s" % (content))
# check that we have mandatory fields
@@ -905,12 +905,12 @@ class KeyCertificate(stem.descriptor.Descriptor):
:raises: ValueError if a validity check fails
"""
- entries, first_keyword, last_keyword, _ = stem.descriptor._get_descriptor_components(content, validate)
+ entries = stem.descriptor._get_descriptor_components(content, validate)
if validate:
- if first_keyword != 'dir-key-certificate-version':
+ if 'dir-key-certificate-version' != entries.keys()[0]:
raise ValueError("Key certificates must start with a 'dir-key-certificate-version' line:\n%s" % (content))
- elif last_keyword != 'dir-key-certification':
+ elif 'dir-key-certification' != entries.keys()[-1]:
raise ValueError("Key certificates must end with a 'dir-key-certification' line:\n%s" % (content))
# check that we have mandatory fields and that our known fields only
diff --git a/stem/descriptor/router_status_entry.py b/stem/descriptor/router_status_entry.py
index f663f07..d94b2e8 100644
--- a/stem/descriptor/router_status_entry.py
+++ b/stem/descriptor/router_status_entry.py
@@ -71,8 +71,8 @@ class RouterStatusEntry(stem.descriptor.Descriptor):
self._unrecognized_lines = []
- entries, first_keyword, _, _ = stem.descriptor._get_descriptor_components(content, validate)
- if validate: self._check_constraints(entries, first_keyword)
+ entries = stem.descriptor._get_descriptor_components(content, validate)
+ if validate: self._check_constraints(entries)
self._parse(entries, validate)
def _parse(self, entries, validate):
@@ -95,13 +95,12 @@ class RouterStatusEntry(stem.descriptor.Descriptor):
else:
self._unrecognized_lines.append("%s %s" % (keyword, value))
- def _check_constraints(self, entries, first_keyword):
+ def _check_constraints(self, entries):
"""
Does a basic check that the entries conform to this descriptor type's
constraints.
:param dict entries: keyword => (value, pgp key) entries
- :param str first_keyword: keyword of the first line
:raises: ValueError if an issue arises in validation
"""
@@ -114,7 +113,7 @@ class RouterStatusEntry(stem.descriptor.Descriptor):
if keyword in entries and len(entries[keyword]) > 1:
raise ValueError("%s can only have a single '%s' line, got %i:\n%s" % (self._name(True), keyword, len(entries[keyword]), str(self)))
- if first_keyword != 'r':
+ if 'r' != entries.keys()[0]:
raise ValueError("%s are expected to start with a 'r' line:\n%s" % (self._name(True), str(self)))
def _name(self, is_plural = False):
diff --git a/stem/descriptor/server_descriptor.py b/stem/descriptor/server_descriptor.py
index 0ded3b1..5e5e04a 100644
--- a/stem/descriptor/server_descriptor.py
+++ b/stem/descriptor/server_descriptor.py
@@ -240,12 +240,12 @@ class ServerDescriptor(stem.descriptor.Descriptor):
# influences the resulting exit policy, but for everything else the order
# does not matter so breaking it into key / value pairs.
- entries, first_keyword, last_keyword, policy = \
+ entries, policy = \
stem.descriptor._get_descriptor_components(raw_contents, validate, ("accept", "reject"))
self.exit_policy = stem.exit_policy.ExitPolicy(*policy)
self._parse(entries, validate)
- if validate: self._check_constraints(entries, first_keyword, last_keyword)
+ if validate: self._check_constraints(entries)
def digest(self):
"""
@@ -495,14 +495,12 @@ class ServerDescriptor(stem.descriptor.Descriptor):
if self.uptime < 0 and self.tor_version >= stem.version.Version("0.1.2.7"):
raise ValueError("Descriptor for version '%s' had a negative uptime value: %i" % (self.tor_version, self.uptime))
- def _check_constraints(self, entries, first_keyword, last_keyword):
+ def _check_constraints(self, entries):
"""
Does a basic check that the entries conform to this descriptor type's
constraints.
:param dict entries: keyword => (value, pgp key) entries
- :param str first_keyword: keyword of the first line
- :param str last_keyword: keyword of the last line
:raises: ValueError if an issue arises in validation
"""
@@ -516,11 +514,11 @@ class ServerDescriptor(stem.descriptor.Descriptor):
raise ValueError("The '%s' entry can only appear once in a descriptor" % keyword)
expected_first_keyword = self._first_keyword()
- if expected_first_keyword and first_keyword != expected_first_keyword:
+ if expected_first_keyword and expected_first_keyword != entries.keys()[0]:
raise ValueError("Descriptor must start with a '%s' entry" % expected_first_keyword)
expected_last_keyword = self._last_keyword()
- if expected_last_keyword and last_keyword != expected_last_keyword:
+ if expected_last_keyword and expected_last_keyword != entries.keys()[-1]:
raise ValueError("Descriptor must end with a '%s' entry" % expected_last_keyword)
if not self.exit_policy:
More information about the tor-commits
mailing list