Exemplo n.º 1
0
    def __init__(self, *args: Any, **kwargs: Any) -> None:
        # The Windows terminal does not support the hide/show cursor ANSI codes
        # even with colorama. So we'll ensure that hide_cursor is False on
        # Windows.
        # This call needs to go before the super() call, so that hide_cursor
        # is set in time. The base progress bar class writes the "hide cursor"
        # code to the terminal in its init, so if we don't set this soon
        # enough, we get a "hide" with no corresponding "show"...
        if WINDOWS and self.hide_cursor:  # type: ignore
            self.hide_cursor = False

        # https://github.com/python/mypy/issues/5887
        super().__init__(*args, **kwargs)  # type: ignore

        # Check if we are running on Windows and we have the colorama module,
        # if we do then wrap our file with it.
        if WINDOWS and colorama:
            self.file = colorama.AnsiToWin32(self.file)  # type: ignore
            # The progress code expects to be able to call self.file.isatty()
            # but the colorama.AnsiToWin32() object doesn't have that, so we'll
            # add it.
            self.file.isatty = lambda: self.file.wrapped.isatty()
            # The progress code expects to be able to call self.file.flush()
            # but the colorama.AnsiToWin32() object doesn't have that, so we'll
            # add it.
            self.file.flush = lambda: self.file.wrapped.flush()
Exemplo n.º 2
0
    def __init__(self, stream=None, no_color=None):
        # type: (Optional[TextIO], bool) -> None
        super().__init__(stream)
        self._no_color = no_color

        if WINDOWS and colorama:
            self.stream = colorama.AnsiToWin32(self.stream)
Exemplo n.º 3
0
    def __init__(self,
                 stream: Optional[TextIO] = None,
                 no_color: bool = None) -> None:
        super().__init__(stream)
        self._no_color = no_color

        if WINDOWS and colorama:
            self.stream = colorama.AnsiToWin32(self.stream)
Exemplo n.º 4
0
def deprecated_action(func=None, new_name=None, sub_commands=False):
    if not func:
        return functools.partial(deprecated_action, new_name=new_name, sub_commands=sub_commands)

    stream = sys.stderr
    try:
        from pip._vendor import colorama
        WINDOWS = (sys.platform.startswith("win") or
                   (sys.platform == 'cli' and os.name == 'nt'))
        if WINDOWS:
            stream = colorama.AnsiToWin32(sys.stderr)
    except Exception:
        colorama = None

    def should_color():
        # Don't colorize things if we do not have colorama or if told not to
        if not colorama:
            return False

        real_stream = (
            stream if not isinstance(stream, colorama.AnsiToWin32)
            else stream.wrapped
        )

        # If the stream is a tty we should color it
        if hasattr(real_stream, "isatty") and real_stream.isatty():
            return True

        if os.environ.get("TERM") and "color" in os.environ.get("TERM"):
            return True

        # If anything else we should not color it
        return False

    @functools.wraps(func)
    def wrapper(args):
        if getattr(args, 'deprecation_warning', True):
            command = args.subcommand or args.func.__name__
            if sub_commands:
                msg = (
                    "The mode (-l, -d, etc) options to {!r} have been deprecated and removed in Airflow 2.0,"
                    " please use the get/set/list subcommands instead"
                ).format(command)
            else:
                prefix = "The {!r} command is deprecated and removed in Airflow 2.0, please use "
                if isinstance(new_name, list):
                    msg = prefix.format(args.subcommand)
                    new_names = list(map(repr, new_name))
                    msg += "{}, or {}".format(", ".join(new_names[:-1]), new_names[-1])
                    msg += " instead"
                else:
                    msg = (prefix + "{!r} instead").format(command, new_name)

            if should_color():
                msg = "".join([colorama.Fore.YELLOW, msg, colorama.Style.RESET_ALL])
            print(msg, file=sys.stderr)
        func(args)
    return wrapper
Exemplo n.º 5
0
    def __init__(self, stream=None, no_color=None):

        super().__init__(stream)

        self._no_color = no_color

        if WINDOWS and colorama:

            self.stream = colorama.AnsiToWin32(self.stream)
