|
| 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