Coverage for .tox/coverage/lib/python3.11/site-packages/wuttjamaican/progress.py: 100%
19 statements
« prev ^ index » next coverage.py v7.3.2, created at 2024-08-26 14:27 -0500
« prev ^ index » next coverage.py v7.3.2, created at 2024-08-26 14:27 -0500
1# -*- coding: utf-8; -*-
2################################################################################
3#
4# WuttJamaican -- Base package for Wutta Framework
5# Copyright © 2023-2024 Lance Edgar
6#
7# This file is part of Wutta Framework.
8#
9# Wutta Framework is free software: you can redistribute it and/or modify it
10# under the terms of the GNU General Public License as published by the Free
11# Software Foundation, either version 3 of the License, or (at your option) any
12# later version.
13#
14# Wutta Framework is distributed in the hope that it will be useful, but
15# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17# more details.
18#
19# You should have received a copy of the GNU General Public License along with
20# Wutta Framework. If not, see <http://www.gnu.org/licenses/>.
21#
22################################################################################
23"""
24Progress Indicators
25"""
27import sys
29from progress.bar import Bar
32class ProgressBase:
33 """
34 Base class for progress indicators.
36 This is *only* a base class, and should not be used directly. For
37 simple console use, see :class:`ConsoleProgress`.
39 Progress indicators are created via factory from various places in
40 the code. The factory is called with ``(message, maximum)`` args
41 and it must return a progress instance with these methods:
43 * :meth:`update()`
44 * :meth:`finish()`
46 Code may call ``update()`` several times while its operation
47 continues; it then ultimately should call ``finish()``.
49 See also :func:`wuttjamaican.util.progress_loop()` and
50 :meth:`wuttjamaican.app.AppHandler.progress_loop()` for a way to
51 do these things automatically from code.
53 :param message: Info message to be displayed along with the
54 progress bar.
56 :param maximum: Max progress value.
57 """
59 def __init__(self, message, maximum):
60 self.message = message
61 self.maximum = maximum
63 def update(self, value):
64 """
65 Update the current progress value.
67 :param value: New progress value to be displayed.
68 """
70 def finish(self):
71 """
72 Wrap things up for the progress display etc.
73 """
76class ConsoleProgress(ProgressBase):
77 """
78 Provides a console-based progress bar.
80 This is a subclass of :class:`ProgressBase`.
82 Simple usage is like::
84 from wuttjamaican.progress import ConsoleProgress
86 def action(obj, i):
87 print(obj)
89 items = [1, 2, 3, 4, 5]
91 app = config.get_app()
92 app.progress_loop(action, items, ConsoleProgress,
93 message="printing items")
95 See also :func:`~wuttjamaican.util.progress_loop()`.
96 """
98 def __init__(self, *args, **kwargs):
99 super().__init__(*args)
101 self.stderr = kwargs.get('stderr', sys.stderr)
102 self.stderr.write(f"\n{self.message}...\n")
104 self.bar = Bar(message='', max=self.maximum, width=70,
105 suffix='%(index)d/%(max)d %(percent)d%% ETA %(eta)ds')
107 def update(self, value):
108 """ """
109 self.bar.next()
111 def finish(self):
112 """ """
113 self.bar.finish()