forked from wolph/python-progressbar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
204 lines (191 loc) · 5.52 KB
/
Copy path__init__.py
File metadata and controls
204 lines (191 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""progressbar2 public API.
Imports are lazy (PEP 562): ``import progressbar`` loads almost nothing; each
submodule and exported name is imported on first access. This keeps the import
light (in particular the widgets and the terminal/color tables are only loaded
when actually used) while preserving the full public API.
"""
import importlib
import typing
# Re-exported as a static module attribute (kept out of ``__all__`` for
# backwards compatibility). ``__date__`` used to be recomputed at import time
# via ``date.today()``; it now mirrors the release date from ``__about__``.
from .__about__ import (
__author__,
__date__ as __date__,
__version__,
)
if typing.TYPE_CHECKING:
# Eager imports for type checkers only; loaded lazily at runtime by
# __getattr__ below. Names appear in __all__ so they read as re-exports.
from .algorithms import (
DoubleExponentialMovingAverage,
ExponentialMovingAverage,
SmoothingAlgorithm,
)
from .bar import DataTransferBar, NullBar, ProgressBar
from .base import UnknownLength
from .fast import FastProgressBar
from .multi import MultiBar, SortKey
from .shortcuts import progressbar
from .terminal.stream import LineOffsetStreamWrapper
from .utils import len_color, streams
from .widgets import (
ETA,
AbsoluteETA,
AdaptiveETA,
AdaptiveTransferSpeed,
AnimatedMarker,
Bar,
BouncingBar,
Counter,
CurrentTime,
DataSize,
DynamicMessage,
FileTransferSpeed,
FormatCustomText,
FormatLabel,
FormatLabelBar,
GranularBar,
JobStatusBar,
MultiProgressBar,
MultiRangeBar,
Percentage,
PercentageLabelBar,
Postfix,
ReverseBar,
RotatingMarker,
SimpleProgress,
SmoothingETA,
Timer,
UnitProgress,
Variable,
VariableMixin,
)
#: Submodules accessible as ``progressbar.<name>``.
_SUBMODULES: frozenset[str] = frozenset(
{
'algorithms',
'bar',
'base',
'env',
'fast',
'multi',
'shortcuts',
'terminal',
'utils',
'widgets',
}
)
#: Exported name -> submodule it lives in.
_NAME_TO_MODULE: dict[str, str] = {
'DoubleExponentialMovingAverage': 'algorithms',
'ExponentialMovingAverage': 'algorithms',
'SmoothingAlgorithm': 'algorithms',
'DataTransferBar': 'bar',
'NullBar': 'bar',
'ProgressBar': 'bar',
'FastProgressBar': 'fast',
'UnknownLength': 'base',
'MultiBar': 'multi',
'SortKey': 'multi',
'progressbar': 'shortcuts',
'LineOffsetStreamWrapper': 'terminal.stream',
'len_color': 'utils',
'streams': 'utils',
'ETA': 'widgets',
'AbsoluteETA': 'widgets',
'AdaptiveETA': 'widgets',
'AdaptiveTransferSpeed': 'widgets',
'AnimatedMarker': 'widgets',
'Bar': 'widgets',
'BouncingBar': 'widgets',
'Counter': 'widgets',
'CurrentTime': 'widgets',
'DataSize': 'widgets',
'DynamicMessage': 'widgets',
'FileTransferSpeed': 'widgets',
'FormatCustomText': 'widgets',
'FormatLabel': 'widgets',
'FormatLabelBar': 'widgets',
'GranularBar': 'widgets',
'JobStatusBar': 'widgets',
'MultiProgressBar': 'widgets',
'MultiRangeBar': 'widgets',
'Percentage': 'widgets',
'Postfix': 'widgets',
'PercentageLabelBar': 'widgets',
'ReverseBar': 'widgets',
'RotatingMarker': 'widgets',
'SimpleProgress': 'widgets',
'SmoothingETA': 'widgets',
'Timer': 'widgets',
'UnitProgress': 'widgets',
'Variable': 'widgets',
'VariableMixin': 'widgets',
}
def __getattr__(name: str) -> typing.Any:
"""Lazily import submodules and exported names on first access."""
if name in _SUBMODULES:
module = importlib.import_module(f'.{name}', __name__)
globals()[name] = module # cache so __getattr__ runs only once
return module
module_name = _NAME_TO_MODULE.get(name)
if module_name is None:
raise AttributeError(f'module {__name__!r} has no attribute {name!r}')
value = getattr(importlib.import_module(f'.{module_name}', __name__), name)
globals()[name] = value
return value
def __dir__() -> list[str]:
return sorted(set(globals()) | set(__all__) | _SUBMODULES)
#: Canonical export list, kept equal to ``sorted(_NAME_TO_MODULE)`` (the single
#: source of truth for lazily re-exported names) plus the eagerly imported
#: dunders. Held as a static literal so type checkers can see the re-exports;
#: ``tests/test_init_exports.py`` asserts it stays in sync with the mapping.
__all__ = [
'ETA',
'AbsoluteETA',
'AdaptiveETA',
'AdaptiveTransferSpeed',
'AnimatedMarker',
'Bar',
'BouncingBar',
'Counter',
'CurrentTime',
'DataSize',
'DataTransferBar',
'DoubleExponentialMovingAverage',
'DynamicMessage',
'ExponentialMovingAverage',
'FastProgressBar',
'FileTransferSpeed',
'FormatCustomText',
'FormatLabel',
'FormatLabelBar',
'GranularBar',
'JobStatusBar',
'LineOffsetStreamWrapper',
'MultiBar',
'MultiProgressBar',
'MultiRangeBar',
'NullBar',
'Percentage',
'PercentageLabelBar',
'Postfix',
'ProgressBar',
'ReverseBar',
'RotatingMarker',
'SimpleProgress',
'SmoothingAlgorithm',
'SmoothingETA',
'SortKey',
'Timer',
'UnitProgress',
'UnknownLength',
'Variable',
'VariableMixin',
'__author__',
'__version__',
'len_color',
'progressbar',
'streams',
]