~ahasenack/landscape-client/landscape-client-1.5.5-0ubuntu0.9.04.0

« back to all changes in this revision

Viewing changes to landscape/package/interface.py

  • Committer: Bazaar Package Importer
  • Author(s): Rick Clark
  • Date: 2008-09-08 16:35:57 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080908163557-l3ixzj5dxz37wnw2
Tags: 1.0.18-0ubuntu1
New upstream release 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import logging
 
2
import types
 
3
import sys
 
4
 
 
5
from smart.interface import Interface
 
6
from smart.const import ERROR, WARNING, INFO, DEBUG
 
7
 
 
8
import smart.interfaces
 
9
 
 
10
 
 
11
class LandscapeInterface(Interface):
 
12
 
 
13
    __output = ""
 
14
    __failed = False
 
15
 
 
16
    def reset_for_landscape(self):
 
17
        """Reset output and failed flag."""
 
18
        self.__failed = False
 
19
        self.__output = u""
 
20
 
 
21
    def get_output_for_landscape(self):
 
22
        """showOutput() is cached, and returned by this method."""
 
23
        return self.__output
 
24
 
 
25
    def has_failed_for_landscape(self):
 
26
        """Return true if any error() messages were logged."""
 
27
        return self.__failed
 
28
 
 
29
    def error(self, msg):
 
30
        self.__failed = True
 
31
        # Calling these logging.* functions here instead of message()
 
32
        # below will output the message or not depending on the debug
 
33
        # level set in landscape-client, rather than the one set in
 
34
        # Smart's configuration.
 
35
        logging.error("[Smart] %s", msg)
 
36
        super(LandscapeInterface, self).error(msg)
 
37
 
 
38
    def warning(self, msg):
 
39
        logging.warning("[Smart] %s", msg)
 
40
        super(LandscapeInterface, self).warning(msg)
 
41
 
 
42
    def info(self, msg):
 
43
        logging.info("[Smart] %s", msg)
 
44
        super(LandscapeInterface, self).info(msg)
 
45
 
 
46
    def debug(self, msg):
 
47
        logging.debug("[Smart] %s", msg)
 
48
        super(LandscapeInterface, self).debug(msg)
 
49
 
 
50
    def message(self, level, msg):
 
51
        prefix = {ERROR: "ERROR", WARNING: "WARNING",
 
52
                  INFO: "INFO", DEBUG: "DEBUG"}.get(level)
 
53
        self.showOutput("%s: %s\n" % (prefix, msg))
 
54
 
 
55
    def showOutput(self, output):
 
56
        if not isinstance(output, unicode):
 
57
            try:
 
58
                output = output.decode("utf-8")
 
59
            except UnicodeDecodeError:
 
60
                output = output.decode("ascii", "replace")
 
61
        self.__output += output
 
62
 
 
63
 
 
64
 
 
65
 
 
66
class LandscapeInterfaceModule(types.ModuleType):
 
67
 
 
68
    def __init__(self):
 
69
        super(LandscapeInterfaceModule, self).__init__("landscape")
 
70
 
 
71
    def create(self, ctrl, command=None, argv=None):
 
72
        return LandscapeInterface(ctrl)
 
73
 
 
74
 
 
75
def install_landscape_interface():
 
76
    if "smart.interfaces.landscape" not in sys.modules:
 
77
        # Plug the interface in a place Smart will recognize.
 
78
        smart.interfaces.landscape = LandscapeInterfaceModule()
 
79
        sys.modules["smart.interfaces.landscape"] = smart.interfaces.landscape
 
80
 
 
81
def uninstall_landscape_interface():
 
82
    sys.modules.pop("smart.interfaces.landscape", None)