|
1 |
| -""" |
2 |
| -mbed SDK |
3 |
| -Copyright (c) 2016 ARM Limited |
4 |
| -
|
5 |
| -Licensed under the Apache License, Version 2.0 (the "License"); |
6 |
| -you may not use this file except in compliance with the License. |
7 |
| -You may obtain a copy of the License at |
8 |
| -
|
9 |
| - http://www.apache.org/licenses/LICENSE-2.0 |
10 |
| -
|
11 |
| -Unless required by applicable law or agreed to in writing, software |
12 |
| -distributed under the License is distributed on an "AS IS" BASIS, |
13 |
| -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
| -See the License for the specific language governing permissions and |
15 |
| -limitations under the License. |
16 |
| -""" |
| 1 | +# mbed SDK |
| 2 | +# Copyright (c) 2016 ARM Limited |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
17 | 15 |
|
18 | 16 | """ This python file is responsible for generating colorized notifiers.
|
19 | 17 | """
|
|
23 | 21 | from colorama import init, Fore, Back, Style
|
24 | 22 | init()
|
25 | 23 |
|
26 |
| -colors = { |
| 24 | +COLORS = { |
27 | 25 | 'none' : "",
|
28 | 26 | 'default' : Style.RESET_ALL,
|
29 | 27 |
|
|
46 | 44 | 'on_white' : Back.WHITE,
|
47 | 45 | }
|
48 | 46 |
|
49 |
| -# Convert a color string from a string into an ascii escape code that will print |
50 |
| -# that color on the terminal. |
51 |
| -color_matcher = re.compile(r"(\w+)(\W+on\W+\w+)?") |
| 47 | +COLOR_MATCHER = re.compile(r"(\w+)(\W+on\W+\w+)?") |
52 | 48 | def colorstring_to_escapecode(color_string):
|
53 |
| - match = re.match(color_matcher, color_string) |
| 49 | + """ Convert a color string from a string into an ascii escape code that |
| 50 | + will print that color on the terminal. |
| 51 | +
|
| 52 | + Positional arguments: |
| 53 | + color_string - the string to parse |
| 54 | + """ |
| 55 | + match = re.match(COLOR_MATCHER, color_string) |
54 | 56 | if match:
|
55 |
| - return colors[match.group(1)] + (colors[match.group(2).strip().replace(" ","_")] if match.group(2) else "") |
| 57 | + return COLORS[match.group(1)] + \ |
| 58 | + (COLORS[match.group(2).strip().replace(" ", "_")] |
| 59 | + if match.group(2) else "") |
56 | 60 | else:
|
57 |
| - return corols['default'] |
| 61 | + return COLORS['default'] |
| 62 | + |
58 | 63 |
|
59 |
| -# Wrap a toolchain notifier in a colorizer. This colorizer will wrap notifications |
60 |
| -# in a color if the severity matches a color in the *color_map*. |
61 |
| -def print_in_color_notifier (color_map, print_fn): |
| 64 | +def print_in_color_notifier(color_map, print_fn): |
| 65 | + """ Wrap a toolchain notifier in a colorizer. This colorizer will wrap |
| 66 | + notifications in a color if the severity matches a color in the *color_map*. |
| 67 | + """ |
62 | 68 | def wrap(event, silent=False):
|
63 |
| - fd = sys.stdout |
| 69 | + """The notification function inself""" |
| 70 | + file_desc = sys.stdout |
64 | 71 | self = event['toolchain']
|
65 |
| - if fd.isatty() and 'severity' in event and event['severity'] in color_map: |
66 |
| - fd.write(colorstring_to_escapecode(color_map[event['severity']])) |
| 72 | + if file_desc.isatty() and 'severity' in event and \ |
| 73 | + event['severity'] in color_map: |
| 74 | + file_desc.write(colorstring_to_escapecode( |
| 75 | + color_map[event['severity']])) |
67 | 76 | print_fn(self, event, silent)
|
68 |
| - fd.write(colorstring_to_escapecode('default')) |
| 77 | + file_desc.write(colorstring_to_escapecode('default')) |
69 | 78 | else:
|
70 | 79 | print_fn(self, event, silent)
|
71 | 80 | return wrap
|
0 commit comments