view RemoteViewer/Viewer.py @ 26:1601d498cfb1

close the log viewer after the process has terminated
author Dirk Olmes <dirk@xanthippe.ping.de>
date Fri, 26 Sep 2014 05:58:53 +0200
parents 2e5a12e3598e
children fb7442228526
line wrap: on
line source

import subprocess

from threading import Thread
from .LogViewer import LogViewer

class ReadThread(Thread):
    def __init__(self, process, viewer):
        super(ReadThread, self).__init__()
        self.daemon = True
        self.process = process
        self.viewer = viewer

    def run(self):
        loop = True
        while loop:
            if self.process.poll() is not None:
                self.viewer.finish()
                loop = False
            line = self.process.stdout.readline()
            line = line.decode('utf-8')
            line = line.rstrip('\n')
            self.viewer.append(line)

class AbstractViewer(object):
    def __init__(self,  connection):
        self.connection = connection
        self.process = None

    def open(self, parent):
        self.buildCommand()
        self.process = subprocess.Popen(self.command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        self.__startLogThread()
        self.process.wait()

    def __startLogThread(self):
        logViewer = self.__createLogViewer()
        readThread = ReadThread(self.process, logViewer)
        readThread.start()
        logViewer.exec_()

    def __createLogViewer(self):
        viewer = LogViewer()
        viewer.setWindowTitle(self.connection.displayString())
        command = 'running ' + ' '.join(self.command) + '\n'
        viewer.append(command)
        return viewer

class VncViewer(AbstractViewer):
    def buildCommand(self):
        self.command = ['vncviewer']
        self.append('-geometry', self.connection.geometry)
        self.append('-DesktopSize', self.connection.geometry)
        if self.connection.lowColor:
            self.command.append('-AutoSelect=0')
            self.append('-LowColourLevel', '1')
        self.command.append(self.connection.host)

    def append(self, option, value):
        if value is not None:
            self.command.append(option)
            self.command.append(value)

class FreeRdp(AbstractViewer):
    def buildCommand(self):
        self.command = ['xfreerdp', '+clipboard']
        self.append('/v:', self.connection.host)
        self.append('/t:', self.connection.displayString())
        self.append('/u:', self.connection.user)
        self.append('/size:', self.connection.geometry)
        if self.connection.lowColor:
            self.command.append('/bpp:8')

    def append(self, option, value):
        if value is not None:
            self.command.append(option + value)