Salome HOME
style: black format
[tools/sat.git] / src / colorama / ansitowin32.py
index b7ff6f2136eec3530563c0fedf226b4b00c52d9d..8b445f56a63994bc7cef32b6ab4b23dff7f0da94 100644 (file)
@@ -14,19 +14,20 @@ if windll is not None:
 
 
 def is_stream_closed(stream):
-    return not hasattr(stream, 'closed') or stream.closed
+    return not hasattr(stream, "closed") or stream.closed
 
 
 def is_a_tty(stream):
-    return hasattr(stream, 'isatty') and stream.isatty()
+    return hasattr(stream, "isatty") and stream.isatty()
 
 
 class StreamWrapper(object):
-    '''
+    """
     Wraps a stream (such as stdout), acting as a transparent proxy for all
     attribute access apart from method 'write()', which is delegated to our
     Converter instance.
-    '''
+    """
+
     def __init__(self, wrapped, converter):
         # double-underscore everything to prevent clashes with names of
         # attributes on the wrapped stream object.
@@ -41,13 +42,18 @@ class StreamWrapper(object):
 
 
 class AnsiToWin32(object):
-    '''
+    """
     Implements a 'write()' method which, on Windows, will strip ANSI character
     sequences from the text, and if outputting to a tty, will convert them into
     win32 function calls.
