pyglove 0.4.5.dev202501060809__py3-none-any.whl → 0.4.5.dev202501080809__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyglove/core/utils/__init__.py +5 -0
- pyglove/core/utils/text_color.py +128 -0
- pyglove/core/utils/text_color_test.py +94 -0
- {pyglove-0.4.5.dev202501060809.dist-info → pyglove-0.4.5.dev202501080809.dist-info}/METADATA +2 -1
- {pyglove-0.4.5.dev202501060809.dist-info → pyglove-0.4.5.dev202501080809.dist-info}/RECORD +8 -6
- {pyglove-0.4.5.dev202501060809.dist-info → pyglove-0.4.5.dev202501080809.dist-info}/LICENSE +0 -0
- {pyglove-0.4.5.dev202501060809.dist-info → pyglove-0.4.5.dev202501080809.dist-info}/WHEEL +0 -0
- {pyglove-0.4.5.dev202501060809.dist-info → pyglove-0.4.5.dev202501080809.dist-info}/top_level.txt +0 -0
pyglove/core/utils/__init__.py
CHANGED
@@ -155,5 +155,10 @@ from pyglove.core.utils.error_utils import ErrorInfo
|
|
155
155
|
from pyglove.core.utils.timing import timeit
|
156
156
|
from pyglove.core.utils.timing import TimeIt
|
157
157
|
|
158
|
+
# Text color.
|
159
|
+
from pyglove.core.utils.text_color import colored
|
160
|
+
from pyglove.core.utils.text_color import colored_block
|
161
|
+
from pyglove.core.utils.text_color import decolor
|
162
|
+
|
158
163
|
# pylint: enable=g-importing-member
|
159
164
|
# pylint: enable=g-bad-import-order
|
@@ -0,0 +1,128 @@
|
|
1
|
+
# Copyright 2025 The PyGlove Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain 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,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
"""Utility library for text coloring."""
|
15
|
+
|
16
|
+
import re
|
17
|
+
from typing import List, Optional
|
18
|
+
|
19
|
+
try:
|
20
|
+
import termcolor # pylint: disable=g-import-not-at-top
|
21
|
+
except ImportError:
|
22
|
+
termcolor = None
|
23
|
+
|
24
|
+
|
25
|
+
# Regular expression for ANSI color characters.
|
26
|
+
_ANSI_COLOR_REGEX = re.compile(r'\x1b\[[0-9;]*m')
|
27
|
+
|
28
|
+
|
29
|
+
def decolor(text: str) -> str:
|
30
|
+
"""De-colors a string that may contains ANSI color characters."""
|
31
|
+
return re.sub(_ANSI_COLOR_REGEX, '', text)
|
32
|
+
|
33
|
+
|
34
|
+
def colored(
|
35
|
+
text: str,
|
36
|
+
color: Optional[str] = None,
|
37
|
+
background: Optional[str] = None,
|
38
|
+
styles: Optional[List[str]] = None,
|
39
|
+
) -> str:
|
40
|
+
"""Returns the colored text with ANSI color characters.
|
41
|
+
|
42
|
+
Args:
|
43
|
+
text: A string that may or may not already has ANSI color characters.
|
44
|
+
color: A string for text colors. Applicable values are:
|
45
|
+
'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'.
|
46
|
+
background: A string for background colors. Applicable values are:
|
47
|
+
'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'.
|
48
|
+
styles: A list of strings for applying styles on the text.
|
49
|
+
Applicable values are:
|
50
|
+
'bold', 'dark', 'underline', 'blink', 'reverse', 'concealed'.
|
51
|
+
|
52
|
+
Returns:
|
53
|
+
A string with ANSI color characters embracing the entire text.
|
54
|
+
"""
|
55
|
+
if not termcolor:
|
56
|
+
return text
|
57
|
+
return termcolor.colored(
|
58
|
+
text,
|
59
|
+
color=color,
|
60
|
+
on_color=('on_' + background) if background else None,
|
61
|
+
attrs=styles
|
62
|
+
)
|
63
|
+
|
64
|
+
|
65
|
+
def colored_block(
|
66
|
+
text: str,
|
67
|
+
block_start: str,
|
68
|
+
block_end: str,
|
69
|
+
color: Optional[str] = None,
|
70
|
+
background: Optional[str] = None,
|
71
|
+
styles: Optional[List[str]] = None,
|
72
|
+
) -> str:
|
73
|
+
"""Apply colors to text blocks.
|
74
|
+
|
75
|
+
Args:
|
76
|
+
text: A string that may or may not already has ANSI color characters.
|
77
|
+
block_start: A string that signals the start of a block. E.g. '{{'
|
78
|
+
block_end: A string that signals the end of a block. E.g. '}}'.
|
79
|
+
color: A string for text colors. Applicable values are:
|
80
|
+
'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'.
|
81
|
+
background: A string for background colors. Applicable values are:
|
82
|
+
'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'.
|
83
|
+
styles: A list of strings for applying styles on the text.
|
84
|
+
Applicable values are:
|
85
|
+
'bold', 'dark', 'underline', 'blink', 'reverse', 'concealed'.
|
86
|
+
|
87
|
+
Returns:
|
88
|
+
A string with ANSI color characters embracing the matched text blocks.
|
89
|
+
"""
|
90
|
+
if not color and not background and not styles:
|
91
|
+
return text
|
92
|
+
|
93
|
+
s = []
|
94
|
+
start_index = 0
|
95
|
+
end_index = 0
|
96
|
+
previous_color = None
|
97
|
+
|
98
|
+
def write_nonblock_text(text: str, previous_color: Optional[str]):
|
99
|
+
if previous_color:
|
100
|
+
s.append(previous_color)
|
101
|
+
s.append(text)
|
102
|
+
|
103
|
+
while start_index < len(text):
|
104
|
+
start_index = text.find(block_start, end_index)
|
105
|
+
if start_index == -1:
|
106
|
+
write_nonblock_text(text[end_index:], previous_color)
|
107
|
+
break
|
108
|
+
|
109
|
+
# Deal with text since last block.
|
110
|
+
since_last_block = text[end_index:start_index]
|
111
|
+
write_nonblock_text(since_last_block, previous_color)
|
112
|
+
colors = re.findall(_ANSI_COLOR_REGEX, since_last_block)
|
113
|
+
if colors:
|
114
|
+
previous_color = colors[-1]
|
115
|
+
|
116
|
+
# Match block.
|
117
|
+
end_index = text.find(block_end, start_index + len(block_start))
|
118
|
+
if end_index == -1:
|
119
|
+
write_nonblock_text(text[start_index:], previous_color)
|
120
|
+
break
|
121
|
+
end_index += len(block_end)
|
122
|
+
|
123
|
+
# Write block text.
|
124
|
+
block = text[start_index:end_index]
|
125
|
+
block = colored(
|
126
|
+
block, color=color, background=background, styles=styles)
|
127
|
+
s.append(block)
|
128
|
+
return ''.join(s)
|
@@ -0,0 +1,94 @@
|
|
1
|
+
# Copyright 2025 The PyGlove Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain 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,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
import inspect
|
15
|
+
import os
|
16
|
+
import unittest
|
17
|
+
from pyglove.core.utils import text_color
|
18
|
+
|
19
|
+
|
20
|
+
class TextColorTest(unittest.TestCase):
|
21
|
+
|
22
|
+
def setUp(self):
|
23
|
+
super().setUp()
|
24
|
+
os.environ.pop('ANSI_COLORS_DISABLED', None)
|
25
|
+
os.environ.pop('NO_COLOR', None)
|
26
|
+
os.environ['FORCE_COLOR'] = '1'
|
27
|
+
|
28
|
+
def test_colored_block_without_colors_and_styles(self):
|
29
|
+
self.assertEqual(text_color.colored_block('foo', '{{', '}}'), 'foo')
|
30
|
+
|
31
|
+
def test_colored_block(self):
|
32
|
+
original_text = inspect.cleandoc("""
|
33
|
+
Hi << foo >>
|
34
|
+
<# print x if x is present #>
|
35
|
+
<% if x %>
|
36
|
+
<< x >>
|
37
|
+
<% endif %>
|
38
|
+
""")
|
39
|
+
|
40
|
+
colored_text = text_color.colored_block(
|
41
|
+
text_color.colored(original_text, color='blue'),
|
42
|
+
'<<', '>>',
|
43
|
+
color='white',
|
44
|
+
background='blue',
|
45
|
+
)
|
46
|
+
origin_color = '\x1b[34m'
|
47
|
+
reset = '\x1b[0m'
|
48
|
+
block_color = text_color.colored(
|
49
|
+
'TEXT', color='white', background='blue'
|
50
|
+
).split('TEXT')[0]
|
51
|
+
self.assertEqual(
|
52
|
+
colored_text,
|
53
|
+
f'{origin_color}Hi {block_color}<< foo >>{reset}{origin_color}\n'
|
54
|
+
'<# print x if x is present #>\n<% if x %>\n'
|
55
|
+
f'{block_color}<< x >>{reset}{origin_color}\n'
|
56
|
+
f'<% endif %>{reset}'
|
57
|
+
)
|
58
|
+
self.assertEqual(text_color.decolor(colored_text), original_text)
|
59
|
+
|
60
|
+
def test_colored_block_without_full_match(self):
|
61
|
+
self.assertEqual(
|
62
|
+
text_color.colored_block(
|
63
|
+
'Hi {{ foo',
|
64
|
+
'{{', '}}',
|
65
|
+
color='white',
|
66
|
+
background='blue',
|
67
|
+
),
|
68
|
+
'Hi {{ foo'
|
69
|
+
)
|
70
|
+
|
71
|
+
def test_colored_block_without_termcolor(self):
|
72
|
+
termcolor = text_color.termcolor
|
73
|
+
text_color.termcolor = None
|
74
|
+
original_text = inspect.cleandoc("""
|
75
|
+
Hi {{ foo }}
|
76
|
+
{# print x if x is present #}
|
77
|
+
{% if x %}
|
78
|
+
{{ x }}
|
79
|
+
{% endif %}
|
80
|
+
""")
|
81
|
+
|
82
|
+
colored_text = text_color.colored_block(
|
83
|
+
text_color.colored(original_text, color='blue'),
|
84
|
+
'{{', '}}',
|
85
|
+
color='white',
|
86
|
+
background='blue',
|
87
|
+
)
|
88
|
+
self.assertEqual(colored_text, original_text)
|
89
|
+
self.assertEqual(text_color.decolor(colored_text), original_text)
|
90
|
+
text_color.termcolor = termcolor
|
91
|
+
|
92
|
+
|
93
|
+
if __name__ == '__main__':
|
94
|
+
unittest.main()
|
{pyglove-0.4.5.dev202501060809.dist-info → pyglove-0.4.5.dev202501080809.dist-info}/METADATA
RENAMED
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: pyglove
|
3
|
-
Version: 0.4.5.
|
3
|
+
Version: 0.4.5.dev202501080809
|
4
4
|
Summary: PyGlove: A library for manipulating Python objects.
|
5
5
|
Home-page: https://github.com/google/pyglove
|
6
6
|
Author: PyGlove Authors
|
@@ -22,6 +22,7 @@ Classifier: Topic :: Software Development :: Libraries
|
|
22
22
|
Description-Content-Type: text/markdown
|
23
23
|
License-File: LICENSE
|
24
24
|
Requires-Dist: docstring-parser>=0.12
|
25
|
+
Requires-Dist: termcolor>=1.1.0
|
25
26
|
|
26
27
|
<div align="center">
|
27
28
|
<img src="https://raw.githubusercontent.com/google/pyglove/main/docs/_static/logo_light.svg#gh-light-mode-only" width="320px" alt="logo"></img>
|
@@ -119,7 +119,7 @@ pyglove/core/typing/typed_missing.py,sha256=-l1omAu0jBZv5BnsFYXBqfvQwVBnmPh_X1wc
|
|
119
119
|
pyglove/core/typing/typed_missing_test.py,sha256=TCNsb1SRpFaVdxYn2mB_yaLuja8w5Qn5NP7uGiZVBWs,2301
|
120
120
|
pyglove/core/typing/value_specs.py,sha256=Yxdrz4aURMBrtqUW4to-2Vptc6Z6oqQrLyMBiAZt2bc,102302
|
121
121
|
pyglove/core/typing/value_specs_test.py,sha256=MsScWDRaN_8pfRDO9MCp9HdUHVm_8wHWyKorWVSnhE4,127165
|
122
|
-
pyglove/core/utils/__init__.py,sha256=
|
122
|
+
pyglove/core/utils/__init__.py,sha256=cegrJtHy0h7DIUFcQN67TZJf9_f28OR-NrKbKKjPkeE,8183
|
123
123
|
pyglove/core/utils/common_traits.py,sha256=PWxOgPhG5H60ZwfO8xNAEGRjFUqqDZQBWQYomOfvdy8,3640
|
124
124
|
pyglove/core/utils/common_traits_test.py,sha256=DIuZB_1xfmeTVfWnGOguDQcDAM_iGgBOe8C-5CsIqBc,1122
|
125
125
|
pyglove/core/utils/docstr_utils.py,sha256=5BY40kXozPKVGOB0eN8jy1P5_GHIzqFJ9FXAu_kzxaw,5119
|
@@ -134,6 +134,8 @@ pyglove/core/utils/json_conversion.py,sha256=I0mWn87aAEdaAok9nDvT0ZrmplU40eNmEDU
|
|
134
134
|
pyglove/core/utils/json_conversion_test.py,sha256=zA_cy7ixVL3sTf6i9BCXMlSH56Aa3JnjHnjyqYJ_9XU,11845
|
135
135
|
pyglove/core/utils/missing.py,sha256=9gslt1lXd1qSEIuAFxUWu30oD-YdYcnm13eau1S9uqY,1445
|
136
136
|
pyglove/core/utils/missing_test.py,sha256=D6-FuVEwCyJemUiPLcwLmwyptqI5Bx0Pfipc2juhKSE,1335
|
137
|
+
pyglove/core/utils/text_color.py,sha256=xcCTCxY2qFNZs_jismMGus8scEXKBpYGAhpAgnz-MHk,4112
|
138
|
+
pyglove/core/utils/text_color_test.py,sha256=3HtZpUB5XPr7A_5Fg4ZSMfNWeDRiQgSzmg9b1tctMI4,2801
|
137
139
|
pyglove/core/utils/thread_local.py,sha256=i-CnyY3VREtLfAj4_JndBnsKuQLIgwG29ma8dAyRxbI,4839
|
138
140
|
pyglove/core/utils/thread_local_test.py,sha256=AOqsdNsA-cYMvJckqxb25ql3Y5kDW0qLfBW1cl85Bnw,6757
|
139
141
|
pyglove/core/utils/timing.py,sha256=fXdwEYXR84qZBC7hxbXbwztW2UKliJZLqCDvhz1qw_A,7241
|
@@ -197,8 +199,8 @@ pyglove/ext/scalars/randoms.py,sha256=LkMIIx7lOq_lvJvVS3BrgWGuWl7Pi91-lA-O8x_gZs
|
|
197
199
|
pyglove/ext/scalars/randoms_test.py,sha256=nEhiqarg8l_5EOucp59CYrpO2uKxS1pe0hmBdZUzRNM,2000
|
198
200
|
pyglove/ext/scalars/step_wise.py,sha256=IDw3tuTpv0KVh7AN44W43zqm1-E0HWPUlytWOQC9w3Y,3789
|
199
201
|
pyglove/ext/scalars/step_wise_test.py,sha256=TL1vJ19xVx2t5HKuyIzGoogF7N3Rm8YhLE6JF7i0iy8,2540
|
200
|
-
pyglove-0.4.5.
|
201
|
-
pyglove-0.4.5.
|
202
|
-
pyglove-0.4.5.
|
203
|
-
pyglove-0.4.5.
|
204
|
-
pyglove-0.4.5.
|
202
|
+
pyglove-0.4.5.dev202501080809.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
|
203
|
+
pyglove-0.4.5.dev202501080809.dist-info/METADATA,sha256=1rkODhRd0zayZYQ5lczr0q5i4XDQ2BQFAYnLRmS_39g,6860
|
204
|
+
pyglove-0.4.5.dev202501080809.dist-info/WHEEL,sha256=A3WOREP4zgxI0fKrHUG8DC8013e3dK3n7a6HDbcEIwE,91
|
205
|
+
pyglove-0.4.5.dev202501080809.dist-info/top_level.txt,sha256=wITzJSKcj8GZUkbq-MvUQnFadkiuAv_qv5qQMw0fIow,8
|
206
|
+
pyglove-0.4.5.dev202501080809.dist-info/RECORD,,
|
File without changes
|
File without changes
|
{pyglove-0.4.5.dev202501060809.dist-info → pyglove-0.4.5.dev202501080809.dist-info}/top_level.txt
RENAMED
File without changes
|