| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import io | ||
| import os | ||
| import sys | ||
|
|
||
| COLORIZE = True | ||
|
|
||
|
|
||
| class ANSIColors: | ||
| BOLD_GREEN = "\x1b[1;32m" | ||
| BOLD_MAGENTA = "\x1b[1;35m" | ||
| BOLD_RED = "\x1b[1;31m" | ||
| GREEN = "\x1b[32m" | ||
| GREY = "\x1b[90m" | ||
| MAGENTA = "\x1b[35m" | ||
| RED = "\x1b[31m" | ||
| RESET = "\x1b[0m" | ||
| YELLOW = "\x1b[33m" | ||
|
|
||
|
|
||
| NoColors = ANSIColors() | ||
|
|
||
| for attr in dir(NoColors): | ||
| if not attr.startswith("__"): | ||
| setattr(NoColors, attr, "") | ||
|
|
||
|
|
||
| def get_colors(colorize: bool = False) -> ANSIColors: | ||
| if colorize or can_colorize(): | ||
| return ANSIColors() | ||
| else: | ||
| return NoColors | ||
|
|
||
|
|
||
| def can_colorize() -> bool: | ||
| if sys.platform == "win32": | ||
| try: | ||
| import nt | ||
|
|
||
| if not nt._supports_virtual_terminal(): | ||
| return False | ||
| except (ImportError, AttributeError): | ||
| return False | ||
| if not sys.flags.ignore_environment: | ||
| if os.environ.get("PYTHON_COLORS") == "0": | ||
| return False | ||
| if os.environ.get("PYTHON_COLORS") == "1": | ||
| return True | ||
| if "NO_COLOR" in os.environ: | ||
| return False | ||
| if not COLORIZE: | ||
| return False | ||
| if not sys.flags.ignore_environment: | ||
| if "FORCE_COLOR" in os.environ: | ||
| return True | ||
| if os.environ.get("TERM") == "dumb": | ||
| return False | ||
|
|
||
| if not hasattr(sys.stderr, "fileno"): | ||
| return False | ||
|
|
||
| try: | ||
| return os.isatty(sys.stderr.fileno()) | ||
| except io.UnsupportedOperation: | ||
| return sys.stderr.isatty() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import contextlib | ||
| import sys | ||
| import unittest | ||
| import unittest.mock | ||
| import _colorize | ||
| from test.support import force_not_colorized | ||
|
|
||
| ORIGINAL_CAN_COLORIZE = _colorize.can_colorize | ||
|
|
||
|
|
||
| def setUpModule(): | ||
| _colorize.can_colorize = lambda: False | ||
|
|
||
|
|
||
| def tearDownModule(): | ||
| _colorize.can_colorize = ORIGINAL_CAN_COLORIZE | ||
|
|
||
|
|
||
| class TestColorizeFunction(unittest.TestCase): | ||
| @force_not_colorized | ||
| def test_colorized_detection_checks_for_environment_variables(self): | ||
| if sys.platform == "win32": | ||
| virtual_patching = unittest.mock.patch("nt._supports_virtual_terminal", | ||
| return_value=True) | ||
| else: | ||
| virtual_patching = contextlib.nullcontext() | ||
| with virtual_patching: | ||
|
|
||
| flags = unittest.mock.MagicMock(ignore_environment=False) | ||
| with (unittest.mock.patch("os.isatty") as isatty_mock, | ||
| unittest.mock.patch("sys.flags", flags), | ||
| unittest.mock.patch("_colorize.can_colorize", ORIGINAL_CAN_COLORIZE)): | ||
| isatty_mock.return_value = True | ||
| with unittest.mock.patch("os.environ", {'TERM': 'dumb'}): | ||
| self.assertEqual(_colorize.can_colorize(), False) | ||
| with unittest.mock.patch("os.environ", {'PYTHON_COLORS': '1'}): | ||
| self.assertEqual(_colorize.can_colorize(), True) | ||
| with unittest.mock.patch("os.environ", {'PYTHON_COLORS': '0'}): | ||
| self.assertEqual(_colorize.can_colorize(), False) | ||
| with unittest.mock.patch("os.environ", {'NO_COLOR': '1'}): | ||
| self.assertEqual(_colorize.can_colorize(), False) | ||
| with unittest.mock.patch("os.environ", | ||
| {'NO_COLOR': '1', "PYTHON_COLORS": '1'}): | ||
| self.assertEqual(_colorize.can_colorize(), True) | ||
| with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1'}): | ||
| self.assertEqual(_colorize.can_colorize(), True) | ||
| with unittest.mock.patch("os.environ", | ||
| {'FORCE_COLOR': '1', 'NO_COLOR': '1'}): | ||
| self.assertEqual(_colorize.can_colorize(), False) | ||
| with unittest.mock.patch("os.environ", | ||
| {'FORCE_COLOR': '1', "PYTHON_COLORS": '0'}): | ||
| self.assertEqual(_colorize.can_colorize(), False) | ||
| isatty_mock.return_value = False | ||
| with unittest.mock.patch("os.environ", {}): | ||
| self.assertEqual(_colorize.can_colorize(), False) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.