|
92 | 92 | from gettext import gettext as _ |
93 | 93 | from gettext import ngettext |
94 | 94 |
|
| 95 | +lazy import _colorize |
| 96 | + |
95 | 97 | SUPPRESS = '==SUPPRESS==' |
96 | 98 |
|
97 | 99 | OPTIONAL = '?' |
@@ -156,6 +158,15 @@ def _identity(value): |
156 | 158 | # Formatting Help |
157 | 159 | # =============== |
158 | 160 |
|
| 161 | +class _ColorlessTheme: |
| 162 | + # A 'fake' theme for no colors |
| 163 | + def __getattr__(self, name): |
| 164 | + # _colorize's no_color themes are just all empty strings |
| 165 | + # by directly using empty strings the import is avoided |
| 166 | + return "" |
| 167 | + |
| 168 | +_colorless_theme = _ColorlessTheme() |
| 169 | + |
159 | 170 |
|
160 | 171 | class HelpFormatter(object): |
161 | 172 | """Formatter for generating usage messages and argument help strings. |
@@ -196,14 +207,32 @@ def __init__( |
196 | 207 | self._set_color(False) |
197 | 208 |
|
198 | 209 | def _set_color(self, color, *, file=None): |
199 | | - from _colorize import can_colorize, decolor, get_theme |
200 | | - |
201 | | - if color and can_colorize(file=file): |
202 | | - self._theme = get_theme(force_color=True).argparse |
203 | | - self._decolor = decolor |
| 210 | + # Set a new color setting and file, clear caches for theme and decolor |
| 211 | + self._theme_color = color |
| 212 | + self._theme_file = file |
| 213 | + self._cached_theme = None |
| 214 | + self._cached_decolor = None |
| 215 | + |
| 216 | + def _get_theme_and_decolor(self): |
| 217 | + # If self._theme_color is false, this prevents _colorize from importing |
| 218 | + if self._theme_color and _colorize.can_colorize(file=self._theme_file): |
| 219 | + self._cached_theme = _colorize.get_theme(force_color=True).argparse |
| 220 | + self._cached_decolor = _colorize.decolor |
204 | 221 | else: |
205 | | - self._theme = get_theme(force_no_color=True).argparse |
206 | | - self._decolor = _identity |
| 222 | + self._cached_theme = _colorless_theme |
| 223 | + self._cached_decolor = _identity |
| 224 | + |
| 225 | + @property |
| 226 | + def _theme(self): |
| 227 | + if self._cached_theme is None: |
| 228 | + self._get_theme_and_decolor() |
| 229 | + return self._cached_theme |
| 230 | + |
| 231 | + @property |
| 232 | + def _decolor(self): |
| 233 | + if self._cached_decolor is None: |
| 234 | + self._get_theme_and_decolor() |
| 235 | + return self._cached_decolor |
207 | 236 |
|
208 | 237 | # =============================== |
209 | 238 | # Section and indentation methods |
@@ -2856,12 +2885,11 @@ def _print_message(self, message, file=None): |
2856 | 2885 | pass |
2857 | 2886 |
|
2858 | 2887 | def _get_theme(self, file=None): |
2859 | | - from _colorize import can_colorize, get_theme |
2860 | | - |
2861 | | - if self.color and can_colorize(file=file): |
2862 | | - return get_theme(force_color=True).argparse |
| 2888 | + # If self.color is False, _colorize is not imported |
| 2889 | + if self.color and _colorize.can_colorize(file=file): |
| 2890 | + return _colorize.get_theme(force_color=True).argparse |
2863 | 2891 | else: |
2864 | | - return get_theme(force_no_color=True).argparse |
| 2892 | + return _colorless_theme |
2865 | 2893 |
|
2866 | 2894 | # =============== |
2867 | 2895 | # Exiting methods |
|
0 commit comments