5
from smart.interface import Interface
6
from smart.const import ERROR, WARNING, INFO, DEBUG
8
import smart.interfaces
11
class LandscapeInterface(Interface):
16
def reset_for_landscape(self):
17
"""Reset output and failed flag."""
21
def get_output_for_landscape(self):
22
"""showOutput() is cached, and returned by this method."""
25
def has_failed_for_landscape(self):
26
"""Return true if any error() messages were logged."""
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)
38
def warning(self, msg):
39
logging.warning("[Smart] %s", msg)
40
super(LandscapeInterface, self).warning(msg)
43
logging.info("[Smart] %s", msg)
44
super(LandscapeInterface, self).info(msg)
47
logging.debug("[Smart] %s", msg)
48
super(LandscapeInterface, self).debug(msg)
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))
55
def showOutput(self, output):
56
if not isinstance(output, unicode):
58
output = output.decode("utf-8")
59
except UnicodeDecodeError:
60
output = output.decode("ascii", "replace")
61
self.__output += output
66
class LandscapeInterfaceModule(types.ModuleType):
69
super(LandscapeInterfaceModule, self).__init__("landscape")
71
def create(self, ctrl, command=None, argv=None):
72
return LandscapeInterface(ctrl)
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
81
def uninstall_landscape_interface():
82
sys.modules.pop("smart.interfaces.landscape", None)