surec.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/python3
  2. from PyQt5.QtCore import QThread, pyqtSignal
  3. import subprocess, contextlib
  4. class SurecThread(QThread):
  5. update = pyqtSignal(str)
  6. def __init__(self, ebeveyn=None):
  7. super(SurecThread, self).__init__(ebeveyn)
  8. self.ebeveyn = ebeveyn
  9. self.komut = self.ebeveyn.komut
  10. def run(self):
  11. try:
  12. proc = subprocess.Popen(self.komut.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
  13. universal_newlines=True)
  14. for line in self.unbuffered(proc):
  15. self.update.emit(line)
  16. except:
  17. self.update.emit("> Hata")
  18. def unbuffered(self, proc, stream='stdout'):
  19. newlines = ['\n', '\r\n', '\r']
  20. stream = getattr(proc, stream)
  21. with contextlib.closing(stream):
  22. while True:
  23. out = []
  24. last = stream.read(1)
  25. if last == '' and proc.poll() is not None:
  26. break
  27. while last not in newlines:
  28. if last == '' and proc.poll() is not None:
  29. break
  30. out.append(last)
  31. last = stream.read(1)
  32. out = ''.join(out)
  33. yield out