From 3d130ffdb1665f0413c19b63ee0586d2e9ef1b48 Mon Sep 17 00:00:00 2001 From: Will Woods Date: Jun 05 2020 17:36:33 +0000 Subject: countme.progress: refactor & fix tqdm traceback with display=False tqdm objects don't have a `.desc` attribute if disable=True, but they still accept set_description. Refactor the progress classes so they both use the same _iter_log_lines function, which doesn't use `prog.desc`. --- diff --git a/countme/progress.py b/countme/progress.py index 67a871f..4666715 100644 --- a/countme/progress.py +++ b/countme/progress.py @@ -51,35 +51,21 @@ class ReadProgressBase: raise NotImplementedError -# If we have the tqdm module available then hooray +# Here's how we use the tqdm progress module to show read progress. class TQDMReadProgress(ReadProgressBase): - def _iter_log_lines(self, logf, num): + def _get_prog(self, logf, num): # Make a progress meter for this file - prog = tqdm(unit="B", unit_scale=True, unit_divisor=1024, + return tqdm(unit="B", unit_scale=True, unit_divisor=1024, total=os.stat(logf.name).st_size, - disable=True if not self.display else None, - desc=f"log {num+1}/{len(self.logs)}") - # Get the first line manually so we can get logdate - line = next(logf) - prog.set_description(f"{prog.desc}, date={log_date(line)}") - # Update bar and yield the first line - prog.update(len(line)) - yield line - # And now we do the rest of the file - for line in logf: - prog.update(len(line)) - yield line - prog.close() + disable=True if not self.display else None) -class DIYReadProgress(ReadProgressBase): def _iter_log_lines(self, logf, num): # Make a progress meter for this file - prog = diyprog(total=os.stat(logf.name).st_size, - disable=True if not self.display else None, - desc=f"log {num+1}/{len(self.logs)}") + prog = self._get_prog(logf, num) # Get the first line manually so we can get logdate line = next(logf) - prog.set_description(f"{prog.desc}, date={log_date(line)}") + desc = f"log {num+1}/{len(self.logs)}, date={log_date(line)}" + prog.set_description(desc) # Update bar and yield the first line prog.update(len(line)) yield line @@ -89,16 +75,22 @@ class DIYReadProgress(ReadProgressBase): yield line prog.close() +# No TQDM? Use our little do-it-yourself knockoff version. +class DIYReadProgress(TQDMReadProgress): + def _get_prog(self, logf, num): + return diyprog(total=os.stat(logf.name).st_size, + disable=True if not self.display else None) + class diyprog: def __init__(self, desc=None, total=None, file=None, disable=False, unit='b', unit_scale=True, barchar='_-=#'): + # COMPAT NOTE: tqdm objects with disable=True have no .desc attribute self.desc = desc self.total = total self.file = file self.disable = disable self.unit = unit self.unit_scale = unit_scale - #self.unit_divisor = unit_divisor self.count = 0 self.showat = 0 self.barchar = barchar