| 1 | #! /usr/bin/env python |
|---|
| 2 | # -*- coding: utf8 -*- |
|---|
| 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 2 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 | class BaseMessage: |
|---|
| 26 | """ |
|---|
| 27 | Base class for console output. |
|---|
| 28 | """ |
|---|
| 29 | |
|---|
| 30 | def __init__(self, message): |
|---|
| 31 | """ |
|---|
| 32 | Constructor. |
|---|
| 33 | |
|---|
| 34 | @type message: str |
|---|
| 35 | @param message: The message to print on the Console. |
|---|
| 36 | """ |
|---|
| 37 | |
|---|
| 38 | self.message = message |
|---|
| 39 | print '[*] %s' % (str(self.message)) |
|---|
| 40 | |
|---|
| 41 | class BaseFailureMessage(BaseMessage): |
|---|
| 42 | """ |
|---|
| 43 | Base class for failure messages. |
|---|
| 44 | """ |
|---|
| 45 | |
|---|
| 46 | def __init__(self, debug, caller, message, type): |
|---|
| 47 | """ |
|---|
| 48 | Constructor. |
|---|
| 49 | |
|---|
| 50 | @type debug: bool |
|---|
| 51 | @param debug: Whether the L{caller} arguments will be printed. |
|---|
| 52 | |
|---|
| 53 | @type caller: tuple |
|---|
| 54 | @param caller: Specifies the class and method were the failure ocurred. |
|---|
| 55 | |
|---|
| 56 | @type message: str |
|---|
| 57 | @param message: The message to print. |
|---|
| 58 | |
|---|
| 59 | @type type: str |
|---|
| 60 | @param type: The type of failure: 'WARNING', 'ERROR', 'DEBUG'. |
|---|
| 61 | """ |
|---|
| 62 | |
|---|
| 63 | self.caller = '.'.join(caller) |
|---|
| 64 | self.message = message |
|---|
| 65 | self.debug = debug |
|---|
| 66 | self.type = type |
|---|
| 67 | |
|---|
| 68 | if self.debug: |
|---|
| 69 | self.message = ' '.join((self.caller, self.message)) |
|---|
| 70 | |
|---|
| 71 | print '[*] %s: %s' % (str(self.type), str(self.message)) |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | class Console: |
|---|
| 75 | """ |
|---|
| 76 | Console I/O handler organized by message type. Also handle GUI logging when passed an instance. |
|---|
| 77 | """ |
|---|
| 78 | |
|---|
| 79 | def __init__(self, itakaglobals): |
|---|
| 80 | """ailuretype == 'ERROR': |
|---|
| 81 | |
|---|
| 82 | Constructor for console output handler. |
|---|
| 83 | |
|---|
| 84 | @type itakaglobals: module |
|---|
| 85 | @param itakaglobals: Configuration module. |
|---|
| 86 | """ |
|---|
| 87 | |
|---|
| 88 | self.itakaglobals = itakaglobals |
|---|
| 89 | |
|---|
| 90 | if self.itakaglobals.output['debug']: |
|---|
| 91 | BaseMessage('Itaka %s (%s) starting' % (itakaglobals.version, itakaglobals.revision)) |
|---|
| 92 | elif self.itakaglobals.output['normal']: |
|---|
| 93 | BaseMessage('Itaka %s starting' % (itakaglobals.version)) |
|---|
| 94 | |
|---|
| 95 | def __del__(self): |
|---|
| 96 | """ |
|---|
| 97 | Destructor. |
|---|
| 98 | """ |
|---|
| 99 | |
|---|
| 100 | if self.itakaglobals.output['normal']: |
|---|
| 101 | BaseMessage('Itaka shutting down') |
|---|
| 102 | |
|---|
| 103 | def message(self, message): |
|---|
| 104 | """ |
|---|
| 105 | Message handler. |
|---|
| 106 | |
|---|
| 107 | @type message: str |
|---|
| 108 | @param message: Message to print to the console. |
|---|
| 109 | """ |
|---|
| 110 | |
|---|
| 111 | if self.itakaglobals.output['normal']: |
|---|
| 112 | BaseMessage(message) |
|---|
| 113 | |
|---|
| 114 | def failure(self, caller, message, failuretype='ERROR'): |
|---|
| 115 | """ |
|---|
| 116 | Failure handler abstract. |
|---|
| 117 | |
|---|
| 118 | @type caller: tuple |
|---|
| 119 | @param caller: Specifies the class and method were the warning ocurred. |
|---|
| 120 | |
|---|
| 121 | @type message: str |
|---|
| 122 | @param message: Message to print to the console. |
|---|
| 123 | |
|---|
| 124 | @type failuretype: str |
|---|
| 125 | @param failuretype: What kind of failure it is, either 'ERROR' (default), 'WARNING' or 'DEBUG' |
|---|
| 126 | """ |
|---|
| 127 | |
|---|
| 128 | if failuretype == 'ERROR': |
|---|
| 129 | if not self.itakaglobals.output['quiet']: |
|---|
| 130 | BaseFailureMessage(self.itakaglobals.output['quiet'], caller, message, failuretype) |
|---|
| 131 | |
|---|
| 132 | elif failuretype == 'WARNING': |
|---|
| 133 | if self.itakaglobals.output['normal']: |
|---|
| 134 | BaseFailureMessage(self.itakaglobals.output['normal'], caller, message, failuretype) |
|---|
| 135 | |
|---|
| 136 | elif failuretype == 'DEBUG': |
|---|
| 137 | if self.itakaglobals.output['debug']: |
|---|
| 138 | BaseFailureMessage(self.itakaglobals.output['debug'], caller, message, failuretype) |
|---|
| 139 | |
|---|