Advertisement
ylSiew

Untitled

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