Skip to content

Commit

Permalink
fixed ruff issues
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed Jan 25, 2024
1 parent 16c4fce commit 671b723
Show file tree
Hide file tree
Showing 11 changed files with 329 additions and 308 deletions.
9 changes: 6 additions & 3 deletions progressbar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
from datetime import date

from .__about__ import __author__, __version__
from .algorithms import (
DoubleExponentialMovingAverage,
ExponentialMovingAverage,
SmoothingAlgorithm,
)
from .bar import DataTransferBar, NullBar, ProgressBar
from .base import UnknownLength
from .multi import MultiBar, SortKey
from .shortcuts import progressbar
from .terminal.stream import LineOffsetStreamWrapper
from .utils import len_color, streams
from .algorithms import ExponentialMovingAverage, SmoothingAlgorithm, DoubleExponentialMovingAverage
from .widgets import (
ETA,
AbsoluteETA,
AdaptiveETA,
SmoothingETA,
AdaptiveTransferSpeed,
AnimatedMarker,
Bar,
Expand All @@ -34,11 +37,11 @@
ReverseBar,
RotatingMarker,
SimpleProgress,
SmoothingETA,
Timer,
Variable,
VariableMixin,
)
from .algorithms import ExponentialMovingAverage, SmoothingAlgorithm

