commit 2a7a13b1140e87f2ea44a93c3e207df98320faa1 Author: Damian Johnson atagar@torproject.org Date: Mon Jan 8 09:01:36 2018 -0800
Add FallbackDirectory.from_str() function --- stem/descriptor/remote.py | 56 ++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 20 deletions(-)
diff --git a/stem/descriptor/remote.py b/stem/descriptor/remote.py index 1f002e27..34402499 100644 --- a/stem/descriptor/remote.py +++ b/stem/descriptor/remote.py @@ -1081,31 +1081,47 @@ class FallbackDirectory(Directory): results = {}
while lines: - # Example of an entry... - # - # "5.9.110.236:9030 orport=9001 id=0756B7CD4DFC8182BE23143FAC0642F515182CEB" - # " ipv6=[2a01:4f8:162:51e2::2]:9001" - # /* nickname=rueckgrat */ - # /* extrainfo=1 */ - section = FallbackDirectory._pop_section(lines)
- if not section: - continue + if section: + fallback = FallbackDirectory.from_str('\n'.join(section)) + results[fallback.fingerprint] = fallback
- address, or_port, dir_port, fingerprint = FallbackDirectory._parse_addr(section) + return results
- results[fingerprint] = FallbackDirectory( - address = address, - or_port = or_port, - dir_port = dir_port, - fingerprint = fingerprint, - nickname = FallbackDirectory._parse_nickname(section, fingerprint), - has_extrainfo = FallbackDirectory._parse_has_extrainfo(section), - orport_v6 = FallbackDirectory._parse_ipv6(section, fingerprint), - ) + @staticmethod + def from_str(content): + """ + Parses a fallback from its textual representation. For example...
- return results + :: + + "5.9.110.236:9030 orport=9001 id=0756B7CD4DFC8182BE23143FAC0642F515182CEB" + " ipv6=[2a01:4f8:162:51e2::2]:9001" + /* nickname=rueckgrat */ + /* extrainfo=1 */ + + .. versionadded:: 1.7.0 + + :param str content: text to parse + + :returns: :class:`~stem.descriptor.remote.FallbackDirectory` in the text + + :raises: **IOError** if content is malformed + """ + + lines = content.splitlines() + address, or_port, dir_port, fingerprint = FallbackDirectory._parse_addr(lines) + + return FallbackDirectory( + address = address, + or_port = or_port, + dir_port = dir_port, + fingerprint = fingerprint, + nickname = FallbackDirectory._parse_nickname(lines, fingerprint), + has_extrainfo = FallbackDirectory._parse_has_extrainfo(lines), + orport_v6 = FallbackDirectory._parse_ipv6(lines, fingerprint), + )
@staticmethod def _pop_section(lines):
tor-commits@lists.torproject.org