| 1 | #! /usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | # |
|---|
| 4 | # Itaka is free software; you can redistribute it and/or modify |
|---|
| 5 | # it under the terms of the GNU General Public License as published by |
|---|
| 6 | # the Free Software Foundation; either version 3 of the License, or |
|---|
| 7 | # any later version. |
|---|
| 8 | # |
|---|
| 9 | # Itaka is distributed in the hope that it will be useful, |
|---|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | # GNU General Public License for more details. |
|---|
| 13 | # |
|---|
| 14 | # You should have received a copy of the GNU General Public License |
|---|
| 15 | # along with Itaka; if not, write to the Free Software |
|---|
| 16 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 17 | # |
|---|
| 18 | # Copyright 2003-2009 Marc E. |
|---|
| 19 | # http://itaka.jardinpresente.com.ar |
|---|
| 20 | # |
|---|
| 21 | # $Id$ |
|---|
| 22 | |
|---|
| 23 | """ Itaka console output and logging engine """ |
|---|
| 24 | |
|---|
| 25 | import __builtin__ |
|---|
| 26 | |
|---|
| 27 | # Global output functions |
|---|
| 28 | def print_message(string): |
|---|
| 29 | """ |
|---|
| 30 | Print wrapper. |
|---|
| 31 | |
|---|
| 32 | @type str: str |
|---|
| 33 | @param str: Message |
|---|
| 34 | """ |
|---|
| 35 | |
|---|
| 36 | print '[*] %s' % (str(string)) |
|---|
| 37 | |
|---|
| 38 | def print_warning(*strings): |
|---|
| 39 | """ |
|---|
| 40 | Print warning wrapper. |
|---|
| 41 | |
|---|
| 42 | @type *strings: anything |
|---|
| 43 | @param *strings: Anything |
|---|
| 44 | """ |
|---|
| 45 | |
|---|
| 46 | print_message('WARNING: %s' % (" - ".join(str(item) for item in strings))) |
|---|
| 47 | |
|---|
| 48 | def print_error(*strings): |
|---|
| 49 | """ |
|---|
| 50 | Print error wrapper. |
|---|
| 51 | |
|---|
| 52 | @type *strings: anything |
|---|
| 53 | @param *strings: Anything |
|---|
| 54 | """ |
|---|
| 55 | |
|---|
| 56 | print_message('ERROR: %s' % (" - ".join(str(item) for item in strings))) |
|---|
| 57 | |
|---|
| 58 | def print_debug(*strings): |
|---|
| 59 | """ |
|---|
| 60 | Print debug wrapper. |
|---|
| 61 | |
|---|
| 62 | @type *strings: anything |
|---|
| 63 | @param *strings: Anything |
|---|
| 64 | """ |
|---|
| 65 | |
|---|
| 66 | print_message('DEBUG: %s' % (" - ".join(str(item) for item in strings))) |
|---|
| 67 | |
|---|
| 68 | def print_type(*strings): |
|---|
| 69 | """ |
|---|
| 70 | Print type wrapper. |
|---|
| 71 | |
|---|
| 72 | @type *strings: anything |
|---|
| 73 | @param *strings: Anything |
|---|
| 74 | """ |
|---|
| 75 | |
|---|
| 76 | print_message('DEBUG: %s' % (" - ".join(str(type(item)) for item in strings))) |
|---|
| 77 | |
|---|
| 78 | def print_dir(*strings): |
|---|
| 79 | """ |
|---|
| 80 | Print dir wrapper. |
|---|
| 81 | |
|---|
| 82 | @type *strings: anything |
|---|
| 83 | @param *strings: Anything |
|---|
| 84 | """ |
|---|
| 85 | |
|---|
| 86 | print_message('DEBUG: %s' % (" - ".join(str(dir(item)) for item in strings))) |
|---|
| 87 | |
|---|
| 88 | # Register them for global use |
|---|
| 89 | __builtin__.print_m = print_message |
|---|
| 90 | __builtin__.print_e = print_error |
|---|
| 91 | __builtin__.print_w = print_warning |
|---|
| 92 | __builtin__.print_d = print_debug |
|---|
| 93 | __builtin__.print_t = print_type |
|---|
| 94 | __builtin__.print_dir = print_dir |
|---|
| 95 | |
|---|
| 96 | class BaseMessage: |
|---|
| 97 | """ |
|---|
| 98 | Base class for console output. |
|---|
| 99 | """ |
|---|
| 100 | |
|---|
| 101 | def __init__(self, message): |
|---|
| 102 | """ |
|---|
| 103 | Constructor. |
|---|
| 104 | |
|---|
| 105 | @type message: str |
|---|
| 106 | @param message: The message to print on the Console |
|---|
| 107 | """ |
|---|
| 108 | |
|---|
| 109 | self.message = message |
|---|
| 110 | print_message(self.message) |
|---|
| 111 | |
|---|
| 112 | class BaseFailureMessage(BaseMessage): |
|---|
| 113 | """ |
|---|
| 114 | Base class for failure messages. |
|---|
| 115 | """ |
|---|
| 116 | |
|---|
| 117 | def __init__(self, debug, caller, message, type): |
|---|
| 118 | """ |
|---|
| 119 | Constructor |
|---|
| 120 | |
|---|
| 121 | @type debug: bool |
|---|
| 122 | @param debug: Whether the L{caller} arguments will be printed |
|---|
| 123 | |
|---|
| 124 | @type caller: tuple |
|---|
| 125 | @param caller: Specifies the class and method were the failure ocurred |
|---|
| 126 | |
|---|
| 127 | @type message: str |
|---|
| 128 | @param message: The message to print |
|---|
| 129 | |
|---|
| 130 | @type type: str |
|---|
| 131 | @param type: The type of failure: 'WARNING', 'ERROR', 'DEBUG' |
|---|
| 132 | """ |
|---|
| 133 | |
|---|
| 134 | self.caller = '.'.join(caller) |
|---|
| 135 | self.message = message |
|---|
| 136 | self.debug = debug |
|---|
| 137 | self.type = type |
|---|
| 138 | |
|---|
| 139 | if self.debug: |
|---|
| 140 | self.message = ' '.join((self.caller, self.message)) |
|---|
| 141 | |
|---|
| 142 | print '[*] %s: %s' % (str(self.type), str(self.message)) |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | class Console: |
|---|
| 146 | """ |
|---|
| 147 | Console I/O handler organized by message type. Also handle GUI logging when passed an instance |
|---|
| 148 | """ |
|---|
| 149 | |
|---|
| 150 | def __init__(self, itaka_globals): |
|---|
| 151 | """ |
|---|
| 152 | Constructor for console output handler |
|---|
| 153 | |
|---|
| 154 | @type itaka_globals: module |
|---|
| 155 | @param itaka_globals: Configuration module globals |
|---|
| 156 | """ |
|---|
| 157 | |
|---|
| 158 | self.itaka_globals = itaka_globals |
|---|
| 159 | if self.itaka_globals.console_verbosity['debug']: |
|---|
| 160 | BaseMessage(_('Itaka %(version)s (r%(revision)s) starting') % {'version': self.itaka_globals.__version__, 'revision': self.itaka_globals.__revision__.split()[1]}) |
|---|
| 161 | elif self.itaka_globals.console_verbosity['normal']: |
|---|
| 162 | BaseMessage(_('Itaka %s starting') % (self.itaka_globals.__version__)) |
|---|
| 163 | |
|---|
| 164 | def message(self, message): |
|---|
| 165 | """ |
|---|
| 166 | Message handler |
|---|
| 167 | |
|---|
| 168 | @type message: str |
|---|
| 169 | @param message: Message to print to the console |
|---|
| 170 | """ |
|---|
| 171 | |
|---|
| 172 | if self.itaka_globals.console_verbosity['normal']: |
|---|
| 173 | BaseMessage(message) |
|---|
| 174 | |
|---|
| 175 | def failure(self, caller, message, failure_type='ERROR'): |
|---|
| 176 | """ |
|---|
| 177 | Failure handler abstract |
|---|
| 178 | |
|---|
| 179 | @type caller: tuple |
|---|
| 180 | @param caller: Specifies the class and method were the warning ocurred |
|---|
| 181 | |
|---|
| 182 | @type message: str |
|---|
| 183 | @param message: Message to print to the console |
|---|
| 184 | |
|---|
| 185 | @type failure_type: str |
|---|
| 186 | @param failure_type: What kind of failure it is, either 'ERROR' (default), 'WARNING' or 'DEBUG' |
|---|
| 187 | """ |
|---|
| 188 | |
|---|
| 189 | if failure_type == 'ERROR': |
|---|
| 190 | if not self.itaka_globals.console_verbosity['quiet']: |
|---|
| 191 | BaseFailureMessage(self.itaka_globals.console_verbosity['quiet'], caller, message, failure_type) |
|---|
| 192 | |
|---|
| 193 | elif failure_type == 'WARNING': |
|---|
| 194 | if self.itaka_globals.console_verbosity['normal']: |
|---|
| 195 | BaseFailureMessage(self.itaka_globals.console_verbosity['normal'], caller, message, failure_type) |
|---|
| 196 | |
|---|
| 197 | elif failure_type == 'DEBUG': |
|---|
| 198 | if self.itaka_globals.console_verbosity['debug']: |
|---|
| 199 | BaseFailureMessage(self.itaka_globals.console_verbosity['debug'], caller, message, failure_type) |
|---|
| 200 | |
|---|