2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
10 # | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # tails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
27 """Utiliy module for holding generic methods that implement handling
28 of console input / output"""
35 # NOTE: This is a hack! We abuse the global logger just to pass around the
37 logger
= cmk
.log
.get_logger("base")
40 # Generic / low level functions
43 # would rather use "def output(text, *args, stream=sys.stdout)", but this is not possible
45 def output(text
, *args
, **kwargs
):
49 stream
= kwargs
.get("stream", sys
.stdout
)
55 # TODO: Way to generic!
56 pass # avoid exception on broken pipe (e.g. due to | head)
59 # Output text if opt_verbose is set (-v). Adds no linefeed
60 def verbose(text
, *args
, **kwargs
):
61 if logger
.is_verbose():
62 output(text
, *args
, **kwargs
)
65 # Output text if, opt_verbose >= 2 (-vv).
66 def vverbose(text
, *args
, **kwargs
):
67 if logger
.is_very_verbose():
68 verbose(text
, *args
, **kwargs
)
72 # More top level wrappers
75 # TODO: Inconsistent -> Adds newline and other functions don't
76 def warning(text
, *args
, **kwargs
):
77 kwargs
.setdefault("stream", sys
.stderr
)
79 stripped
= text
.lstrip()
80 indent
= text
[:len(text
) - len(stripped
)]
82 text
= "%s%s%sWARNING:%s %s\n" % (indent
, tty
.bold
, tty
.yellow
, tty
.normal
, stripped
)
84 output(text
, *args
, **kwargs
)
87 def error(text
, *args
):
88 output(text
, *args
, stream
=sys
.stderr
)
91 def section_begin(text
, *args
, **kwargs
):
92 verbose(tty
.bold
+ text
+ tty
.normal
+ ":\n")
95 def section_success(text
):
96 verbose("%sSUCCESS%s - %s\n" % (tty
.green
, tty
.normal
, text
))
99 def section_error(text
):
100 verbose("%sERROR%s - %s\n" % (tty
.red
, tty
.normal
, text
))
104 verbose("%s+%s %s\n" % (tty
.yellow
, tty
.normal
, text
.upper()))
107 def step_error(text
):