Licenses: Updated the list of licenses and added a PDF containing all license texts
[check_mk.git] / cmk_base / console.py
bloba3f61958495fc0d2399cfeee901761dabe280270
1 #!/usr/bin/env python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
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.
26 """Utiliy module for holding generic methods that implement handling
27 of console input / output"""
29 import sys
31 import cmk.utils.tty as tty
32 import cmk.utils.log
34 # NOTE: This is a hack! We abuse the global logger just to pass around the
35 # verbosity setting.
36 logger = cmk.utils.log.get_logger("base")
39 # Generic / low level functions
43 # would rather use "def output(text, *args, stream=sys.stdout)", but this is not possible
44 # with python 2.7
45 def output(text, *args, **kwargs):
46 if args:
47 text = text % args
49 stream = kwargs.get("stream", sys.stdout)
51 try:
52 stream.write(text)
53 stream.flush()
54 except:
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
76 # TODO: Inconsistent -> Adds newline and other functions don't
77 def warning(text, *args, **kwargs):
78 kwargs.setdefault("stream", sys.stderr)
80 stripped = text.lstrip()
81 indent = text[:len(text) - len(stripped)]
83 text = "%s%s%sWARNING:%s %s\n" % (indent, tty.bold, tty.yellow, tty.normal, stripped)
85 output(text, *args, **kwargs)
88 def error(text, *args):
89 output(text, *args, stream=sys.stderr)
92 def section_begin(text, *args, **kwargs):
93 verbose(tty.bold + text + tty.normal + ":\n")
96 def section_success(text):
97 verbose("%sSUCCESS%s - %s\n" % (tty.green, tty.normal, text))
100 def section_error(text):
101 verbose("%sERROR%s - %s\n" % (tty.red, tty.normal, text))
104 def step(text):
105 verbose("%s+%s %s\n" % (tty.yellow, tty.normal, text.upper()))
108 def step_error(text):
109 verbose(text + "\n")