__date__ = str(date.today())
__all__ = [
Expand Down
4 changes: 1 addition & 3 deletions progressbar/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class SmoothingAlgorithm(abc.ABC):

@abc.abstractmethod
def __init__(self, **kwargs):
raise NotImplementedError
Expand Down Expand Up @@ -41,7 +40,7 @@ class DoubleExponentialMovingAverage(SmoothingAlgorithm):
It's more responsive to recent changes in data.
'''

def __init__(self, alpha: float=0.5) -> None:
def __init__(self, alpha: float = 0.5) -> None:
self.alpha = alpha
self.ema1 = 0
self.ema2 = 0
Expand All @@ -50,4 +49,3 @@ def update(self, new_value: float, elapsed: timedelta) -> float:
self.ema1 = self.alpha * new_value + (1 - self.alpha) * self.ema1
self.ema2 = self.alpha * self.ema1 + (1 - self.alpha) * self.ema2
return 2 * self.ema1 - self.ema2

19 changes: 10 additions & 9 deletions progressbar/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from . import base



@typing.overload
def env_flag(name: str, default: bool) -> bool:
...
Expand Down Expand Up @@ -68,7 +67,7 @@ def from_env(cls):
)

if os.environ.get('JUPYTER_COLUMNS') or os.environ.get(
'JUPYTER_LINES',
'JUPYTER_LINES',
):
# Jupyter notebook always supports true color.
return cls.XTERM_TRUECOLOR
Expand All @@ -77,9 +76,10 @@ def from_env(cls):
# will assume it is supported if the console is configured to
# support it.
from .terminal.os_specific import windows

if (
windows.get_console_mode() &
windows.WindowsConsoleModeFlags.ENABLE_PROCESSED_OUTPUT
windows.get_console_mode()
& windows.WindowsConsoleModeFlags.ENABLE_PROCESSED_OUTPUT
):
return cls.XTERM_TRUECOLOR
else:
Expand All @@ -103,8 +103,8 @@ def from_env(cls):


def is_ansi_terminal(
fd: base.IO,
is_terminal: bool | None = None,
fd: base.IO,
is_terminal: bool | None = None,
) -> bool | None: # pragma: no cover
if is_terminal is None:
# Jupyter Notebooks define this variable and support progress bars
Expand All @@ -113,7 +113,7 @@ def is_ansi_terminal(
# This works for newer versions of pycharm only. With older versions
# there is no way to check.
elif os.environ.get('PYCHARM_HOSTED') == '1' and not os.environ.get(
'PYTEST_CURRENT_TEST',
'PYTEST_CURRENT_TEST',
):
is_terminal = True

Expand All @@ -133,9 +133,10 @@ def is_ansi_terminal(
is_terminal = True
elif os.name == 'nt':
from .terminal.os_specific import windows

return bool(
windows.get_console_mode() &
windows.WindowsConsoleModeFlags.ENABLE_PROCESSED_OUTPUT
windows.get_console_mode()
& windows.WindowsConsoleModeFlags.ENABLE_PROCESSED_OUTPUT,
)
else:
is_terminal = None
Expand Down
51 changes: 27 additions & 24 deletions progressbar/terminal/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
import enum
import threading
from collections import defaultdict

# Ruff is being stupid and doesn't understand `ClassVar` if it comes from the
# `types` module
from typing import ClassVar

from python_utils import converters, types

from .os_specific import getch
from .. import (
base as pbase,
env,
)
from .os_specific import getch

ESC = '\x1B'

Expand Down Expand Up @@ -178,7 +179,6 @@ def column(self, stream):
return column



class WindowsColors(enum.Enum):
BLACK = 0, 0, 0
BLUE = 0, 0, 128
Expand Down Expand Up @@ -235,20 +235,23 @@ class WindowsColor:
>>> WindowsColor(WindowsColors.RED)('test')
'test'
'''
__slots__ = 'color',

__slots__ = ('color',)

def __init__(self, color: Color):
self.color = color

def __call__(self, text):
return text
# In the future we might want to use this, but it requires direct printing to stdout and all of our surrounding functions expect buffered output so it's not feasible right now.
# Additionally, recent Windows versions all support ANSI codes without issue so there is little need.
## In the future we might want to use this, but it requires direct
## printing to stdout and all of our surrounding functions expect
## buffered output so it's not feasible right now. Additionally,
## recent Windows versions all support ANSI codes without issue so
## there is little need.
# from progressbar.terminal.os_specific import windows
# windows.print_color(text, WindowsColors.from_rgb(self.color.rgb))



class RGB(collections.namedtuple('RGB', ['red', 'green', 'blue'])):
__slots__ = ()

Expand Down Expand Up @@ -387,14 +390,14 @@ def underline(self):
@property
def ansi(self) -> types.Optional[str]:
if (
env.COLOR_SUPPORT is env.ColorSupport.XTERM_TRUECOLOR
env.COLOR_SUPPORT is env.ColorSupport.XTERM_TRUECOLOR
): # pragma: no branch
return f'2;{self.rgb.red};{self.rgb.green};{self.rgb.blue}'

if self.xterm: # pragma: no branch
color = self.xterm
elif (
env.COLOR_SUPPORT is env.ColorSupport.XTERM_256
env.COLOR_SUPPORT is env.ColorSupport.XTERM_256
): # pragma: no branch
color = self.rgb.to_ansi_256
elif env.COLOR_SUPPORT is env.ColorSupport.XTERM: # pragma: no branch
Expand Down Expand Up @@ -442,11 +445,11 @@ class Colors:

@classmethod
def register(
cls,
rgb: RGB,
hls: types.Optional[HSL] = None,
name: types.Optional[str] = None,
xterm: types.Optional[int] = None,
cls,
rgb: RGB,
hls: types.Optional[HSL] = None,
name: types.Optional[str] = None,
xterm: types.Optional[int] = None,
) -> Color:
color = Color(rgb, hls, name, xterm)

Expand Down Expand Up @@ -483,9 +486,9 @@ def __call__(self, value: float) -> Color:
def get_color(self, value: float) -> Color:
'Map a value from 0 to 1 to a color.'
if (
value == pbase.Undefined
or value == pbase.UnknownLength
or value <= 0
value == pbase.Undefined
or value == pbase.UnknownLength
or value <= 0
):
return self.colors[0]
elif value >= 1:
Expand Down Expand Up @@ -531,14 +534,14 @@ def get_color(value: float, color: OptionalColor) -> Color | None:


def apply_colors(
text: str,
percentage: float | None = None,
*,
fg: OptionalColor = None,
bg: OptionalColor = None,
fg_none: Color | None = None,
bg_none: Color | None = None,
**kwargs: types.Any,
text: str,
percentage: float | None = None,
*,
fg: OptionalColor = None,
bg: OptionalColor = None,
fg_none: Color | None = None,
bg_none: Color | None = None,
**kwargs: types.Any,
) -> str:
'''Apply colors/gradients to a string depending on the given percentage.
Expand Down
2 changes: 1 addition & 1 deletion progressbar/terminal/os_specific/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

if sys.platform.startswith('win'):
from .windows import (
get_console_mode as _get_console_mode,
getch as _getch,
reset_console_mode as _reset_console_mode,
set_console_mode as _set_console_mode,
get_console_mode as _get_console_mode,
)

else:
Expand Down
13 changes: 8 additions & 5 deletions progressbar/terminal/os_specific/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,16 @@ def reset_console_mode():


def set_console_mode() -> bool:
mode = _input_mode.value | WindowsConsoleModeFlags.ENABLE_VIRTUAL_TERMINAL_INPUT
mode = (
_input_mode.value
| WindowsConsoleModeFlags.ENABLE_VIRTUAL_TERMINAL_INPUT
)
_SetConsoleMode(_HANDLE(_h_console_input), _DWORD(mode))

mode = (
_output_mode.value
| WindowsConsoleModeFlags.ENABLE_PROCESSED_OUTPUT
| WindowsConsoleModeFlags.ENABLE_VIRTUAL_TERMINAL_PROCESSING
_output_mode.value
| WindowsConsoleModeFlags.ENABLE_PROCESSED_OUTPUT
| WindowsConsoleModeFlags.ENABLE_VIRTUAL_TERMINAL_PROCESSING
)
return bool(_SetConsoleMode(_HANDLE(_h_console_output), _DWORD(mode)))

Expand All @@ -147,7 +150,7 @@ def set_text_color(color):

def print_color(text, color):
set_text_color(color)
print(text)
print(text) # noqa: T201
set_text_color(7) # Reset to default color, grey


Expand Down
Loading

0 comments on commit 671b723

Please sign in to comment.