mirror of
https://github.com/openSUSE/osc.git
synced 2025-01-19 11:56:13 +01:00
Refactor 'meter' module, use config settings to pick the right class
This commit is contained in:
parent
c7af0e458f
commit
7d6eebeabb
51
osc/meter.py
51
osc/meter.py
@ -5,6 +5,9 @@
|
|||||||
|
|
||||||
|
|
||||||
import signal
|
import signal
|
||||||
|
from abc import ABC
|
||||||
|
from abc import abstractmethod
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import progressbar as pb
|
import progressbar as pb
|
||||||
@ -13,9 +16,25 @@ except ImportError:
|
|||||||
have_pb_module = False
|
have_pb_module = False
|
||||||
|
|
||||||
|
|
||||||
class PBTextMeter:
|
class TextMeterBase(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def start(self, basename: str, size: Optional[int] = None):
|
||||||
|
pass
|
||||||
|
|
||||||
def start(self, basename, size=None):
|
@abstractmethod
|
||||||
|
def update(self, amount_read: int):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def end(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class PBTextMeter(TextMeterBase):
|
||||||
|
def __init__(self):
|
||||||
|
self.bar: pb.ProgressBar
|
||||||
|
|
||||||
|
def start(self, basename: str, size: Optional[int] = None):
|
||||||
if size is None:
|
if size is None:
|
||||||
widgets = [f"{basename}: ", pb.AnimatedMarker(), ' ', pb.Timer()]
|
widgets = [f"{basename}: ", pb.AnimatedMarker(), ' ', pb.Timer()]
|
||||||
self.bar = pb.ProgressBar(widgets=widgets, maxval=pb.UnknownLength)
|
self.bar = pb.ProgressBar(widgets=widgets, maxval=pb.UnknownLength)
|
||||||
@ -33,7 +52,7 @@ class PBTextMeter:
|
|||||||
signal.siginterrupt(signal.SIGWINCH, False)
|
signal.siginterrupt(signal.SIGWINCH, False)
|
||||||
self.bar.start()
|
self.bar.start()
|
||||||
|
|
||||||
def update(self, amount_read):
|
def update(self, amount_read: int):
|
||||||
self.bar.update(amount_read)
|
self.bar.update(amount_read)
|
||||||
|
|
||||||
def end(self):
|
def end(self):
|
||||||
@ -41,25 +60,27 @@ class PBTextMeter:
|
|||||||
|
|
||||||
|
|
||||||
class NoPBTextMeter:
|
class NoPBTextMeter:
|
||||||
def start(self, basename, size=None):
|
def start(self, basename: str, size: Optional[int] = None):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def update(self, *args, **kwargs):
|
def update(self, amount_read: int):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def end(self, *args, **kwargs):
|
def end(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def create_text_meter(*args, **kwargs):
|
def create_text_meter(*args, **kwargs) -> TextMeterBase:
|
||||||
use_pb_fallback = kwargs.pop('use_pb_fallback', True)
|
from .conf import config
|
||||||
if have_pb_module or use_pb_fallback:
|
|
||||||
return TextMeter(*args, **kwargs)
|
# this option is no longer used
|
||||||
return None
|
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
|
# vim: sw=4 et
|
||||||
|
Loading…
Reference in New Issue
Block a user