File: progressbar.py

package info (click to toggle)
orange3 3.40.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,908 kB
  • sloc: python: 162,745; ansic: 622; makefile: 322; sh: 93; cpp: 77
file content (77 lines) | stat: -rw-r--r-- 1,961 bytes parent folder | download | duplicates (2)
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
import contextlib
import warnings

from orangewidget.utils.progressbar import (
    ProgressBarMixin as _ProgressBarMixin
)
from Orange.widgets import gui

__all__ = [
    "ProgressBarMixin"
]


def _warn_deprecated_arg():
    warnings.warn(
        "'processEvents' argument is deprecated.\n"
        "It does nothing and will be removed in the future (passing it "
        "will raise a TypeError).",
        FutureWarning, stacklevel=3,
    )


class ProgressBarMixin(_ProgressBarMixin):
    def progressBarInit(self, *args, **kwargs):
        if args or kwargs:
            _warn_deprecated_arg()
        super().progressBarInit()

    def progressBarSet(self, value, *args, **kwargs):
        if args or kwargs:
            _warn_deprecated_arg()
        super().progressBarSet(value)

    def progressBarAdvance(self, value, *args, **kwargs):
        if args or kwargs:
            _warn_deprecated_arg()
        super().progressBarAdvance(value)

    def progressBarFinished(self, *args, **kwargs):
        if args or kwargs:
            _warn_deprecated_arg()
        super().progressBarFinished()

    @contextlib.contextmanager
    def progressBar(self, iterations=0):
        """
        Context manager for progress bar.

        Using it ensures that the progress bar is removed at the end without
        needing the `finally` blocks.

        Usage:

            with self.progressBar(20) as progress:
                ...
                progress.advance()

        or

            with self.progressBar() as progress:
                ...
                progress.advance(0.15)

        or

            with self.progressBar():
                ...
                self.progressBarSet(50)

        :param iterations: the number of iterations (optional)
        :type iterations: int
        """
        progress_bar = gui.ProgressBar(self, iterations)
        try:
            yield progress_bar
        finally:
            progress_bar.finish()