2 # -*- coding: utf-8 -*-
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
16 # @author: David Blaisonneau <david.blaisonneau@orange.com>
17 # @author: Arnaud Morin <arnaud1.morin@orange.com>
22 class OpenSteakPrinter:
23 """ Just a nice message printer """
35 def header(self, msg):
37 Print a header for a block
39 @param msg: The message to print in the header (limited to 78 chars)
46 """.format(msg[0:78]))
48 def config(self, msg, name, value=None, indent=0):
50 Print a line with the value of a parameter
52 @param msg: The message to print in the header (limited to 78 chars)
53 @param name: The name of the prameter
54 @param value: The value of the parameter
55 @param indent: Tab size at the beginning of the line
58 ind = ' ' * indent * self.TABSIZE
60 print('{} - {} = {}'.format(ind, msg, name))
62 print('{} [{}KO{}] {} > {} (NOT found)'.
63 format(ind, self.FAIL, self.ENDC, msg, name))
65 print('{} [{}OK{}] {} > {} = {}'.
66 format(ind, self.OKGREEN, self.ENDC, msg, name, str(value)))
68 def list(self, msg, indent=0):
72 @param msg: The message to print in the header (limited to 78 chars)
73 @param indent: Tab size at the beginning of the line
76 print(' ' * indent * self.TABSIZE, '-', msg)
78 def list_id(self, dic, indent=0):
80 Print a list of dict items
82 @param dic: The dict to print
83 @param indent: Tab size at the beginning of the line
86 for (k, v) in dic.items():
87 self.list("{}: {}".format(k, v), indent=indent)
89 def status(self, res, msg, failed="", eol="\n", quit=True, indent=0):
92 - OK/KO if the result is a boolean
93 - Else the result text
95 @param res: The status to show
96 @param msg: The message to show
97 @param eol: End of line
98 @param quit: Exit the system in case of failure
99 @param indent: Tab size at the beginning of the line
102 ind = ' ' * indent * self.TABSIZE
104 msg = '{} [{}OK{}] {}'.format(ind, self.OKGREEN, self.ENDC, msg)
106 msg = '{} [{}{}{}] {}'.format(ind, self.OKBLUE, res,
109 msg = '{} [{}KO{}] {}'.format(ind, self.FAIL, self.ENDC, msg)
111 msg += '\n > {}'.format(failed)
112 msg = msg.ljust(140) + eol
113 sys.stdout.write(msg)
114 if res is False and quit is True:
117 def ask_validation(self, prompt=None, resp=False):
118 """ Function ask_validation
119 Ask a validation message
121 @param prompt: The question to ask ('Continue ?') if None
122 @param resp: The default value (Default is False)
123 @return RETURN: Trie or False
126 prompt = 'Continue ?'
128 prompt += ' [{}Y{}/n]: '.format(self.BOLD, self.ENDC)
130 prompt += ' [y/{}N{}]: '.format(self.BOLD, self.ENDC)
134 ans = 'y' if resp else 'n'
135 if ans not in ['y', 'Y', 'n', 'N']:
136 print('please enter y or n.')
138 if ans == 'y' or ans == 'Y':
140 if ans == 'n' or ans == 'N':