[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [sbws/master] Pass state file path and scale constant



commit cf9a55a359d8071a8bc2abdfeb1886804165c4bd
Author: juga0 <juga@xxxxxxxxxx>
Date:   Wed Aug 29 15:01:38 2018 +0000

    Pass state file path and scale constant
    
    instead of conf and args, so that it's easier to test the method
    without creating ConfigParser and ArgumentParser objects.
---
 sbws/core/generate.py |  3 ++-
 sbws/lib/v3bwfile.py  | 18 ++++++++++--------
 2 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/sbws/core/generate.py b/sbws/core/generate.py
index f10ecc1..777b05e 100644
--- a/sbws/core/generate.py
+++ b/sbws/core/generate.py
@@ -58,7 +58,8 @@ def main(args, conf):
         log.warning('No recent results, so not generating anything. (Have you '
                     'ran sbws scanner recently?)')
         return
-    bw_file = V3BWFile.from_arg_results(args, conf, results)
+    state_fpath = conf.getpath('paths', 'state_fname')
+    bw_file = V3BWFile.from_results(results, state_fpath, args.scale_constant)
     output = args.output or \
         conf.getpath('paths', 'v3bw_fname').format(now_fname())
     bw_file.write(output)
diff --git a/sbws/lib/v3bwfile.py b/sbws/lib/v3bwfile.py
index 576345e..ef9eadc 100644
--- a/sbws/lib/v3bwfile.py
+++ b/sbws/lib/v3bwfile.py
@@ -115,11 +115,11 @@ class V3BWHeader(object):
         return self.strv200
 
     @classmethod
-    def from_results(cls, conf, results):
+    def from_results(cls, results, state_fpath=''):
         kwargs = dict()
         latest_bandwidth = cls.latest_bandwidth_from_results(results)
         earliest_bandwidth = cls.earliest_bandwidth_from_results(results)
-        generator_started = cls.generator_started_from_file(conf)
+        generator_started = cls.generator_started_from_file(state_fpath)
         timestamp = str(latest_bandwidth)
         kwargs['latest_bandwidth'] = unixts_to_isodt_str(latest_bandwidth)
         kwargs['earliest_bandwidth'] = unixts_to_isodt_str(earliest_bandwidth)
@@ -159,12 +159,12 @@ class V3BWHeader(object):
         return self.from_lines_v110(text.split(LINE_SEP))
 
     @staticmethod
-    def generator_started_from_file(conf):
+    def generator_started_from_file(state_fpath):
         '''
         ISO formatted timestamp for the time when the scanner process most
         recently started.
         '''
-        state = State(conf.getpath('paths', 'state_fname'))
+        state = State(state_fpath)
         if 'scanner_started' in state:
             return state['scanner_started']
         else:
@@ -376,12 +376,14 @@ class V3BWFile(object):
                                            for bw_line in self.bw_lines])
 
     @classmethod
-    def from_arg_results(cls, args, conf, results):
+    def from_results(cls, results, state_fpath='',
+                     scale_constant=None):
         bw_lines = [V3BWLine.from_results(results[fp]) for fp in results]
         bw_lines = sorted(bw_lines, key=lambda d: d.bw, reverse=True)
-        if args.scale:
-            bw_lines = scale_lines(bw_lines, args.scale_constant)
-        header = V3BWHeader.from_results(conf, results)
+        if scale_constant:
+            bw_lines = cls.bw_sbws_scale(bw_lines, scale_constant)
+            cls.warn_if_not_accurate_enough(bw_lines, scale_constant)
+        header = V3BWHeader.from_results(results, state_fpath)
         f = cls(header, bw_lines)
         return f
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits