commit e0ad461440361736b8ce340786433fe6dfaf44f2 Author: Damian Johnson atagar@torproject.org Date: Sat May 12 15:39:15 2012 -0700
Parsing dirreq-v2-share and dirreq-v3-share lines
Couple more extrainfo descriptor fields. Ye gods there a lot. --- stem/descriptor/extrainfo_descriptor.py | 21 ++++++++++++++++ test/unit/descriptor/extrainfo_descriptor.py | 33 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 0 deletions(-)
diff --git a/stem/descriptor/extrainfo_descriptor.py b/stem/descriptor/extrainfo_descriptor.py index 61e00dd..58e2220 100644 --- a/stem/descriptor/extrainfo_descriptor.py +++ b/stem/descriptor/extrainfo_descriptor.py @@ -161,6 +161,8 @@ class ExtraInfoDescriptor(stem.descriptor.Descriptor): dir_v3_ips (dict) - mapping of locales to rounded count of requester ips dir_v2_reqs (dict) - mapping of locales to rounded count of requests dir_v3_reqs (dict) - mapping of locales to rounded count of requests + dir_v2_share (float) - percent of total directory traffic it expects to serve + dir_v3_share (float) - percent of total directory traffic it expects to serve
Bytes read/written for directory mirroring dir_read_history_end (datetime) - end of the sampling interval @@ -223,6 +225,8 @@ class ExtraInfoDescriptor(stem.descriptor.Descriptor): self.dir_v3_ips = None self.dir_v2_reqs = None self.dir_v3_reqs = None + self.dir_v2_share = None + self.dir_v3_share = None
self.dir_read_history_end = None self.dir_read_history_interval = None @@ -305,6 +309,23 @@ class ExtraInfoDescriptor(stem.descriptor.Descriptor): raise ValueError("Geoip digest line had an invalid sha1 digest: %s" % line)
self.geoip_db_digest = value + elif keyword in ("dirreq-v2-share", "dirreq-v3-share"): + # "<keyword>" num% + + try: + if not value.endswith("%"): raise ValueError() + percentage = float(value[:-1]) / 100 + + if validate and (percentage > 1 or percentage < 0): + raise ValueError() + + if keyword == "dirreq-v2-share": + self.dir_v2_share = percentage + elif keyword == "dirreq-v3-share": + self.dir_v3_share = percentage + except ValueError, exc: + if validate: + raise ValueError("Value can't be parsed as a percentage: %s" % line) elif keyword in ("published", "geoip-start-time"): # "<keyword>" YYYY-MM-DD HH:MM:SS
diff --git a/test/unit/descriptor/extrainfo_descriptor.py b/test/unit/descriptor/extrainfo_descriptor.py index 9732d4d..24034be 100644 --- a/test/unit/descriptor/extrainfo_descriptor.py +++ b/test/unit/descriptor/extrainfo_descriptor.py @@ -131,6 +131,39 @@ class TestExtraInfoDescriptor(unittest.TestCase): desc_text = _make_descriptor({"geoip-db-digest": entry}) desc = self._expect_invalid_attr(desc_text, "geoip_db_digest", entry)
+ def test_percentage_lines(self): + """ + Uses valid and invalid data to tests lines of the form... + "<keyword>" num% + """ + + for keyword in ('dirreq-v2-share', 'dirreq-v3-share'): + attr = keyword.replace('-', '_').replace('dirreq', 'dir') + + test_entries = ( + ("0.00%", 0.0), + ("0.01%", 0.0001), + ("50%", 0.5), + ("100.0%", 1.0), + ) + + for test_value, expected_value in test_entries: + desc_text = _make_descriptor({keyword: test_value}) + desc = ExtraInfoDescriptor(desc_text) + self.assertEquals(expected_value, getattr(desc, attr)) + + test_entries = ( + ("", None), + (" ", None), + ("100", None), + ("100.1%", 1.001), + ("-5%", -0.05), + ) + + for entry, expected in test_entries: + desc_text = _make_descriptor({keyword: entry}) + self._expect_invalid_attr(desc_text, attr, expected) + def test_timestamp_lines(self): """ Uses valid and invalid data to tests lines of the form...
tor-commits@lists.torproject.org