aboutsummaryrefslogtreecommitdiff
blob: e43c2afbd7d41aec0d8e8d2cdee2c7f2a573f3d3 (plain)
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
# Copyright 2005-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import time
import signal

import portage


class ProgressHandler(object):
	def __init__(self):
		self.reset()

	def reset(self):
		self.curval = 0
		self.maxval = 0
		self.last_update = 0
		self.min_display_latency = 0.2

	def onProgress(self, maxval, curval):
		self.maxval = maxval
		self.curval = curval
		cur_time = time.time()
		if cur_time - self.last_update >= self.min_display_latency:
			self.last_update = cur_time
			self.display()

	def display(self):
		raise NotImplementedError(self)


class ProgressBar(ProgressHandler):
	"""Class to set up and return a Progress Bar"""

	def __init__(self, isatty, **kwargs):
		self.isatty = isatty
		self.kwargs = kwargs
		ProgressHandler.__init__(self)
		self.progressBar = None

	def start(self):
		if self.isatty:
			self.progressBar = portage.output.TermProgressBar(**self.kwargs)
			signal.signal(signal.SIGWINCH, self.sigwinch_handler)
		else:
			self.onProgress = None
		return self.onProgress

	def set_label(self, _label):
		self.kwargs['label'] = _label

	def display(self):
		self.progressBar.set(self.curval, self.maxval)

	def sigwinch_handler(self, signum, frame):
		lines, self.progressBar.term_columns = \
			portage.output.get_term_size()

	def stop(self):
		signal.signal(signal.SIGWINCH, signal.SIG_DFL)