# Module `lib.tools.console`

Standard console redirector

Expand source code

    """ Standard console redirector """

    class Console:
            """ Console redirection """
            stdout = [None]

            @staticmethod
            def print(message, end=None):
                    """ Redirected print """
                    # pylint:disable=global-variable-not-assigned
                    if Console.stdout[0] is None:
                            if end is None:
                                    print(message)
                            else:
                                    print(message, end=end)
                    else:
                            Console.stdout[0].write(message)
                            if end is None:
                                    Console.stdout[0].write("\n")
                            else:
                                    Console.stdout[0].write(end)

            @staticmethod
            def is_redirected():
                    """ Indicates if the print is redirected """
                    if Console.stdout[0] is None:
                            return False
                    return True

            @staticmethod
            def open(filename, attrib="w", encoding="utf8"):
                    """ Open redirection """
                    if Console.is_redirected():
                            Console.close()
                    Console.stdout[0] = open(filename, attrib, encoding=encoding)

            @staticmethod
            def close():
                    """ Close redirection """
                    if Console.is_redirected():
                            Console.stdout[0].close()
                            Console.stdout[0] = None

## Classes

`class Console`

  Console redirection

  Expand source code

      class Console:
              """ Console redirection """
              stdout = [None]

              @staticmethod
              def print(message, end=None):
                      """ Redirected print """
                      # pylint:disable=global-variable-not-assigned
                      if Console.stdout[0] is None:
                              if end is None:
                                      print(message)
                              else:
                                      print(message, end=end)
                      else:
                              Console.stdout[0].write(message)
                              if end is None:
                                      Console.stdout[0].write("\n")
                              else:
                                      Console.stdout[0].write(end)

              @staticmethod
              def is_redirected():
                      """ Indicates if the print is redirected """
                      if Console.stdout[0] is None:
                              return False
                      return True

              @staticmethod
              def open(filename, attrib="w", encoding="utf8"):
                      """ Open redirection """
                      if Console.is_redirected():
                              Console.close()
                      Console.stdout[0] = open(filename, attrib, encoding=encoding)

              @staticmethod
              def close():
                      """ Close redirection """
                      if Console.is_redirected():
                              Console.stdout[0].close()
                              Console.stdout[0] = None

  ### Class variables

  `var stdout`

  ### Static methods

  `def close()`

    Close redirection

    Expand source code

        @staticmethod
        def close():
                """ Close redirection """
                if Console.is_redirected():
                        Console.stdout[0].close()
                        Console.stdout[0] = None

  `def is_redirected()`

    Indicates if the print is redirected

    Expand source code

        @staticmethod
        def is_redirected():
                """ Indicates if the print is redirected """
                if Console.stdout[0] is None:
                        return False
                return True

  `def open(filename, attrib='w', encoding='utf8')`

    Open redirection

    Expand source code

        @staticmethod
        def open(filename, attrib="w", encoding="utf8"):
                """ Open redirection """
                if Console.is_redirected():
                        Console.close()
                Console.stdout[0] = open(filename, attrib, encoding=encoding)

  `def print(message, end=None)`

    Redirected print

    Expand source code

        @staticmethod
        def print(message, end=None):
                """ Redirected print """
                # pylint:disable=global-variable-not-assigned
                if Console.stdout[0] is None:
                        if end is None:
                                print(message)
                        else:
                                print(message, end=end)
                else:
                        Console.stdout[0].write(message)
                        if end is None:
                                Console.stdout[0].write("\n")
                        else:
                                Console.stdout[0].write(end)