commit 61f2ba8450c8123e5c0ba2e04311d88ee9d85102 Author: juga0 juga@riseup.net Date: Mon Feb 11 10:50:53 2019 +0000
v3bwfile: include destinations' country in headers
Closes: #29299. --- sbws/core/generate.py | 5 ++++- sbws/lib/v3bwfile.py | 13 +++++++++---- tests/unit/lib/test_v3bwfile.py | 7 ++++++- 3 files changed, 19 insertions(+), 6 deletions(-)
diff --git a/sbws/core/generate.py b/sbws/core/generate.py index c022b5b..7283c25 100644 --- a/sbws/core/generate.py +++ b/sbws/core/generate.py @@ -9,6 +9,7 @@ from argparse import ArgumentDefaultsHelpFormatter import os import logging from sbws.util.timestamp import now_fname +from sbws.lib import destination
log = logging.getLogger(__name__)
@@ -106,7 +107,9 @@ def main(args, conf): "cached-consensus") # Accept None as scanner_country to be compatible with older versions. scanner_country = conf['scanner'].get('country') - bw_file = V3BWFile.from_results(results, scanner_country, state_fpath, + destinations_countries = destination.parse_destinations_countries(conf) + bw_file = V3BWFile.from_results(results, scanner_country, + destinations_countries, state_fpath, args.scale_constant, scaling_method, torflow_cap=args.torflow_bw_margin, round_digs=args.round_digs, diff --git a/sbws/lib/v3bwfile.py b/sbws/lib/v3bwfile.py index b0ab385..a7ce57d 100644 --- a/sbws/lib/v3bwfile.py +++ b/sbws/lib/v3bwfile.py @@ -29,7 +29,7 @@ KEYVALUE_SEP_V2 = ' ' # List of the extra KeyValues accepted by the class EXTRA_ARG_KEYVALUES = ['software', 'software_version', 'file_created', 'earliest_bandwidth', 'generator_started', - 'scanner_country'] + 'scanner_country', 'destinations_countries'] STATS_KEYVALUES = ['number_eligible_relays', 'minimum_number_eligible_relays', 'number_consensus_relays', 'percent_eligible_relays', 'minimum_percent_eligible_relays'] @@ -141,7 +141,8 @@ class V3BWHeader(object): return self.strv2
@classmethod - def from_results(cls, results, scanner_country=None, state_fpath=''): + def from_results(cls, results, scanner_country=None, + destinations_countries=None, state_fpath=''): kwargs = dict() latest_bandwidth = cls.latest_bandwidth_from_results(results) earliest_bandwidth = cls.earliest_bandwidth_from_results(results) @@ -154,6 +155,8 @@ class V3BWHeader(object): # To be compatible with older bandwidth files, do not require it. if scanner_country is not None: kwargs['scanner_country'] = scanner_country + if destinations_countries is not None: + kwargs['destinations_countries'] = destinations_countries h = cls(timestamp, **kwargs) return h
@@ -528,7 +531,8 @@ class V3BWFile(object): for bw_line in self.bw_lines])
@classmethod - def from_results(cls, results, scanner_country=None, state_fpath='', + def from_results(cls, results, scanner_country=None, + destinations_countries=None, state_fpath='', scale_constant=SBWS_SCALE_CONSTANT, scaling_method=TORFLOW_SCALING, torflow_obs=TORFLOW_OBS_LAST, @@ -555,7 +559,8 @@ class V3BWFile(object):
""" log.info('Processing results to generate a bandwidth list file.') - header = V3BWHeader.from_results(results, scanner_country, state_fpath) + header = V3BWHeader.from_results(results, scanner_country, + destinations_countries, state_fpath) bw_lines_raw = [] number_consensus_relays = cls.read_number_consensus_relays( consensus_path) diff --git a/tests/unit/lib/test_v3bwfile.py b/tests/unit/lib/test_v3bwfile.py index 506f63b..0448c69 100644 --- a/tests/unit/lib/test_v3bwfile.py +++ b/tests/unit/lib/test_v3bwfile.py @@ -18,6 +18,9 @@ timestamp_l = str(timestamp) version_l = KEYVALUE_SEP_V1.join(['version', SPEC_VERSION]) scanner_country = 'US' scanner_country_l = KEYVALUE_SEP_V1.join(['scanner_country', scanner_country]) +destinations_countries = '00,DE' +destinations_countries_l = KEYVALUE_SEP_V1.join(['destinations_countries', + destinations_countries]) software_l = KEYVALUE_SEP_V1.join(['software', 'sbws']) software_version_l = KEYVALUE_SEP_V1.join(['software_version', version]) file_created = '2018-04-25T13:10:57' @@ -25,7 +28,8 @@ file_created_l = KEYVALUE_SEP_V1.join(['file_created', file_created]) latest_bandwidth = '2018-04-17T14:09:07' latest_bandwidth_l = KEYVALUE_SEP_V1.join(['latest_bandwidth', latest_bandwidth]) -header_ls = [timestamp_l, version_l, file_created_l, latest_bandwidth_l, +header_ls = [timestamp_l, version_l, destinations_countries_l, file_created_l, + latest_bandwidth_l, scanner_country_l, software_l, software_version_l, TERMINATOR] header_str = LINE_SEP.join(header_ls) + LINE_SEP earliest_bandwidth = '2018-04-16T14:09:07' @@ -56,6 +60,7 @@ v3bw_str = header_extra_str + raw_bwl_str def test_v3bwheader_str(): """Test header str""" header = V3BWHeader(timestamp_l, scanner_country=scanner_country, + destinations_countries=destinations_countries, file_created=file_created) assert header_str == str(header)