Advertisement
ylSiew

Untitled

Aug 25th, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.06 KB | None | 0 0
  1. class FBConsoleWrapper(QtCore.QObject):
  2.     """
  3.    This wrapper class creates an object that emits a signal whenever
  4.    console output is written.
  5.    """
  6.  
  7.     def __init__(self, parent, stdout=True):
  8.         """
  9.        The constructor.
  10.        This redirects the system logging to internal class variables that
  11.        can be recalled.
  12.  
  13.        :param parent: ``QWidget`` that acts as parent for the console widget
  14.        :param stdout: ``bool`` determining if ``stdout`` logging should be captured.
  15.        :return: ``None``
  16.        """
  17.  
  18.         # Call base constructor
  19.         super(FBConsoleWrapper, self).__init__(self, parent)
  20.  
  21.         if stdout:
  22.             self._stream = sys.stdout
  23.             sys.stdout = self
  24.  
  25.         else:
  26.             self._stream = sys.stderr
  27.             sys.stderr = self
  28.  
  29.         self._stdout = stdout
  30.  
  31.         # Define custom QSignal to be emitted when logging has been output to
  32.         self.logging_written_to = QtCore.Signal(object, object)
  33.  
  34.  
  35.     def __getattr__(self, name):
  36.         """
  37.        This method is called if the attribute ``name`` has not already been
  38.        explicitly defined.
  39.  
  40.        :param name: ``string`` that is the attribute to get on the object.
  41.        :return: ``None``
  42.        """
  43.  
  44.         # Get the stream logging output and return it
  45.         return getattr(self._stream, name)
  46.  
  47.  
  48.     def __del__(self):
  49.         """
  50.        The destructor.
  51.        This redirects system logging back to the normal streams.
  52.  
  53.        :return:
  54.        """
  55.  
  56.         try:
  57.             if self._stdout:
  58.                 sys.stdout = self._stream
  59.             else:
  60.                 sys.stderr = self._stream
  61.  
  62.         except AttributeError:
  63.             pass
  64.  
  65.  
  66.     def write(self, text):
  67.         """
  68.        This method is called whenever there is new text to write to the
  69.        logger stream.
  70.  
  71.        :param text: ``string`` containing logging output to write.
  72.        :return: ``None``
  73.        """
  74.  
  75.         self._stream.write(text)
  76.         self.logging_written_to.emit(text, self._stdout)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement