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

[tor-commits] [sbws/master] Refactor V3BwFile ...



commit 873530454b07d99674f805b8652888b866cb9683
Author: Matt Traudt <sirmatt@xxxxxxx>
Date:   Thu Jun 14 18:34:37 2018 -0400

    Refactor V3BwFile ...
    
    It doesn't need to know how to write to a file. It store and organizes
    data.
---
 sbws/core/generate.py | 29 +++++++++++++++++++++++++++++
 sbws/lib/v3bwfile.py  | 31 ++-----------------------------
 2 files changed, 31 insertions(+), 29 deletions(-)

diff --git a/sbws/core/generate.py b/sbws/core/generate.py
index 61f807d..3f0df18 100644
--- a/sbws/core/generate.py
+++ b/sbws/core/generate.py
@@ -4,6 +4,8 @@ from sbws.lib.resultdump import load_recent_results_in_datadir
 from argparse import ArgumentDefaultsHelpFormatter
 import os
 import logging
+from sbws.util.filelock import DirectoryLock
+from sbws.util.timestamp import now_fname
 
 log = logging.getLogger(__name__)
 
@@ -31,6 +33,31 @@ def gen_parser(sub):
                    'out, and we do so proportionally')
 
 
+def _write_v3bw_file(v3bwfile, output):
+    log.info('Writing v3bw file to %s', output)
+    out_dir = os.path.dirname(output)
+    out_link = os.path.join(out_dir, 'latest.v3bw')
+    if os.path.exists(out_link):
+        log.debug('Deleting existing symlink before creating a new one.')
+        os.remove(out_link)
+    # to keep test_generate.py working
+    if output != '/dev/stdout':
+        with DirectoryLock(out_dir):
+            with open(output, 'wt') as fd:
+                fd.write(str(v3bwfile.header))
+                for line in v3bwfile.bw_lines:
+                    fd.write(str(line))
+            output_basename = os.path.basename(output)
+            log.debug('Creating symlink from {} to {}.'
+                      .format(output_basename, out_link))
+            os.symlink(output_basename, out_link)
+    else:
+        with open(output, 'wt') as fd:
+            fd.write(str(v3bwfile.header))
+            for line in v3bwfile.bw_lines:
+                fd.write(str(line))
+
+
 def main(args, conf):
     if not is_initted(args.directory):
         fail_hard('Sbws isn\'t initialized.  Try sbws init')
@@ -51,4 +78,6 @@ def main(args, conf):
                     'ran sbws scanner recently?)')
         return
     bw_file = V3BwFile.from_arg_results(args, conf, results)
+    output = args.output or conf['paths']['v3bw_fname'].format(now_fname())
+    _write_v3bw_file(bw_file, output)
     log.info('Mean bandwidth per line: %f "KiB"', bw_file.avg_bw)
diff --git a/sbws/lib/v3bwfile.py b/sbws/lib/v3bwfile.py
index 9b8e2e8..3cff29d 100644
--- a/sbws/lib/v3bwfile.py
+++ b/sbws/lib/v3bwfile.py
@@ -3,14 +3,13 @@
 (v3bw) used by bandwidth authorities."""
 
 import logging
-import os
 from statistics import median
 
 from sbws import __version__
 from sbws.globals import SPEC_VERSION, BW_LINE_SIZE
 from sbws.lib.resultdump import ResultSuccess, _ResultType
-from sbws.util.filelock import FileLock, DirectoryLock
-from sbws.util.timestamp import now_isodt_str, unixts_to_isodt_str, now_fname
+from sbws.util.filelock import FileLock
+from sbws.util.timestamp import now_isodt_str, unixts_to_isodt_str
 
 log = logging.getLogger(__name__)
 
@@ -396,30 +395,4 @@ class V3BwFile(object):
             bw_lines = scale_lines(bw_lines, args.scale_constant)
         header = V3BwHeader.from_results(conf, results)
         f = cls(header, bw_lines)
-        output = args.output or conf['paths']['v3bw_fname'].format(now_fname())
-        f.write(output)
         return f
-
-    def write(self, output):
-        log.info('Writing v3bw file to %s', output)
-        out_dir = os.path.dirname(output)
-        out_link = os.path.join(out_dir, 'latest.v3bw')
-        if os.path.exists(out_link):
-            log.debug('Deleting existing symlink before creating a new one.')
-            os.remove(out_link)
-        # to keep test_generate.py working
-        if output != '/dev/stdout':
-            with DirectoryLock(out_dir):
-                with open(output, 'wt') as fd:
-                    fd.write(str(self.header))
-                    for line in self.bw_lines:
-                        fd.write(str(line))
-                output_basename = os.path.basename(output)
-                log.debug('Creating symlink from {} to {}.'
-                          .format(output_basename, out_link))
-                os.symlink(output_basename, out_link)
-        else:
-            with open(output, 'wt') as fd:
-                fd.write(str(self.header))
-                for line in self.bw_lines:
-                    fd.write(str(line))



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