aboutsummaryrefslogtreecommitdiff
blob: 9fec3b6932f480e08c8b1a5b2514cae91680a323 (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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    logger.py
    ~~~~~~~~~
    
    logging classes
    
    :copyright: (c) 2013 by Jauhien Piatlicki
    :license: GPL-2, see LICENSE for more details.
"""

import sys

import portage


class Logger(object):
    """
    A simple logger object. Uses portage out facilities.
    """
    def __init__(self):
        self.out = portage.output.EOutput()

    def error(self, message):
        self.out.eerror(message)

    def info(self, message):
        self.out.einfo(message)

    def warn(self, message):
        self.out.ewarn(message)


class ProgressBar(object):
    """
    A progress bar for CLI
    """

    __slots__ = ('length', 'total', 'processed', 'chars')
    
    def __init__(self, length, total, processed = 0):
        """
        Args:
            length: Length of the progress bar.
            total: The overall number of items to process.
            processe: Number of processed items.
        """
        self.length = length
        self.total = total
        self.chars = ['-', '\\', '|', '/']
        self.processed = processed

    def begin(self):
        """
        Start displaying the progress bar with 0% progress.
        """
        self.processed = 0
        self.display()

    def display(self, processed = None):
        """
        Show the progress bar with current progress.

        Args:
            processed: Number of processed items.
        """
        if processed:
            self.processed = processed

        show = self.chars[self.processed % 4]
        percent = (self.processed * 100)//self.total
        progress = (percent * self.length)//100
        blank = self.length - progress
        sys.stderr.write("\r %s [%s%s] %s%%" % \
                             (show, "#" * progress, " " * blank, percent))
        sys.stderr.flush()

    def increment(self, count = 1):
        """
        Increment number of processed items.

        Args:
            count: Step of incrementation.
        """
        self.processed += count
        self.display()

    def end(self):
        """
        Show 100%.
        """
        self.processed = self.total
        self.display()