Merge "Remove Compass from genesis."
[genesis.git] / opensteak / tools / opensteak / printer.py
1 #!/usr/bin/python3
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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
13 # under the License.
14 #
15 # Authors:
16 # @author: David Blaisonneau <david.blaisonneau@orange.com>
17 # @author: Arnaud Morin <arnaud1.morin@orange.com>
18
19 import sys
20
21
22 class OpenSteakPrinter:
23     """ Just a nice message printer """
24     HEADER = '\033[95m'
25     OKBLUE = '\033[94m'
26     OKGREEN = '\033[92m'
27     WARNING = '\033[93m'
28     FAIL = '\033[91m'
29     ENDC = '\033[0m'
30     BOLD = '\033[1m'
31     UNDERLINE = '\033[4m'
32
33     TABSIZE = 4
34
35     def header(self, msg):
36         """ Function header
37         Print a header for a block
38
39         @param msg: The message to print in the header (limited to 78 chars)
40         @return RETURN: None
41         """
42         print("""
43 #
44 # {}
45 #
46 """.format(msg[0:78]))
47
48     def config(self, msg, name, value=None, indent=0):
49         """ Function config
50         Print a line with the value of a parameter
51
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
56         @return RETURN: None
57         """
58         ind = ' ' * indent * self.TABSIZE
59         if value is None:
60             print('{} - {} = {}'.format(ind, msg, name))
61         elif value is False:
62             print('{} [{}KO{}] {} > {} (NOT found)'.
63                   format(ind, self.FAIL, self.ENDC, msg, name))
64         else:
65             print('{} [{}OK{}] {} > {} = {}'.
66                   format(ind, self.OKGREEN, self.ENDC, msg, name, str(value)))
67
68     def list(self, msg, indent=0):
69         """ Function list
70         Print a list item
71
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
74         @return RETURN: None
75         """
76         print(' ' * indent * self.TABSIZE, '-', msg)
77
78     def list_id(self, dic, indent=0):
79         """ Function list_id
80         Print a list of dict items
81
82         @param dic: The dict to print
83         @param indent: Tab size at the beginning of the line
84         @return RETURN: None
85         """
86         for (k, v) in dic.items():
87             self.list("{}: {}".format(k, v), indent=indent)
88
89     def status(self, res, msg, failed="", eol="\n", quit=True, indent=0):
90         """ Function status
91         Print status message
92         - OK/KO if the result is a boolean
93         - Else the result text
94
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
100         @return RETURN: None
101         """
102         ind = ' ' * indent * self.TABSIZE
103         if res is True:
104             msg = '{} [{}OK{}] {}'.format(ind, self.OKGREEN, self.ENDC, msg)
105         elif res:
106             msg = '{} [{}{}{}] {}'.format(ind, self.OKBLUE, res,
107                                           self.ENDC, msg)
108         else:
109             msg = '{} [{}KO{}] {}'.format(ind, self.FAIL, self.ENDC, msg)
110             if failed:
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:
115             sys.exit(0)
116
117     def ask_validation(self, prompt=None, resp=False):
118         """ Function ask_validation
119         Ask a validation message
120
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
124         """
125         if prompt is None:
126             prompt = 'Continue ?'
127         if resp:
128             prompt += ' [{}Y{}/n]: '.format(self.BOLD, self.ENDC)
129         else:
130             prompt += ' [y/{}N{}]: '.format(self.BOLD, self.ENDC)
131         while True:
132             ans = input(prompt)
133             if not ans:
134                 ans = 'y' if resp else 'n'
135             if ans not in ['y', 'Y', 'n', 'N']:
136                 print('please enter y or n.')
137                 continue
138             if ans == 'y' or ans == 'Y':
139                 return True
140             if ans == 'n' or ans == 'N':
141                 sys.exit(0)