1823b8294SYaroslav Brustinov#!/router/bin/python
289a2be82Simaromfrom __future__ import print_function
3823b8294SYaroslav Brustinovimport threading
4823b8294SYaroslav Brustinovimport sys
5823b8294SYaroslav Brustinovimport time
6823b8294SYaroslav Brustinovimport outer_packages
7823b8294SYaroslav Brustinovimport termstyle
8823b8294SYaroslav Brustinovimport progressbar
9823b8294SYaroslav Brustinov
10823b8294SYaroslav Brustinov
11823b8294SYaroslav Brustinovclass ProgressThread(threading.Thread):
12823b8294SYaroslav Brustinov    def __init__(self, notifyMessage = None):
13823b8294SYaroslav Brustinov        super(ProgressThread, self).__init__()
14823b8294SYaroslav Brustinov        self.stoprequest = threading.Event()
15823b8294SYaroslav Brustinov        self.notifyMessage = notifyMessage
16823b8294SYaroslav Brustinov
17823b8294SYaroslav Brustinov    def run(self):
18823b8294SYaroslav Brustinov        if self.notifyMessage is not None:
1989a2be82Simarom            print(self.notifyMessage, end=' ')
20823b8294SYaroslav Brustinov
21823b8294SYaroslav Brustinov        while not self.stoprequest.is_set():
2289a2be82Simarom            print("\b.", end=' ')
23823b8294SYaroslav Brustinov            sys.stdout.flush()
24823b8294SYaroslav Brustinov            time.sleep(5)
25823b8294SYaroslav Brustinov
26823b8294SYaroslav Brustinov    def join(self, timeout=None):
27823b8294SYaroslav Brustinov        if self.notifyMessage is not None:
2889a2be82Simarom            print(termstyle.green("Done!\n"), end=' ')
29823b8294SYaroslav Brustinov        self.stoprequest.set()
30823b8294SYaroslav Brustinov        super(ProgressThread, self).join(timeout)
31823b8294SYaroslav Brustinov
32823b8294SYaroslav Brustinov
33823b8294SYaroslav Brustinovclass TimedProgressBar(threading.Thread):
34823b8294SYaroslav Brustinov    def __init__(self, time_in_secs):
35823b8294SYaroslav Brustinov        super(TimedProgressBar, self).__init__()
36823b8294SYaroslav Brustinov        self.stoprequest    = threading.Event()
37823b8294SYaroslav Brustinov        self.stopFlag       = False
38823b8294SYaroslav Brustinov        self.time_in_secs   = time_in_secs + 15 # 80 # taking 15 seconds extra
39d279c8c1SYaroslav Brustinov        widgets             = ['Running TRex: ', progressbar.Percentage(), ' ',
40823b8294SYaroslav Brustinov                   progressbar.Bar(marker='>',left='[',right=']'),
41823b8294SYaroslav Brustinov                   ' ', progressbar.ETA()]
42823b8294SYaroslav Brustinov        self.pbar           = progressbar.ProgressBar(widgets=widgets, maxval=self.time_in_secs*2)
43823b8294SYaroslav Brustinov
44823b8294SYaroslav Brustinov
45823b8294SYaroslav Brustinov    def run (self):
46823b8294SYaroslav Brustinov        # global g_stop
4789a2be82Simarom        print()
48823b8294SYaroslav Brustinov        self.pbar.start()
49823b8294SYaroslav Brustinov
50823b8294SYaroslav Brustinov        try:
51823b8294SYaroslav Brustinov            for i in range(0, self.time_in_secs*2 + 1):
52823b8294SYaroslav Brustinov                if (self.stopFlag == True):
53823b8294SYaroslav Brustinov                    break
54823b8294SYaroslav Brustinov                time.sleep(0.5)
55823b8294SYaroslav Brustinov                self.pbar.update(i)
56823b8294SYaroslav Brustinov            # self.pbar.finish()
57823b8294SYaroslav Brustinov
58823b8294SYaroslav Brustinov        except KeyboardInterrupt:
59823b8294SYaroslav Brustinov            # self.pbar.finish()
6089a2be82Simarom            print("\nInterrupted by user!!")
61823b8294SYaroslav Brustinov            self.join()
62823b8294SYaroslav Brustinov        finally:
6389a2be82Simarom            print()
64823b8294SYaroslav Brustinov
65823b8294SYaroslav Brustinov    def join(self, isPlannedStop = True, timeout=None):
66823b8294SYaroslav Brustinov        if isPlannedStop:
67823b8294SYaroslav Brustinov            self.pbar.update(self.time_in_secs*2)
68823b8294SYaroslav Brustinov            self.stopFlag = True
69823b8294SYaroslav Brustinov        else:
70823b8294SYaroslav Brustinov            self.stopFlag = True # Stop the progress bar in its current location
71823b8294SYaroslav Brustinov        self.stoprequest.set()
72823b8294SYaroslav Brustinov        super(TimedProgressBar, self).join(timeout)
73823b8294SYaroslav Brustinov
74823b8294SYaroslav Brustinov
75823b8294SYaroslav Brustinovdef timedProgressBar(time_in_secs):
76d279c8c1SYaroslav Brustinov    widgets = ['Running TRex: ', progressbar.Percentage(), ' ',
77823b8294SYaroslav Brustinov                   Bar(marker='>',left='[',right=']'),
78823b8294SYaroslav Brustinov                   ' ', progressbar.ETA()]
79823b8294SYaroslav Brustinov    pbar = progressbar.ProgressBar(widgets=widgets, maxval=time_in_secs*2)
80823b8294SYaroslav Brustinov    pbar.start()
81823b8294SYaroslav Brustinov    for i in range(0, time_in_secs*2 + 1):
82823b8294SYaroslav Brustinov        time.sleep(0.5)
83823b8294SYaroslav Brustinov        pbar.update(i)
84823b8294SYaroslav Brustinov    pbar.finish()
8589a2be82Simarom    print()
86823b8294SYaroslav Brustinov
87823b8294SYaroslav Brustinov
88