Package node_manager_fkie :: Module supervised_popen
[frames] | no frames]

Source Code for Module node_manager_fkie.supervised_popen

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2012, Fraunhofer FKIE/US, Alexander Tiderko 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Fraunhofer nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32   
 33  from python_qt_binding.QtCore import QObject, Signal 
 34  try: 
 35      from python_qt_binding.QtGui import QMessageBox 
 36  except: 
 37      from python_qt_binding.QtWidgets import QMessageBox 
 38  import subprocess 
 39  import threading 
 40   
 41  from .detailed_msg_box import WarningMessageBox 
42 43 44 -class SupervisedPopen(QObject):
45 ''' 46 The class overrides the subprocess.Popen and waits in a thread for its finish. 47 If an error is printed out, it will be shown in a message dialog. 48 ''' 49 error = Signal(str, str, str) 50 '''@ivar: the signal is emitted if error output was detected (id, decription, message)''' 51 52 finished = Signal(str) 53 '''@ivar: the signal is emitted on exit (id)''' 54
55 - def __init__(self, args, bufsize=0, executable=None, stdin=None, stdout=None, 56 stderr=subprocess.PIPE, preexec_fn=None, close_fds=False, 57 shell=False, cwd=None, env=None, universal_newlines=False, 58 startupinfo=None, creationflags=0, object_id='', description=''):
59 ''' 60 For arguments see https://docs.python.org/2/library/subprocess.html 61 Additional arguments: 62 :param object_id: the identification string of this object and title of the 63 error message dialog 64 :type object_id: str 65 :param description: the description string used as addiotional information 66 in dialog if an error was occured 67 :type description: str 68 ''' 69 try: 70 QObject.__init__(self) 71 self._args = args 72 self._object_id = object_id 73 self._description = description 74 self.error.connect(self.on_error) 75 # wait for process to avoid 'defunct' processes 76 self.popen = subprocess.Popen(args=args, bufsize=bufsize, executable=executable, stdin=stdin, stdout=stdout, 77 stderr=stderr, preexec_fn=preexec_fn, close_fds=close_fds, shell=shell, cwd=cwd, env=env, 78 universal_newlines=universal_newlines, startupinfo=startupinfo, creationflags=creationflags) 79 thread = threading.Thread(target=self._supervise) 80 thread.setDaemon(True) 81 thread.start() 82 except Exception as _: 83 raise
84 85 # def __del__(self): 86 # print "Deleted:", self._description 87 88 @property
89 - def stdout(self):
90 return self.popen.stdout
91 92 @property
93 - def stderr(self):
94 return self.popen.stderr
95 96 @property
97 - def stdin(self):
98 return self.popen.stdin
99
100 - def _supervise(self):
101 ''' 102 Wait for process to avoid 'defunct' processes 103 ''' 104 self.popen.wait() 105 result_err = '' 106 if self.stderr is not None: 107 result_err = self.stderr.read() 108 if result_err: 109 self.error.emit(self._object_id, self._description, result_err) 110 self.finished.emit(self._object_id)
111
112 - def on_error(self, object_id, descr, msg):
113 WarningMessageBox(QMessageBox.Warning, object_id, '%s\n\n' 114 '%s' % (descr, msg), ' '.join(self._args)).exec_()
115