Exemplo n.º 6
0
 def add_consumers(self, *consumers):
     if sys.platform.startswith("win"):
         for level, consumer in consumers:
             if hasattr(consumer, "write"):
                 self.consumers.append(
                     (level, colorama.AnsiToWin32(consumer)), )
             else:
                 self.consumers.append((level, consumer))
     else:
         self.consumers.extend(consumers)
Exemplo n.º 7
0
 def add_consumers(self, *consumers):
     if WINDOWS:
         for level, consumer in consumers:
             if hasattr(consumer, "write"):
                 self.consumers.append(
                     (level, colorama.AnsiToWin32(consumer)), )
             else:
                 self.consumers.append((level, consumer))
     else:
         self.consumers.extend(consumers)
Exemplo n.º 8
0
    def add_consumers(self, *consumers):
        for level, consumer in consumers:
            # Try to check for duplicate consumers before adding them
            for chk_level, chk_consumer in self.consumers:
                # Account for coloroma wrapped streams
                if isinstance(chk_consumer, colorama.AnsiToWin32):
                    chk_consumer = chk_consumer.wrapped

                if (level, consumer) == (chk_level, chk_consumer):
                    break
            # If we didn't find a duplicate, then add it
            else:
                # Colorize consumer for Windows
                if sys.platform.startswith('win') \
                   and hasattr(consumer, 'write'):
                    consumer = colorama.AnsiToWin32(consumer)

                self.consumers.append((level, consumer))
Exemplo n.º 9
0
    def __init__(self, *args, **kwargs):
        super(WindowsMixin, self).__init__(*args, **kwargs)

        # Check if we are running on Windows and we have the colorama module,
        # if we do then wrap our file with it.
        if WINDOWS and colorama:
            self.file = colorama.AnsiToWin32(self.file)
            # The progress code expects to be able to call self.file.isatty()
            # but the colorama.AnsiToWin32() object doesn't have that, so we'll
            # add it.
            self.file.isatty = lambda: self.file.wrapped.isatty()
            # The progress code expects to be able to call self.file.flush()
            # but the colorama.AnsiToWin32() object doesn't have that, so we'll
            # add it.
            self.file.flush = lambda: self.file.wrapped.flush()

        # The Windows terminal does not support the hide/show cursor ANSI codes
        # even with colorama. So we'll ensure that hide_cursor is False on
        # Windows.
        if WINDOWS and self.hide_cursor:
            self.hide_cursor = False
Exemplo n.º 10
0
    def __init__(self, stream=None):
        logging_t.StreamHandler.__init__(self, stream)

        if WINDOWS and colorama:
            self.stream = colorama.AnsiToWin32(self.stream)
Exemplo n.º 11
0
    def __init__(self, stream=None, no_color=None):
        logging.StreamHandler.__init__(self, stream)
        self._no_color = no_color

        if WINDOWS and colorama:
            self.stream = colorama.AnsiToWin32(self.stream)
Exemplo n.º 12
0
        # is set in time. The base progress bar class writes the "hide cursor"
        # code to the terminal in its init, so if we don't set this soon
        # enough, we get a "hide" with no corresponding "show"...
        if WINDOWS and self.hide_cursor:  # type: ignore
            self.hide_cursor = False

<<<<<<< HEAD
        # https://github.com/python/mypy/issues/5887
=======
>>>>>>> b66a76afa15ab74019740676a52a071b85ed8f71
        super(WindowsMixin, self).__init__(*args, **kwargs)  # type: ignore

        # Check if we are running on Windows and we have the colorama module,
        # if we do then wrap our file with it.
        if WINDOWS and colorama:
            self.file = colorama.AnsiToWin32(self.file)  # type: ignore
            # The progress code expects to be able to call self.file.isatty()
            # but the colorama.AnsiToWin32() object doesn't have that, so we'll
            # add it.
            self.file.isatty = lambda: self.file.wrapped.isatty()
            # The progress code expects to be able to call self.file.flush()
            # but the colorama.AnsiToWin32() object doesn't have that, so we'll
            # add it.
            self.file.flush = lambda: self.file.wrapped.flush()


class BaseDownloadProgressBar(WindowsMixin, InterruptibleMixin,
                              DownloadProgressMixin):

    file = sys.stdout
    message = "%(percent)d%%"