1 |
commit: c2778e03b73bde4cbc3e1de60872fb02560192b6 |
2 |
Author: Brian Dolbec <brian.dolbec <AT> gmail <DOT> com> |
3 |
AuthorDate: Sun Feb 6 23:38:49 2011 +0000 |
4 |
Commit: Brian Dolbec <brian.dolbec <AT> gmail <DOT> com> |
5 |
CommitDate: Sun Feb 6 23:38:49 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/overlord.git;a=commit;h=c2778e03 |
7 |
|
8 |
new simplified message class |
9 |
|
10 |
--- |
11 |
overlord/output.py | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++++ |
12 |
1 files changed, 183 insertions(+), 0 deletions(-) |
13 |
|
14 |
diff --git a/overlord/output.py b/overlord/output.py |
15 |
new file mode 100644 |
16 |
index 0000000..80a1ca0 |
17 |
--- /dev/null |
18 |
+++ b/overlord/output.py |
19 |
@@ -0,0 +1,183 @@ |
20 |
+#!/usr/bin/python |
21 |
+# -*- coding: utf-8 -*- |
22 |
+ |
23 |
+""" Copyright 2005 - 2008 Gunnar Wrobel |
24 |
+ 2011 - Brian Dolbec |
25 |
+ Distributed under the terms of the GNU General Public License v2 |
26 |
+""" |
27 |
+ |
28 |
+ |
29 |
+__version__ = "0.1" |
30 |
+ |
31 |
+ |
32 |
+import sys, inspect, types |
33 |
+ |
34 |
+from optparse import OptionGroup |
35 |
+ |
36 |
+from overlord.constants import codes |
37 |
+ |
38 |
+ |
39 |
+ |
40 |
+class Message: |
41 |
+ #FIXME: Think about some simple doctests before you modify this class the |
42 |
+ # next time. |
43 |
+ |
44 |
+ def __init__(self, |
45 |
+ out = sys.stdout, |
46 |
+ err = sys.stderr, |
47 |
+ info_level = 4, |
48 |
+ warn_level = 4, |
49 |
+ col = True |
50 |
+ ): |
51 |
+ |
52 |
+ # Where should the error output go? This can also be a file |
53 |
+ self.error_out = err |
54 |
+ |
55 |
+ # Where should the normal output go? This can also be a file |
56 |
+ self.std_out = out |
57 |
+ |
58 |
+ # The higher the level the more information you will get |
59 |
+ self.warn_lev = warn_level |
60 |
+ |
61 |
+ # The higher the level the more information you will get |
62 |
+ self.info_lev = info_level |
63 |
+ |
64 |
+ # Should the output be colored? |
65 |
+ self.set_colorize(col) |
66 |
+ |
67 |
+ self.has_error = False |
68 |
+ |
69 |
+ |
70 |
+ def color (self, col, text): |
71 |
+ return codes[col] + text + codes['reset'] |
72 |
+ |
73 |
+ |
74 |
+ def no_color (self, col, text): |
75 |
+ return text |
76 |
+ |
77 |
+ |
78 |
+ def set_colorize(self, state): |
79 |
+ if state: |
80 |
+ self.color_func = self.color |
81 |
+ else: |
82 |
+ self.color_func = self.no_color |
83 |
+ |
84 |
+ |
85 |
+ def set_info_level(self, info_level = 4): |
86 |
+ self.info_lev = info_level |
87 |
+ |
88 |
+ |
89 |
+ def info_off(self): |
90 |
+ self.set_info_level(0) |
91 |
+ |
92 |
+ |
93 |
+ def info_on(self, info_level = 4): |
94 |
+ self.set_info_level(info_level) |
95 |
+ |
96 |
+ |
97 |
+ def set_warn_level(self, warn_level = 4): |
98 |
+ self.warn_lev = warn_level |
99 |
+ |
100 |
+ |
101 |
+ def warn_off(self): |
102 |
+ self.set_warn_level(0) |
103 |
+ |
104 |
+ |
105 |
+ def warn_on(self, warn_level = 4): |
106 |
+ self.set_warn_level(warn_level) |
107 |
+ |
108 |
+ |
109 |
+ |
110 |
+ |
111 |
+ ## Output Functions |
112 |
+ |
113 |
+ def notice (self, note): |
114 |
+ print >> self.std_out, note |
115 |
+ |
116 |
+ |
117 |
+ def info (self, info, level = 4): |
118 |
+ |
119 |
+ #print "info =", info |
120 |
+ |
121 |
+ if type(info) not in types.StringTypes: |
122 |
+ info = str(info) |
123 |
+ |
124 |
+ if level > self.info_lev: |
125 |
+ return |
126 |
+ |
127 |
+ for i in info.split('\n'): |
128 |
+ print >> self.std_out, self.set_colorize('green', '* ') + i |
129 |
+ |
130 |
+ |
131 |
+ def status (self, message, status, info = 'ignored'): |
132 |
+ |
133 |
+ if type(message) not in types.StringTypes: |
134 |
+ message = str(message) |
135 |
+ |
136 |
+ lines = message.split('\n') |
137 |
+ |
138 |
+ if not lines: |
139 |
+ return |
140 |
+ |
141 |
+ for i in lines[0:-1]: |
142 |
+ print >> self.std_out, self.set_colorize('green', '* ') + i |
143 |
+ |
144 |
+ i = lines[-1] |
145 |
+ |
146 |
+ if len(i) > 58: |
147 |
+ i = i[0:57] |
148 |
+ |
149 |
+ if status == 1: |
150 |
+ result = '[' + self.set_colorize('green', 'ok') + ']' |
151 |
+ elif status == 0: |
152 |
+ result = '[' + self.set_colorize('red', 'failed') + ']' |
153 |
+ else: |
154 |
+ result = '[' + self.set_colorize('yellow', info) + ']' |
155 |
+ |
156 |
+ print >> self.std_out, self.set_colorize('green', '* ') + i + ' ' + \ |
157 |
+ '.' * (58 - len(i)) + ' ' + result |
158 |
+ |
159 |
+ |
160 |
+ def warn (self, warn, level = 4): |
161 |
+ |
162 |
+ #print "DEBUG.warn()" |
163 |
+ |
164 |
+ if type(warn) not in types.StringTypes: |
165 |
+ warn = str(warn) |
166 |
+ |
167 |
+ if level > self.warn_lev: |
168 |
+ return |
169 |
+ |
170 |
+ for i in warn.split('\n'): |
171 |
+ print >> self.std_out, self.set_colorize('yellow', '* ') + i |
172 |
+ |
173 |
+ |
174 |
+ def error (self, error): |
175 |
+ |
176 |
+ if type(error) not in types.StringTypes: |
177 |
+ error = str(error) |
178 |
+ |
179 |
+ for i in error.split('\n'): |
180 |
+ # NOTE: Forced flushing ensures that stdout and stderr |
181 |
+ # stay in nice order. This is a workaround for calls like |
182 |
+ # "overlord -L |& less". |
183 |
+ sys.stdout.flush() |
184 |
+ print >> self.error_out, self.set_colorize('red', '* ') + i |
185 |
+ self.error_out.flush() |
186 |
+ self.has_error = True |
187 |
+ |
188 |
+ |
189 |
+ def die (self, error): |
190 |
+ |
191 |
+ if type(error) not in types.StringTypes: |
192 |
+ error = str(error) |
193 |
+ |
194 |
+ for i in error.split('\n'): |
195 |
+ self.error(self.set_colorize('red', 'Fatal error: ') + i) |
196 |
+ self.error(self.set_colorize('red', 'Fatal error(s) - aborting')) |
197 |
+ sys.exit(1) |
198 |
+ |
199 |
+ |
200 |
+ |
201 |
+## gloabal message handler |
202 |
+OUT = Message() |