-    '''
-    ANSI_CSI_RE = re.compile('\001?\033\[((?:\d|;)*)([a-zA-Z])\002?')     # Control Sequence Introducer
-    ANSI_OSC_RE = re.compile('\001?\033\]((?:.|;)*?)(\x07)\002?')         # Operating System Command
+    """
+
+    ANSI_CSI_RE = re.compile(
+        "\001?\033\[((?:\d|;)*)([a-zA-Z])\002?"
+    )  # Control Sequence Introducer
+    ANSI_OSC_RE = re.compile(
+        "\001?\033\]((?:.|;)*?)(\x07)\002?"
+    )  # Operating System Command
 
     def __init__(self, wrapped, convert=None, strip=None, autoreset=False):
         # The wrapped stream (normally sys.stdout or sys.stderr)
@@ -59,7 +65,7 @@ class AnsiToWin32(object):
         # create the proxy wrapping our output stream
         self.stream = StreamWrapper(wrapped, self)
 
-        on_windows = os.name == 'nt'
+        on_windows = os.name == "nt"
         # We test if the WinAPI works, because even if we are on Windows
         # we may be using a terminal that doesn't support the WinAPI
         # (e.g. Cygwin Terminal). In this case it's up to the terminal
@@ -68,12 +74,18 @@ class AnsiToWin32(object):
 
         # should we strip ANSI sequences from our output?
         if strip is None:
-            strip = conversion_supported or (not is_stream_closed(wrapped) and not is_a_tty(wrapped))
+            strip = conversion_supported or (
+                not is_stream_closed(wrapped) and not is_a_tty(wrapped)
+            )
         self.strip = strip
 
         # should we should convert ANSI sequences into win32 calls?
         if convert is None:
-            convert = conversion_supported and not is_stream_closed(wrapped) and is_a_tty(wrapped)
+            convert = (
+                conversion_supported
+                and not is_stream_closed(wrapped)
+                and is_a_tty(wrapped)
+            )
         self.convert = convert
 
         # dict of ansi codes to win32 functions and parameters
@@ -83,19 +95,19 @@ class AnsiToWin32(object):
         self.on_stderr = self.wrapped is sys.stderr
 
     def should_wrap(self):
-        '''
+        """
         True if this class is actually needed. If false, then the output
         stream will not be affected, nor will win32 calls be issued, so
         wrapping stdout is not actually required. This will generally be
         False on non-Windows platforms, unless optional functionality like
         autoreset has been requested using kwargs to init()
-        '''
+        """
         return self.convert or self.strip or self.autoreset
 
     def get_win32_calls(self):
         if self.convert and winterm:
             return {
-                AnsiStyle.RESET_ALL: (winterm.reset_all, ),
+                AnsiStyle.RESET_ALL: (winterm.reset_all,),
                 AnsiStyle.BRIGHT: (winterm.style, WinStyle.BRIGHT),
                 AnsiStyle.DIM: (winterm.style, WinStyle.NORMAL),
                 AnsiStyle.NORMAL: (winterm.style, WinStyle.NORMAL),
@@ -107,7 +119,7 @@ class AnsiToWin32(object):
                 AnsiFore.MAGENTA: (winterm.fore, WinColor.MAGENTA),
                 AnsiFore.CYAN: (winterm.fore, WinColor.CYAN),
                 AnsiFore.WHITE: (winterm.fore, WinColor.GREY),
-                AnsiFore.RESET: (winterm.fore, ),
+                AnsiFore.RESET: (winterm.fore,),
                 AnsiFore.LIGHTBLACK_EX: (winterm.fore, WinColor.BLACK, True),
                 AnsiFore.LIGHTRED_EX: (winterm.fore, WinColor.RED, True),
                 AnsiFore.LIGHTGREEN_EX: (winterm.fore, WinColor.GREEN, True),
@@ -124,7 +136,7 @@ class AnsiToWin32(object):
                 AnsiBack.MAGENTA: (winterm.back, WinColor.MAGENTA),
                 AnsiBack.CYAN: (winterm.back, WinColor.CYAN),
                 AnsiBack.WHITE: (winterm.back, WinColor.GREY),
-                AnsiBack.RESET: (winterm.back, ),
+                AnsiBack.RESET: (winterm.back,),
                 AnsiBack.LIGHTBLACK_EX: (winterm.back, WinColor.BLACK, True),
                 AnsiBack.LIGHTRED_EX: (winterm.back, WinColor.RED, True),
                 AnsiBack.LIGHTGREEN_EX: (winterm.back, WinColor.GREEN, True),
@@ -145,20 +157,18 @@ class AnsiToWin32(object):
         if self.autoreset:
             self.reset_all()
 
-
     def reset_all(self):
         if self.convert:
-            self.call_win32('m', (0,))
+            self.call_win32("m", (0,))
         elif not self.strip and not is_stream_closed(self.wrapped):
             self.wrapped.write(Style.RESET_ALL)
 
-
     def write_and_convert(self, text):
-        '''
+        """
         Write the given text to our wrapped stream, stripping any ANSI
         sequences from the text, and optionally converting them into win32
         calls.
-        '''
+        """
         cursor = 0
         text = self.convert_osc(text)
         for match in self.ANSI_CSI_RE.finditer(text):
@@ -168,39 +178,35 @@ class AnsiToWin32(object):
             cursor = end
         self.write_plain_text(text, cursor, len(text))
 
-
     def write_plain_text(self, text, start, end):
         if start < end:
             self.wrapped.write(text[start:end])
             self.wrapped.flush()
 
-
     def convert_ansi(self, paramstring, command):
         if self.convert:
             params = self.extract_params(command, paramstring)
             self.call_win32(command, params)
 
-
     def extract_params(self, command, paramstring):
-        if command in 'Hf':
-            params = tuple(int(p) if len(p) != 0 else 1 for p in paramstring.split(';'))
+        if command in "Hf":
+            params = tuple(int(p) if len(p) != 0 else 1 for p in paramstring.split(";"))
             while len(params) < 2:
                 # defaults:
                 params = params + (1,)
         else:
-            params = tuple(int(p) for p in paramstring.split(';') if len(p) != 0)
+            params = tuple(int(p) for p in paramstring.split(";") if len(p) != 0)
             if len(params) == 0:
                 # defaults:
-                if command in 'JKm':
+                if command in "JKm":
                     params = (0,)
-                elif command in 'ABCD':
+                elif command in "ABCD":
                     params = (1,)
 
         return params
 
-
     def call_win32(self, command, params):
-        if command == 'm':
+        if command == "m":
             for param in params:
                 if param in self.win32_calls:
                     func_args = self.win32_calls[param]
@@ -208,29 +214,28 @@ class AnsiToWin32(object):
                     args = func_args[1:]
                     kwargs = dict(on_stderr=self.on_stderr)
                     func(*args, **kwargs)
-        elif command in 'J':
+        elif command in "J":
             winterm.erase_screen(params[0], on_stderr=self.on_stderr)
-        elif command in 'K':
+        elif command in "K":
             winterm.erase_line(params[0], on_stderr=self.on_stderr)
-        elif command in 'Hf':     # cursor position - absolute
+        elif command in "Hf":  # cursor position - absolute
             winterm.set_cursor_position(params, on_stderr=self.on_stderr)
-        elif command in 'ABCD':   # cursor position - relative
+        elif command in "ABCD":  # cursor position - relative
             n = params[0]
             # A - up, B - down, C - forward, D - back
-            x, y = {'A': (0, -n), 'B': (0, n), 'C': (n, 0), 'D': (-n, 0)}[command]
+            x, y = {"A": (0, -n), "B": (0, n), "C": (n, 0), "D": (-n, 0)}[command]
             winterm.cursor_adjust(x, y, on_stderr=self.on_stderr)
 
-
     def convert_osc(self, text):
         for match in self.ANSI_OSC_RE.finditer(text):
             start, end = match.span()
             text = text[:start] + text[end:]
             paramstring, command = match.groups()
-            if command in '\x07':       # \x07 = BEL
+            if command in "\x07":  # \x07 = BEL
                 params = paramstring.split(";")
                 # 0 - change title and icon (we will only change title)
                 # 1 - change icon (we don't support this)
                 # 2 - change title
-                if params[0] in '02':
+                if params[0] in "02":
                     winterm.set_title(params[1])
         return text