1
|
|
|
from typing import Iterable, Union |
2
|
|
|
|
3
|
|
|
from cleo.formatters.style import Style |
4
|
|
|
from cleo.io.inputs.input import Input |
5
|
|
|
from cleo.io.io import IO |
6
|
|
|
from cleo.io.outputs.output import Output, Verbosity |
7
|
|
|
|
8
|
|
|
from pystratum_backend.Helper.Terminal import Terminal |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class StratumIO(IO): |
12
|
|
|
""" |
13
|
|
|
Output style for PyStratum. |
14
|
|
|
""" |
15
|
|
|
|
16
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
17
|
|
|
def __init__(self, input_object: Input, output_object: Output, error_output_object: Output): |
18
|
|
|
""" |
19
|
|
|
Object constructor. |
20
|
|
|
|
21
|
|
|
:param input_object: The input object. |
22
|
|
|
:param output_object: The output object. |
23
|
|
|
""" |
24
|
|
|
IO.__init__(self, input_object, output_object, error_output_object) |
25
|
|
|
|
26
|
|
|
# Create style titles. |
27
|
|
|
style = Style('yellow') |
28
|
|
|
self.output.formatter.set_style('title', style) |
29
|
|
|
self.error_output.formatter.set_style('title', style) |
30
|
|
|
|
31
|
|
|
# Create style for errors. |
32
|
|
|
style = Style(foreground='white', background='red') |
33
|
|
|
self.output.formatter.set_style('ERROR', style) |
34
|
|
|
self.error_output.formatter.set_style('ERROR', style) |
35
|
|
|
|
36
|
|
|
# Create style for warnings. |
37
|
|
|
style = Style(foreground='red') |
38
|
|
|
self.output.formatter.set_style('WARNING', style) |
39
|
|
|
self.error_output.formatter.set_style('WARNING', style) |
40
|
|
|
|
41
|
|
|
# Create style notes. |
42
|
|
|
style = Style('yellow', None, ['bold']) |
43
|
|
|
self.output.formatter.set_style('note', style) |
44
|
|
|
self.error_output.formatter.set_style('note', style) |
45
|
|
|
|
46
|
|
|
# Create style for database objects. |
47
|
|
|
style = Style('green', None, ['bold']) |
48
|
|
|
self.output.formatter.set_style('dbo', style) |
49
|
|
|
self.error_output.formatter.set_style('dbo', style) |
50
|
|
|
|
51
|
|
|
# Create style for file and directory names. |
52
|
|
|
style = Style('white', None, ['bold']) |
53
|
|
|
self.output.formatter.set_style('fso', style) |
54
|
|
|
self.error_output.formatter.set_style('fso', style) |
55
|
|
|
|
56
|
|
|
# Create style for SQL statements. |
57
|
|
|
style = Style('magenta', None, ['bold']) |
58
|
|
|
self.output.formatter.set_style('sql', style) |
59
|
|
|
self.error_output.formatter.set_style('sql', style) |
60
|
|
|
|
61
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
62
|
|
|
def text(self, message: Union[str, Iterable[str]]) -> None: |
63
|
|
|
""" |
64
|
|
|
Formats informational text. |
65
|
|
|
|
66
|
|
|
:param message: The message or messages. |
67
|
|
|
""" |
68
|
|
|
if isinstance(message, list): |
69
|
|
|
messages = message |
70
|
|
|
else: |
71
|
|
|
messages = [message] |
72
|
|
|
|
73
|
|
|
for line in messages: |
74
|
|
|
self._output.write_line(' {0}'.format(line)) |
75
|
|
|
|
76
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
77
|
|
|
def listing(self, elements: Iterable[str]) -> None: |
78
|
|
|
""" |
79
|
|
|
Writes a bullet list. |
80
|
|
|
|
81
|
|
|
:param elements: The items in the list. |
82
|
|
|
""" |
83
|
|
|
elements = list(map(lambda element: ' * %s' % element, elements)) |
84
|
|
|
|
85
|
|
|
self.output.write_line(elements) |
86
|
|
|
self.output.write_line('') |
87
|
|
|
|
88
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
89
|
|
|
def log_verbose(self, message: Union[str, Iterable[str]]) -> None: |
90
|
|
|
""" |
91
|
|
|
Logs a message only when logging level is verbose. |
92
|
|
|
|
93
|
|
|
:param message: The message or messages. |
94
|
|
|
""" |
95
|
|
|
if self._output.verbosity >= Verbosity.VERBOSE: |
96
|
|
|
self.text(message) |
97
|
|
|
|
98
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
99
|
|
|
def log_very_verbose(self, messages: Union[str, Iterable[str]]) -> None: |
100
|
|
|
""" |
101
|
|
|
Logs a message only when logging level is very verbose. |
102
|
|
|
|
103
|
|
|
:param messages: The message or messages. |
104
|
|
|
""" |
105
|
|
|
if self._output.verbosity >= Verbosity.VERY_VERBOSE: |
106
|
|
|
self.text(messages) |
107
|
|
|
|
108
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
109
|
|
|
def __block(self, block_type: str, messages: Union[str, Iterable[str]]) -> None: |
110
|
|
|
""" |
111
|
|
|
Writes a block message to the output. |
112
|
|
|
|
113
|
|
|
:param messages: The title of a section. |
114
|
|
|
""" |
115
|
|
|
terminal_width = Terminal().width |
116
|
|
|
|
117
|
|
|
if not isinstance(messages, list): |
118
|
|
|
messages = [messages] |
119
|
|
|
|
120
|
|
|
lines = ['<{}></>'.format(block_type)] |
121
|
|
|
for key, message in enumerate(messages): |
122
|
|
|
if key == 0: |
123
|
|
|
text = ' [{}] {}'.format(block_type, self.output.formatter.format(message)) |
124
|
|
|
else: |
125
|
|
|
text = ' {}'.format(self.output.formatter.format(message)) |
126
|
|
|
line = '<{}>{}{}'.format(block_type, text, ' ' * (terminal_width - len(text))) |
127
|
|
|
lines.append(line) |
128
|
|
|
lines.append('<{}></>'.format(block_type)) |
129
|
|
|
|
130
|
|
|
self.output.write_line(lines) |
131
|
|
|
|
132
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
133
|
|
|
def error(self, messages: Union[str, Iterable[str]]) -> None: |
134
|
|
|
""" |
135
|
|
|
Writes an error message to the output. |
136
|
|
|
|
137
|
|
|
:param messages: The title of a section. |
138
|
|
|
""" |
139
|
|
|
self.__block('ERROR', messages) |
140
|
|
|
|
141
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
142
|
|
|
def warning(self, messages: Union[str, Iterable[str]]) -> None: |
143
|
|
|
""" |
144
|
|
|
Writes a warning message to the output. |
145
|
|
|
|
146
|
|
|
:param messages: The title of a section. |
147
|
|
|
""" |
148
|
|
|
self.__block('WARNING', messages) |
149
|
|
|
|
150
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
151
|
|
|
def title(self, title: str) -> None: |
152
|
|
|
""" |
153
|
|
|
Writes a title to the output. |
154
|
|
|
|
155
|
|
|
:param title: The title of a section. |
156
|
|
|
""" |
157
|
|
|
self.write_line(['<title>%s</>' % title, '<title>%s</>' % ('=' * len(title)), '']) |
158
|
|
|
|
159
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
160
|
|
|
|