summaryrefslogtreecommitdiffstats
path: root/layman/output.py
blob: c90fccc7b42fbcdaf1f5826d4e748e1336c5de4e (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/python
# -*- coding: utf-8 -*-

""" Copyright 2005 - 2008 Gunnar Wrobel
              2011 - Brian Dolbec
 Distributed under the terms of the GNU General Public License v2
"""


__version__ = "0.1"


import sys, types

from layman.constants import codes, INFO_LEVEL, WARN_LEVEL, DEBUG_LEVEL, OFF
from layman.compatibility import encode


class MessageBase(object):
    """Base Message class helper functions and variables
    """

    def __init__(self,
                 out = sys.stdout,
                 err = sys.stderr,
                 info_level = INFO_LEVEL,
                 warn_level = WARN_LEVEL,
                 col = True,
                 error_callback=None
                 ):
        # Where should the error output go? This can also be a file
        if isinstance(err, file):
            self.error_out = err
        else:
            raise Exception("MessageBase: input parameter 'err' must be of type: file")

        # Where should the normal output go? This can also be a file
        if isinstance(out, file):
            self.std_out = out
        else:
            raise Exception("MessageBase: input parameter 'out' must be of type: file")

        # The higher the level the more information you will get
        self.warn_lev = warn_level

        # The higher the level the more information you will get
        self.info_lev = info_level

        # Should the output be colored?
        self.color_func = None
        self.set_colorize(col)

        self.debug_lev = OFF

        # callback function that gets passed any error messages
        # that have shown up.
        self.error_callback = error_callback
        self.block_callback = False


    def _color (self, col, text):
        return codes[col] + text + codes['reset']


    def _no_color (self, col, text):
        return text


    def set_colorize(self, state):
        if state:
            self.color_func = self._color
        else:
            self.color_func = self._no_color


    def set_info_level(self, info_level = INFO_LEVEL):
        self.info_lev = info_level


    def set_warn_level(self, warn_level = WARN_LEVEL):
        self.warn_lev = warn_level


    def set_debug_level(self, debugging_level = DEBUG_LEVEL):
        self.debug_lev = debugging_level

    def do_error_callback(self, error):
        """runs the error_callback function with the error
        that occurred
        """
        if self.error_callback is not None and not self.block_callback:
            self.error_callback(error)


class Message(MessageBase):
    """Primary Message output methods
    """
    #FIXME: Think about some simple doctests before you modify this class the
    #       next time.

    def __init__(self,
                 out = sys.stdout,
                 err = sys.stderr,
                 info_level = INFO_LEVEL,
                 warn_level = WARN_LEVEL,
                 col = True,
                 error_callback = None
                ):

        MessageBase.__init__(self, out, err, info_level, warn_level,
            col, error_callback)


    ## Output Functions

    def debug(self, info, level = OFF):
        """empty debug function, does nothing,
        declared here for compatibility with DebugMessage
        """
        if type(info) != str:#not in types.StringTypes:
            info = encode(info)

        if level > self.debug_lev:
            return

        for i in info.split('\n'):
            print  >> self.std_out, self.color_func('yellow', 'DEBUG: ') + i


    def notice (self, note):
        print >> self.std_out, note


    def info (self, info, level = INFO_LEVEL):

        if type(info) != str:#not in types.StringTypes:
            info = encode(info)

        if level > self.info_lev:
            return

        for i in info.split('\n'):
            print  >> self.std_out, " %s %s" % (self.color_func('green', '*'),i)


    def status (self, message, status, info = 'ignored'):

        if type(message) != str:#not in types.StringTypes:
            message = encode(message)

        lines = message.split('\n')

        if not len(lines):
            return

        for i in lines[0:-1]:
            print >> self.std_out, " %s %s" % (self.color_func('green', '*'),i)

        i = lines[-1]

        if len(i) > 58:
            i = i[0:57]

        if status == 1:
            result = '[' + self.color_func('green', 'ok') + ']'
        elif status == 0:
            result = '[' + self.color_func('red', 'failed') + ']'
        else:
            result = '[' + self.color_func('yellow', info) + ']'

        print >> " %s %s %s %S" % (self.color_func('green', '*'), i,
            ('.' * (58 - len(i))), result)


    def warn (self, warn, level = WARN_LEVEL):

        if type(warn) != str:#not in types.StringTypes:
            warn = encode(warn)

        if level > self.warn_lev:
            return

        for i in warn.split('\n'):
            print >> self.std_out, " %s %s" % (self.color_func('yellow', '*'),i)


    def error (self, error):

        if type(error) != str:#not in types.StringTypes:
            error = encode(error)

        for i in error.split('\n'):
            # NOTE: Forced flushing ensures that stdout and stderr
            # stay in nice order.  This is a workaround for calls like
            # "layman -L |& less".
            self.std_out.flush()
            self.error_out.flush()
            print >> self.std_out, " %s %s" % (self.color_func('red', '*'), i)
            self.std_out.flush()
        self.do_error_callback(error)


    def die (self, error):

        if type(error) != str:#not in types.StringTypes:
            error = encode(error)

        for i in error.split('\n'):
            self.error(self.color_func('red', 'Fatal error: ') + i)
        self.error(self.color_func('red', 'Fatal error(s) - aborting'))
        sys.exit(1)