Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit e79d628

Browse files
4.0.0 python-multibar refactoring
1 parent 5c6cf18 commit e79d628

31 files changed

Lines changed: 1260 additions & 1012 deletions

multibar/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
from .impl import *
2-
3-
__all__ = impl.__all__

multibar/__main__.py

Lines changed: 0 additions & 78 deletions
This file was deleted.

multibar/calculation_service.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import abc
2+
import typing
3+
4+
from . import iterators
5+
6+
7+
class CalculationServiceAware(abc.ABC):
8+
__slots__ = ()
9+
10+
@abc.abstractmethod
11+
def calculate_filled_indexes(self) -> iterators.AbstractIterator[int]:
12+
...
13+
14+
@abc.abstractmethod
15+
def calculate_unfilled_indexes(self) -> iterators.AbstractIterator[int]:
16+
...
17+
18+
@property
19+
@abc.abstractmethod
20+
def progressbar_length(self) -> int:
21+
...
22+
23+
@property
24+
@abc.abstractmethod
25+
def progress_percents(self) -> float:
26+
...
27+
28+
@property
29+
@abc.abstractmethod
30+
def start_value(self) -> typing.Union[int, float]:
31+
...
32+
33+
@property
34+
@abc.abstractmethod
35+
def end_value(self) -> typing.Union[int, float]:
36+
...
37+
38+
@staticmethod
39+
@abc.abstractmethod
40+
def get_progress_percentage(start: int, end: int, /) -> float:
41+
...
42+
43+
44+
class ProgressbarCalculationService(CalculationServiceAware):
45+
def __init__(
46+
self,
47+
start_value: typing.Union[int, float],
48+
end_value: typing.Union[int, float],
49+
length: int,
50+
) -> None:
51+
self._start_value = start_value
52+
self._end_value = end_value
53+
self._length = length
54+
55+
def calculate_filled_indexes(self) -> iterators.AbstractIterator[int]:
56+
filled_range = range(round(self.progress_percents / (100 / self._length)))
57+
return iterators.Iterator(iter(filled_range)).indexes()
58+
59+
def calculate_unfilled_indexes(self) -> iterators.AbstractIterator[int]:
60+
filled_range_len = len(tuple(self.calculate_filled_indexes()))
61+
unfilled_range = range(self._length - filled_range_len)
62+
return iterators.Iterator(iter(unfilled_range)).indexes(conversion=lambda i: i + filled_range_len)
63+
64+
@property
65+
def progressbar_length(self) -> int:
66+
return self._length
67+
68+
@property
69+
def progress_percents(self) -> float:
70+
return self.get_progress_percentage(self._start_value, self._end_value)
71+
72+
@property
73+
def start_value(self) -> typing.Union[int, float]:
74+
return self._start_value
75+
76+
@property
77+
def end_value(self) -> typing.Union[int, float]:
78+
return self._end_value
79+
80+
@staticmethod
81+
def get_progress_percentage(start: int, end: int, /) -> float:
82+
return (start / end) * 100

multibar/clients.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
from __future__ import annotations
2+
import abc
3+
import typing
4+
5+
from . import utils
6+
from . import writers
7+
from . import contracts
8+
from . import hooks as hooks_
9+
from . import types as progress_types
10+
11+
if typing.TYPE_CHECKING:
12+
from . import progressbars
13+
from . import sectors
14+
15+
16+
class ProgressbarClientAware(abc.ABC):
17+
@abc.abstractmethod
18+
@typing.overload
19+
def get_progress(
20+
self,
21+
start_value: int,
22+
end_value: int,
23+
/,
24+
) -> typing.Optional[progressbars.Progressbar[sectors.AbstractSector]]:
25+
...
26+
27+
@abc.abstractmethod
28+
@typing.overload
29+
def get_progress(
30+
self,
31+
start_value: int,
32+
end_value: int,
33+
/,
34+
*,
35+
length: int,
36+
) -> typing.Optional[progressbars.Progressbar[sectors.AbstractSector]]:
37+
...
38+
39+
@abc.abstractmethod
40+
def get_progress(
41+
self,
42+
start_value: int,
43+
end_value: int,
44+
/,
45+
*,
46+
length: int = 20,
47+
) -> typing.Optional[progressbars.Progressbar[sectors.AbstractSector]]:
48+
...
49+
50+
@abc.abstractmethod
51+
def set_hooks(self, hooks: hooks_.HooksAware) -> ProgressbarClientAware:
52+
...
53+
54+
@abc.abstractmethod
55+
def update_hooks(self, hooks: hooks_.HooksAware) -> ProgressbarClientAware:
56+
...
57+
58+
@property
59+
@abc.abstractmethod
60+
def hooks(self) -> hooks_.HooksAware:
61+
...
62+
63+
@property
64+
@abc.abstractmethod
65+
def contract_manager(self) -> contracts.ContractManagerAware:
66+
...
67+
68+
69+
class ProgressbarClient(ProgressbarClientAware):
70+
def __init__(
71+
self,
72+
*,
73+
hooks: typing.Optional[hooks_.HooksAware] = None,
74+
progress_writer: typing.Optional[writers.ProgressbarWriterAware] = None,
75+
contract_manager: typing.Optional[contracts.ContractManagerAware] = None,
76+
) -> None:
77+
self._hooks = utils.none_or(hooks_.Hooks(), hooks)
78+
self._writer = utils.none_or(writers.ProgressbarWriter(), progress_writer)
79+
80+
if contract_manager is None:
81+
contract_manager = contracts.ContractManager()
82+
contract_manager.subscribe(contracts.WriteProgressContract())
83+
84+
self._contract_manager: contracts.ContractManagerAware = contract_manager
85+
86+
def _validate_contracts(self, *args: typing.Any, **kwargs: typing.Any) -> None:
87+
try:
88+
self._contract_manager.trigger_contracts(*args, **kwargs)
89+
except Exception as exc:
90+
self._hooks.trigger_on_error(*args, exc, **kwargs)
91+
92+
def get_progress(
93+
self,
94+
start_value: int,
95+
end_value: int,
96+
/,
97+
*,
98+
length: int = 20,
99+
) -> typing.Optional[progressbars.Progressbar[sectors.AbstractSector]]:
100+
writer = self._writer
101+
call_metadata: progress_types.ProgressMetadataType = {
102+
"calculation_service_cls": writer.calculation_cls,
103+
"progressbar": None,
104+
"start_value": start_value,
105+
"end_value": end_value,
106+
"length": length,
107+
"sig": writer.signature,
108+
}
109+
110+
self._validate_contracts(self._writer, metadata=call_metadata)
111+
self._hooks.trigger_pre_execution(self, metadata=call_metadata)
112+
113+
progressbar = self._writer.write(start_value, end_value, length=length)
114+
call_metadata["progressbar"] = progressbar
115+
116+
self._hooks.trigger_post_execution(self, metadata=call_metadata)
117+
return progressbar
118+
119+
def set_hooks(self, hooks: hooks_.HooksAware) -> ProgressbarClient:
120+
self._hooks = hooks
121+
return self
122+
123+
def update_hooks(self, hooks: hooks_.HooksAware) -> ProgressbarClient:
124+
self._hooks = self._hooks | hooks
125+
return self
126+
127+
@property
128+
def hooks(self) -> hooks_.HooksAware:
129+
return self._hooks
130+
131+
@property
132+
def contract_manager(self) -> contracts.ContractManagerAware:
133+
return self._contract_manager

0 commit comments

Comments
 (0)