|
14 | 14 | # Python 3.6+ should have Collection in the typing module |
15 | 15 | from typing import Collection |
16 | 16 | except ImportError: |
17 | | - from typing import Container, Sized |
18 | | - import sys |
19 | | - |
20 | | - # Unfortunately need slightly different solutions for Python 3.4 vs 3.5 |
21 | | - if sys.version_info < (3, 5): |
22 | | - # Python 3.4 |
23 | | - # noinspection PyAbstractClass |
24 | | - class Collection(Container, Sized, Iterable): |
25 | | - """hack to enable Collection typing""" |
26 | | - __slots__ = () |
27 | | - |
28 | | - # noinspection PyPep8Naming |
29 | | - @classmethod |
30 | | - def __subclasshook__(cls, C): |
31 | | - if cls is Collection: |
32 | | - if any("__len__" in B.__dict__ for B in C.__mro__) and \ |
33 | | - any("__iter__" in B.__dict__ for B in C.__mro__) and \ |
34 | | - any("__contains__" in B.__dict__ for B in C.__mro__): |
35 | | - return True |
36 | | - return NotImplemented |
37 | | - else: |
38 | | - # Python 3.5 |
39 | | - # noinspection PyAbstractClass |
40 | | - from typing import Generic, TypeVar |
41 | | - class Collection(Generic[TypeVar('T_co', covariant=True)], Container, Sized, Iterable): |
42 | | - """hack to enable Collection typing""" |
43 | | - __slots__ = () |
44 | | - |
45 | | - # noinspection PyPep8Naming |
46 | | - @classmethod |
47 | | - def __subclasshook__(cls, C): |
48 | | - if cls is Collection: |
49 | | - if any("__len__" in B.__dict__ for B in C.__mro__) and \ |
50 | | - any("__iter__" in B.__dict__ for B in C.__mro__) and \ |
51 | | - any("__contains__" in B.__dict__ for B in C.__mro__): |
52 | | - return True |
53 | | - return NotImplemented |
| 17 | + from typing import Container, Generic, Sized, TypeVar |
| 18 | + # Python 3.5 |
| 19 | + # noinspection PyAbstractClass |
| 20 | + class Collection(Generic[TypeVar('T_co', covariant=True)], Container, Sized, Iterable): |
| 21 | + """hack to enable Collection typing""" |
| 22 | + __slots__ = () |
| 23 | + |
| 24 | + # noinspection PyPep8Naming |
| 25 | + @classmethod |
| 26 | + def __subclasshook__(cls, C): |
| 27 | + if cls is Collection: |
| 28 | + if any("__len__" in B.__dict__ for B in C.__mro__) and \ |
| 29 | + any("__iter__" in B.__dict__ for B in C.__mro__) and \ |
| 30 | + any("__contains__" in B.__dict__ for B in C.__mro__): |
| 31 | + return True |
| 32 | + return NotImplemented |
54 | 33 |
|
55 | 34 |
|
56 | 35 | ANSI_ESCAPE_RE = re.compile(r'\x1b[^m]*m') |
@@ -303,6 +282,44 @@ class TableColors(object): |
303 | 282 | BOLD = '' |
304 | 283 | RESET = '' |
305 | 284 |
|
| 285 | + @classmethod |
| 286 | + def set_color_library(cls, library_name: str) -> None: |
| 287 | + """Manually override the color library being used.""" |
| 288 | + if library_name == 'colored': |
| 289 | + from colored import fg, bg, attr |
| 290 | + |
| 291 | + cls.TEXT_COLOR_WHITE = fg('white') |
| 292 | + cls.TEXT_COLOR_YELLOW = fg(226) |
| 293 | + cls.TEXT_COLOR_RED = fg(196) |
| 294 | + cls.TEXT_COLOR_GREEN = fg(119) |
| 295 | + cls.TEXT_COLOR_BLUE = fg(27) |
| 296 | + cls.BG_COLOR_ROW = bg(234) |
| 297 | + cls.BG_RESET = bg(0) |
| 298 | + cls.BOLD = attr('bold') |
| 299 | + cls.RESET = attr('reset') |
| 300 | + elif library_name == 'colorama': |
| 301 | + from colorama import Fore, Back, Style |
| 302 | + |
| 303 | + cls.TEXT_COLOR_WHITE = Fore.WHITE |
| 304 | + cls.TEXT_COLOR_YELLOW = Fore.LIGHTYELLOW_EX |
| 305 | + cls.TEXT_COLOR_RED = Fore.LIGHTRED_EX |
| 306 | + cls.TEXT_COLOR_GREEN = Fore.LIGHTGREEN_EX |
| 307 | + cls.TEXT_COLOR_BLUE = Fore.LIGHTBLUE_EX |
| 308 | + cls.BG_COLOR_ROW = Back.LIGHTBLACK_EX |
| 309 | + cls.BG_RESET = Back.RESET |
| 310 | + cls.BOLD = Style.BRIGHT |
| 311 | + cls.RESET = Fore.RESET + Back.RESET |
| 312 | + else: |
| 313 | + cls.TEXT_COLOR_WHITE = '' |
| 314 | + cls.TEXT_COLOR_YELLOW = '' |
| 315 | + cls.TEXT_COLOR_RED = '' |
| 316 | + cls.TEXT_COLOR_GREEN = '' |
| 317 | + cls.TEXT_COLOR_BLUE = '' |
| 318 | + cls.BG_COLOR_ROW = '' |
| 319 | + cls.BG_RESET = '' |
| 320 | + cls.BOLD = '' |
| 321 | + cls.RESET = '' |
| 322 | + |
306 | 323 |
|
307 | 324 | class ColumnAlignment(enum.Enum): |
308 | 325 | """Column alignment""" |
|
0 commit comments