1
0
Fork 0
mirror of https://github.com/kbumsik/VirtScreen.git synced 2025-02-12 19:31:50 +00:00

Conformance to PEP 8

This commit is contained in:
Bumsik Kim 2018-05-16 17:37:24 -04:00
parent 6ccdd556f2
commit 9001fee975
No known key found for this signature in database
GPG key ID: E31041C8EC5B01C6

View file

@ -13,13 +13,13 @@ from PyQt5.QtQml import qmlRegisterType, QQmlApplicationEngine, QQmlListProperty
from twisted.internet import protocol, error from twisted.internet import protocol, error
from netifaces import interfaces, ifaddresses, AF_INET from netifaces import interfaces, ifaddresses, AF_INET
#------------------------------------------------------------------------------- # -------------------------------------------------------------------------------
# file path definitions # file path definitions
#------------------------------------------------------------------------------- # -------------------------------------------------------------------------------
# Sanitize environment variables # Sanitize environment variables
# https://wiki.sei.cmu.edu/confluence/display/c/ENV03-C.+Sanitize+the+environment+when+invoking+external+programs # https://wiki.sei.cmu.edu/confluence/display/c/ENV03-C.+Sanitize+the+environment+when+invoking+external+programs
del os.environ['HOME'] # Delete $HOME env for security reason. This will make del os.environ['HOME'] # Delete $HOME env for security reason. This will make
# Path.home() to look up in the password directory (pwd module) # Path.home() to look up in the password directory (pwd module)
os.environ['PATH'] = os.confstr("CS_PATH") # Sanitize $PATH os.environ['PATH'] = os.confstr("CS_PATH") # Sanitize $PATH
HOME_PATH = str(Path.home()) HOME_PATH = str(Path.home())
@ -35,9 +35,10 @@ ICON_PATH = PROGRAM_PATH + "/icon/icon.png"
ICON_TABLET_OFF_PATH = PROGRAM_PATH + "/icon/icon_tablet_off.png" ICON_TABLET_OFF_PATH = PROGRAM_PATH + "/icon/icon_tablet_off.png"
ICON_TABLET_ON_PATH = PROGRAM_PATH + "/icon/icon_tablet_on.png" ICON_TABLET_ON_PATH = PROGRAM_PATH + "/icon/icon_tablet_on.png"
#-------------------------------------------------------------------------------
# -------------------------------------------------------------------------------
# Subprocess wrapper # Subprocess wrapper
#------------------------------------------------------------------------------- # -------------------------------------------------------------------------------
class SubprocessWrapper: class SubprocessWrapper:
def __init__(self): def __init__(self):
pass pass
@ -51,9 +52,10 @@ class SubprocessWrapper:
return subprocess.run(arg.split(), input=input, stdout=subprocess.PIPE, return subprocess.run(arg.split(), input=input, stdout=subprocess.PIPE,
check=check, stderr=subprocess.STDOUT).stdout.decode('utf-8') check=check, stderr=subprocess.STDOUT).stdout.decode('utf-8')
#-------------------------------------------------------------------------------
# -------------------------------------------------------------------------------
# Twisted class # Twisted class
#------------------------------------------------------------------------------- # -------------------------------------------------------------------------------
class ProcessProtocol(protocol.ProcessProtocol): class ProcessProtocol(protocol.ProcessProtocol):
def __init__(self, onConnected, onOutReceived, onErrRecevied, onProcessEnded, logfile=None): def __init__(self, onConnected, onOutReceived, onErrRecevied, onProcessEnded, logfile=None):
self.onConnected = onConnected self.onConnected = onConnected
@ -125,12 +127,14 @@ class ProcessProtocol(protocol.ProcessProtocol):
print("quitting") print("quitting")
self.onProcessEnded(exitCode) self.onProcessEnded(exitCode)
#-------------------------------------------------------------------------------
# -------------------------------------------------------------------------------
# Display properties # Display properties
#------------------------------------------------------------------------------- # -------------------------------------------------------------------------------
class Display(object): class Display(object):
__slots__ = ['name', 'primary', 'connected', 'active', 'width', 'height', 'x_offset', 'y_offset'] __slots__ = ['name', 'primary', 'connected', 'active', 'width', 'height', 'x_offset', 'y_offset']
def __init__(self, parent=None):
def __init__(self):
self.name: str = None self.name: str = None
self.primary: bool = False self.primary: bool = False
self.connected: bool = False self.connected: bool = False
@ -154,9 +158,8 @@ class Display(object):
ret += f" not active {self.width}x{self.height}" ret += f" not active {self.width}x{self.height}"
return ret return ret
class DisplayProperty(QObject):
_display: Display
class DisplayProperty(QObject):
def __init__(self, display: Display, parent=None): def __init__(self, display: Display, parent=None):
super(DisplayProperty, self).__init__(parent) super(DisplayProperty, self).__init__(parent)
self._display = display self._display = display
@ -168,6 +171,7 @@ class DisplayProperty(QObject):
@pyqtProperty(str, constant=True) @pyqtProperty(str, constant=True)
def name(self): def name(self):
return self._display.name return self._display.name
@name.setter @name.setter
def name(self, name): def name(self, name):
self._display.name = name self._display.name = name
@ -175,6 +179,7 @@ class DisplayProperty(QObject):
@pyqtProperty(bool, constant=True) @pyqtProperty(bool, constant=True)
def primary(self): def primary(self):
return self._display.primary return self._display.primary
@primary.setter @primary.setter
def primary(self, primary): def primary(self, primary):
self._display.primary = primary self._display.primary = primary
@ -182,6 +187,7 @@ class DisplayProperty(QObject):
@pyqtProperty(bool, constant=True) @pyqtProperty(bool, constant=True)
def connected(self): def connected(self):
return self._display.connected return self._display.connected
@connected.setter @connected.setter
def connected(self, connected): def connected(self, connected):
self._display.connected = connected self._display.connected = connected
@ -189,6 +195,7 @@ class DisplayProperty(QObject):
@pyqtProperty(bool, constant=True) @pyqtProperty(bool, constant=True)
def active(self): def active(self):
return self._display.active return self._display.active
@active.setter @active.setter
def active(self, active): def active(self, active):
self._display.active = active self._display.active = active
@ -196,6 +203,7 @@ class DisplayProperty(QObject):
@pyqtProperty(int, constant=True) @pyqtProperty(int, constant=True)
def width(self): def width(self):
return self._display.width return self._display.width
@width.setter @width.setter
def width(self, width): def width(self, width):
self._display.width = width self._display.width = width
@ -203,6 +211,7 @@ class DisplayProperty(QObject):
@pyqtProperty(int, constant=True) @pyqtProperty(int, constant=True)
def height(self): def height(self):
return self._display.height return self._display.height
@height.setter @height.setter
def height(self, height): def height(self, height):
self._display.height = height self._display.height = height
@ -210,6 +219,7 @@ class DisplayProperty(QObject):
@pyqtProperty(int, constant=True) @pyqtProperty(int, constant=True)
def x_offset(self): def x_offset(self):
return self._display.x_offset return self._display.x_offset
@x_offset.setter @x_offset.setter
def x_offset(self, x_offset): def x_offset(self, x_offset):
self._display.x_offset = x_offset self._display.x_offset = x_offset
@ -217,13 +227,15 @@ class DisplayProperty(QObject):
@pyqtProperty(int, constant=True) @pyqtProperty(int, constant=True)
def y_offset(self): def y_offset(self):
return self._display.y_offset return self._display.y_offset
@y_offset.setter @y_offset.setter
def y_offset(self, y_offset): def y_offset(self, y_offset):
self._display.y_offset = y_offset self._display.y_offset = y_offset
#-------------------------------------------------------------------------------
# -------------------------------------------------------------------------------
# Screen adjustment class # Screen adjustment class
#------------------------------------------------------------------------------- # -------------------------------------------------------------------------------
class XRandR(SubprocessWrapper): class XRandR(SubprocessWrapper):
DEFAULT_VIRT_SCREEN = "VIRTUAL1" DEFAULT_VIRT_SCREEN = "VIRTUAL1"
VIRT_SCREEN_SUFFIX = "_virt" VIRT_SCREEN_SUFFIX = "_virt"
@ -268,7 +280,7 @@ class XRandR(SubprocessWrapper):
for s in self.screens: for s in self.screens:
print("\t", s) print("\t", s)
if self.virt_idx == self.primary_idx: if self.virt_idx == self.primary_idx:
raise RuntimeError("VIrtual screen must be selected other than the primary screen") raise RuntimeError("Virtual screen must be selected other than the primary screen")
if self.virt_idx is None: if self.virt_idx is None:
for idx, screen in enumerate(self.screens): for idx, screen in enumerate(self.screens):
if not screen.connected and not screen.active: if not screen.connected and not screen.active:
@ -338,11 +350,13 @@ class XRandR(SubprocessWrapper):
atexit.unregister(self.delete_virtual_screen) atexit.unregister(self.delete_virtual_screen)
self._update_screens() self._update_screens()
#-------------------------------------------------------------------------------
# -------------------------------------------------------------------------------
# QML Backend class # QML Backend class
#------------------------------------------------------------------------------- # -------------------------------------------------------------------------------
class Backend(QObject): class Backend(QObject):
""" Backend class for QML frontend """ """ Backend class for QML frontend """
class VNCState: class VNCState:
""" Enum to indicate a state of the VNC server """ """ Enum to indicate a state of the VNC server """
OFF = 0 OFF = 0
@ -383,6 +397,7 @@ class Backend(QObject):
except FileNotFoundError: except FileNotFoundError:
with open(DEFAULT_CONFIG_PATH, "r") as f: with open(DEFAULT_CONFIG_PATH, "r") as f:
return f.read() return f.read()
@settings.setter @settings.setter
def settings(self, json_str): def settings(self, json_str):
with open(CONFIG_PATH, "w") as f: with open(CONFIG_PATH, "w") as f:
@ -391,6 +406,7 @@ class Backend(QObject):
@pyqtProperty(bool, notify=onVirtScreenCreatedChanged) @pyqtProperty(bool, notify=onVirtScreenCreatedChanged)
def virtScreenCreated(self): def virtScreenCreated(self):
return self._virtScreenCreated return self._virtScreenCreated
@virtScreenCreated.setter @virtScreenCreated.setter
def virtScreenCreated(self, value): def virtScreenCreated(self, value):
self._virtScreenCreated = value self._virtScreenCreated = value
@ -403,6 +419,7 @@ class Backend(QObject):
@pyqtProperty(int, notify=onVirtScreenIndexChanged) @pyqtProperty(int, notify=onVirtScreenIndexChanged)
def virtScreenIndex(self): def virtScreenIndex(self):
return self._virtScreenIndex return self._virtScreenIndex
@virtScreenIndex.setter @virtScreenIndex.setter
def virtScreenIndex(self, virtScreenIndex): def virtScreenIndex(self, virtScreenIndex):
print("Changing virt to ", virtScreenIndex) print("Changing virt to ", virtScreenIndex)
@ -418,6 +435,7 @@ class Backend(QObject):
if self._vncUsePassword: if self._vncUsePassword:
self.vncUsePassword = False self.vncUsePassword = False
return self._vncUsePassword return self._vncUsePassword
@vncUsePassword.setter @vncUsePassword.setter
def vncUsePassword(self, use): def vncUsePassword(self, use):
self._vncUsePassword = use self._vncUsePassword = use
@ -426,6 +444,7 @@ class Backend(QObject):
@pyqtProperty(VNCState, notify=onVncStateChanged) @pyqtProperty(VNCState, notify=onVncStateChanged)
def vncState(self): def vncState(self):
return self._vncState return self._vncState
@vncState.setter @vncState.setter
def vncState(self, state): def vncState(self, state):
self._vncState = state self._vncState = state
@ -513,10 +532,12 @@ class Backend(QObject):
# regex used in callbacks # regex used in callbacks
patter_connected = re.compile(r"^.*Got connection from client.*$", re.M) patter_connected = re.compile(r"^.*Got connection from client.*$", re.M)
patter_disconnected = re.compile(r"^.*client_count: 0*$", re.M) patter_disconnected = re.compile(r"^.*client_count: 0*$", re.M)
# define callbacks # define callbacks
def _onConnected(): def _onConnected():
print("VNC started.") print("VNC started.")
self.vncState = self.VNCState.WAITING self.vncState = self.VNCState.WAITING
def _onReceived(data): def _onReceived(data):
data = data.decode("utf-8") data = data.decode("utf-8")
if (self._vncState is not self.VNCState.CONNECTED) and patter_connected.search(data): if (self._vncState is not self.VNCState.CONNECTED) and patter_connected.search(data):
@ -525,6 +546,7 @@ class Backend(QObject):
if (self._vncState is self.VNCState.CONNECTED) and patter_disconnected.search(data): if (self._vncState is self.VNCState.CONNECTED) and patter_disconnected.search(data):
print("VNC disconnected.") print("VNC disconnected.")
self.vncState = self.VNCState.WAITING self.vncState = self.VNCState.WAITING
def _onEnded(exitCode): def _onEnded(exitCode):
if exitCode is not 0: if exitCode is not 0:
self.vncState = self.VNCState.ERROR self.vncState = self.VNCState.ERROR
@ -534,6 +556,7 @@ class Backend(QObject):
self.vncState = self.VNCState.OFF self.vncState = self.VNCState.OFF
print("VNC Exited.") print("VNC Exited.")
atexit.unregister(self.stopVNC) atexit.unregister(self.stopVNC)
logfile = open(X11VNC_LOG_PATH, "wb") logfile = open(X11VNC_LOG_PATH, "wb")
self.vncServer = ProcessProtocol(_onConnected, _onReceived, _onReceived, _onEnded, logfile) self.vncServer = ProcessProtocol(_onConnected, _onReceived, _onReceived, _onEnded, logfile)
virt = self.xrandr.get_virtual_screen() virt = self.xrandr.get_virtual_screen()
@ -550,13 +573,16 @@ class Backend(QObject):
# define callbacks # define callbacks
def _onConnected(): def _onConnected():
print("External Display Setting opened.") print("External Display Setting opened.")
def _onReceived(data): def _onReceived(data):
pass pass
def _onEnded(exitCode): def _onEnded(exitCode):
print("External Display Setting closed.") print("External Display Setting closed.")
self.onDisplaySettingClosed.emit() self.onDisplaySettingClosed.emit()
if exitCode is not 0: if exitCode is not 0:
self.onError.emit(f'Error opening "{running_program}".') self.onError.emit(f'Error opening "{running_program}".')
program_list = ["gnome-control-center display", "arandr"] program_list = ["gnome-control-center display", "arandr"]
program = ProcessProtocol(_onConnected, _onReceived, _onReceived, _onEnded, None) program = ProcessProtocol(_onConnected, _onReceived, _onReceived, _onEnded, None)
running_program = '' running_program = ''
@ -578,7 +604,7 @@ class Backend(QObject):
if force: if force:
# Usually called from atexit(). # Usually called from atexit().
self.vncServer.kill() self.vncServer.kill()
time.sleep(2) # Make sure X11VNC shutdown before execute next atexit. time.sleep(2) # Make sure X11VNC shutdown before execute next atexit().
if self._vncState in (self.VNCState.WAITING, self.VNCState.CONNECTED): if self._vncState in (self.VNCState.WAITING, self.VNCState.CONNECTED):
self.vncServer.kill() self.vncServer.kill()
else: else:
@ -593,9 +619,10 @@ class Backend(QObject):
self.blockSignals(True) # This will prevent invoking auto-restart or etc. self.blockSignals(True) # This will prevent invoking auto-restart or etc.
QApplication.instance().quit() QApplication.instance().quit()
#-------------------------------------------------------------------------------
# -------------------------------------------------------------------------------
# Main Code # Main Code
#------------------------------------------------------------------------------- # -------------------------------------------------------------------------------
if __name__ == '__main__': if __name__ == '__main__':
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling) QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv) app = QApplication(sys.argv)
@ -624,6 +651,7 @@ if __name__ == '__main__':
sys.exit(1) sys.exit(1)
import qt5reactor # pylint: disable=E0401 import qt5reactor # pylint: disable=E0401
qt5reactor.install() qt5reactor.install()
from twisted.internet import utils, reactor # pylint: disable=E0401 from twisted.internet import utils, reactor # pylint: disable=E0401