Skip to content

Commit

Permalink
Refactor 'meter' module, use config settings to pick the right class
Browse files Browse the repository at this point in the history
  • Loading branch information
dmach committed Jan 24, 2024
1 parent c7af0e4 commit 7d6eebe
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions osc/meter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@


import signal
from abc import ABC
from abc import abstractmethod
from typing import Optional

try:
import progressbar as pb
Expand All @@ -13,9 +16,25 @@
have_pb_module = False


class PBTextMeter:
class TextMeterBase(ABC):
@abstractmethod
def start(self, basename: str, size: Optional[int] = None):
pass

@abstractmethod
def update(self, amount_read: int):
pass

@abstractmethod
def end(self):
pass

def start(self, basename, size=None):

class PBTextMeter(TextMeterBase):
def __init__(self):
self.bar: pb.ProgressBar

def start(self, basename: str, size: Optional[int] = None):
if size is None:
widgets = [f"{basename}: ", pb.AnimatedMarker(), ' ', pb.Timer()]
self.bar = pb.ProgressBar(widgets=widgets, maxval=pb.UnknownLength)
Expand All @@ -33,33 +52,35 @@ def start(self, basename, size=None):
signal.siginterrupt(signal.SIGWINCH, False)
self.bar.start()

def update(self, amount_read):
def update(self, amount_read: int):
self.bar.update(amount_read)

def end(self):
self.bar.finish()


class NoPBTextMeter:
def start(self, basename, size=None):
def start(self, basename: str, size: Optional[int] = None):
pass

def update(self, *args, **kwargs):
def update(self, amount_read: int):
pass

def end(self, *args, **kwargs):
def end(self):
pass


def create_text_meter(*args, **kwargs):
use_pb_fallback = kwargs.pop('use_pb_fallback', True)
if have_pb_module or use_pb_fallback:
return TextMeter(*args, **kwargs)
return None
def create_text_meter(*args, **kwargs) -> TextMeterBase:
from .conf import config

# this option is no longer used
kwargs.pop("use_pb_fallback", True)

meter_class = PBTextMeter
if not have_pb_module or config.quiet or not config.show_download_progress:
meter_class = NoPBTextMeter

return meter_class(*args, **kwargs)


if have_pb_module:
TextMeter = PBTextMeter
else:
TextMeter = NoPBTextMeter
# vim: sw=4 et

0 comments on commit 7d6eebe

Please sign in to comment.